1.

Explain MySQLi Queries with examples?

Answer»

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_pass word” , “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>';

Example - Object Oriented style

Perform query against a database: $mysqli = new mysqli("localhost","my_user","my_password","my_db"); // Check connection. if ($mysqli -> connect_errno) { echo "Failed to connect to MySQL: " . $



Discussion

No Comment Found