AdBlock Detected

We provide high-quality source code for free. Please consider disabling your AdBlocker to support our work.

Buy me a Coffee

Saved Tutorials

No saved posts yet.

Press Enter to see all results

How to Connect a PHP script to a MySQL Database

By pushpam abhishek
Listen to this article

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 this post

pushpam abhishek

About pushpam abhishek

Pushpam Abhishek is a Software & web developer and designer who specializes in back-end as well as front-end development. If you'd like to connect with him, follow him on Twitter as @pushpambhshk

Comments