PHP : MySQL Select Data

The MySQL SELECT statement is used to select data from database tables.

Process of Data Fetching/Retrieving:

  • Create Database connection using mysqli_connect()
  • Use mysqli_query() to perform a select query on database
  • mysqli_query() returns a resource (data type) on success for select statement
  • The returned resource should be passed to mysqli_fetch_array() to access data/tables.
  • mysqli_fetch_array() Returns an array of strings for each table row or NULL if there are no more rows in resultset.
  • Finally, The while() loop iterates to display data from each row.
 * Create Database Connection
$database_server = 'localhost';
$database_username = 'root';
$database_password = '';
$database_name = 'student';
// Create connection using mysqli_connect()
$conn = mysqli_connect($database_server, $database_username, $database_password, $database_name);
// If $conn is false, connection is failed
if (!$conn ) {
  die("Failed to connect to MySQL: " . mysqli_connect_error());
echo "Database Connected Successfully. <br />";
 * Select Table Data
 * Fectching aata from database using mysqli_fetch_array() function and without table tag
// Mysql query to select data from table
$mysql_query_statement = "SELECT * FROM users";
$result = $conn->query($mysql_query_statement);
while($user_data = mysqli_fetch_array($result)) {
 	echo $user_data['name']." ".$user_data['mobile']." ".$user_data['email'] . '<br />';
//Connection Close	

Congratulations! Chapter Finished. Learn more about the similar topics:
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.