Current Location: Home> Function Categories> mysqli::query

mysqli::query

(mysqli_query) Perform a query on the database
Name:mysqli::query
Category:MySQLi
Programming Language:php
One-line Description:Perform a query on the database.

Definition and usage

query() / mysqli_query() function executes a query on the database.

Example

Example 1 - Object-Oriented Style

Perform a query on the database:

 <?php
$mysqli = new mysqli ( "localhost" , "my_user" , "my_password" , "my_db" ) ;

// Check the connection
if ( $mysqli -> connect_errno ) {
  echo "Failed to connect to MySQL: " . $mysqli -> connect_error ;
  exit ( ) ;
}

// Execute query
if ( $result = $mysqli -> query ( "SELECT * FROM Persons" ) ) {
  echo "Returned rows are: " . $result -> num_rows ;
  // Release the result set
  $result -> free_result ( ) ;
}

$mysqli -> close ( ) ;
?>

Example 2 - Procedural Style

Perform a query on the database:

 <?php
$con = mysqli_connect ( "localhost" , "my_user" , "my_password" , "my_db" ) ;

if ( mysqli_connect_errno ( ) ) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error ( ) ;
  exit ( ) ;
}

// Execute query
if ( $result = mysqli_query ( $con , "SELECT * FROM Persons" ) ) {
  echo "Returned rows are: " . mysqli_num_rows ( $result ) ;
  // Release the result set
  mysqli_free_result ( $result ) ;
}

mysqli_close ( $con ) ;
?>
Similar Functions
Popular Articles