How to Connect a PHP script to a MySQL Database

How to Connect a PHP script to a MySQL Database,PHP to connect to the MySQL database server at 'localhost',Execute the SQL Statement: mysql_query
Share it:

How to Connect a PHP script to a MySQL Database

PHP to connect to the MySQL database server at 'localhost', using the username stored in $database_username and the password in $database_password. After you have connected to the database server you must then select the database you wish to use.

How to Connect a PHP script to a MySQL Database


<?php
 
 // Collects data from "user" table
 $sql = "SELECT * FROM user";
 $resultSet = mysql_query($sql, $conn) or die("Could not execute the query." . mysql_error());

?>

Execute the SQL Statement: mysql_query



For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning a result set, mysql_query() returns a result set on success, or FALSE on error.
For other types of SQL statements like INSERT, UPDATE, DELETE, or DROP, mysql_query() returns TRUE on success or FALSE on error.

Once connected to the database let’s try to retrieve some information from the ‘user’ MySQL table.
 <?php
 
 // Collects data from "user" table
 $sql = "SELECT * FROM user";
 $resultSet = mysql_query($sql, $conn) or die("Could not execute the query." . mysql_error());

?>

Note: In this case, the whole content of the user table is now contained in a special array with the name $resultSet.
Share it:

MySQL

PHP

Post A Comment:

0 comments: