There are simple 5 steps on how your PHP interacts (connects) to your MySQL database (via PHPMyAdmin). Here are php code snippets you can use.
1. Create a connection.
<php?
//Declares variables.
$DBHOST = “localhost”;
$USERNAME = “root”;
$PASSWORD = “”; //if your Dbase has a password, put it between the two double quotes; otherwise leave it blank.
//creates connection
$connection = mysql_connect(“DBHOST”,”USERNAME”,””); or die(“Database connection failed” . mysql_error());
?>
The die() function will execute if the connection has failed.
2. Selecting a database
<php?
$select_db = mysql_select_db(“dbMyDatabase”, $connection); or die(“Database selection failed” . mysql_error());
?>
3. Perform a database query.
<php?
$query_result = mysql_query(“SELECT * FROM tbMyTable
WHERE id = 1
ORDER BY 1 ASC”, $connection); or die(“Database selection has failed” . mysql_error());
?>
4. Use returned data (if any)
<php?
while($row=mysql_fetch_array($query_result)){
echo $row[1] . “ “ . row[2] . “<br />”; //prints the records from tablerow1 and tablerow2 simultaneously
}
?>
5. Close the connection.
<php?
mysql_close($connection);
?>
Putting all
the steps in your PHP as one will look
like this:
1.
Create a connection
//Your Codes here
2.
Database selection
<html >
<head >
<title>My
PHP File
< /title>
</head >
<body >
3.
Performing a query
selection
//Your
Codes here
4.
Use returned a data
//Your
Codes here
</body >
</html >
5.
Close a connection.
//Your
Codes here
Your thoughts about this post? Leave a commment now.