Advertisements
Advertisements
Question
Explain MySQLi Queries with examples.
Solution
- Performing Queries:
The main goal of MySQL and PHP connectivity is to retrieve and manipulate the data from MySQL database server. The SQL query statements are helping with PHP MySQL extension ^to achieve the objective of MySQL and PHP connection. “mysqli_query” is a function, helps to execute the SQL query statements in PHP scripting language.
Syntax:
mysqli_query(“Connection Object” “SQL Query”)
Example:
$con=mysqli_connect(“localhost”,“my_user”,“my_password”,“Student_DB”); $sql=“SELECT student_name,student_age FROM student”;mysqli_query($con,$sql);
- Closing Connection:
mysqli_close( ) Function is used to close an existing opened database connection between. PHP scripting and MySQL Database Server.
Syntax:
mysqli_close(“Connection Object”);
<?php
$con=mysqli_connect(“localhost”,“$user”,“$password”,“SCHOOLDB”);
// ….some PHP code… mysqli_close($con);
?>
Example of PHP and MySQL Program:
<?php .
$servemame = “localhost”;
$usemame = “username”;
$password = “password”;
$dbname = “schoolDB”;
$connection = mysqli_connect(“$servemame”, “$usemame”, “$password” “$dbname”);
if (mysqli_connect_error ( ))
{
echo “Failed to connect to MySQL:”
mysqli_connect_error( );
}
sql stmt = “SELECT * FROM mycontacts”; //SQL select query
$result = mysqli_query($connection,$sql_stmt);//execute SQL statement$rows =
mysqli__num_r°ws($result);// get number of rows returned
if($rows) {
while ($row = mysqli_fetch_array($result)) {
echo ‘ID:’. $row[‘id’]. ‘<br>’;
APPEARS IN
RELATED QUESTIONS
Which is the correct function to establish connection in PHP?
How many parameter are required for MYSQLi connect function in PHP?
What is MySQLi function?
Difference between Connection and Close function?
Give few examples of MySQLi Queries.
What is Connection string?
Write the Syntax for MySQLi Queries.
Write is the purpose of MySQLi function available.
Write MySQL Connection Syntax with example.
Discuss in detail about MySQL functions with example.