Use app×
Join Bloom Tuition
One on One Online Tuition
JEE MAIN 2025 Foundation Course
NEET 2025 Foundation Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
0 votes
991 views
in Connecting PHP and MYSQL by (54.8k points)
closed by

Explain MySQLi Queries with examples?

2 Answers

+1 vote
by (49.1k points)
selected by
 
Best 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>';

0 votes
by (85 points)

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: " . $

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...