Advertisements
Advertisements
Question
Explain MySQLi Queries with examples.
Advertisements
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?
Which is the not a correct MySQL Function in PHP?
Which version of PHP supports MySQLi fuctions?
What is MySQLi function?
What are the types MySQLi function available PHP?
Difference between Connection and Close function?
Write the Syntax for MySQLi Queries.
Write MySQL Connection Syntax with example.
Discuss in detail about MySQL functions with example.
Explain in details types of MySQL connection method in PHP?
