How to Use Databases with PHP (Example)

Here is a simple example of how to use your MySQL database with a PHP script. The database used here is the one described in the Knowledge Base article How To Create a MySQL Database, in which we then created a table called books with 3 fields: IDauthortitle. The following script will output the title and author of each book:

$dbh=mysql_connect ("localhost", "johndoe_mysqlusername", "") or die ('Connexion error: ' . mysql_error());
mysql_select_db ("johndoe_project01");

$sql = "SELECT * FROM books";
$result = mysql_query($sql) or die('SQL Error: ' . mysql_error());
while ($row = mysql_fetch_array($result)){
echo 'Title: ' . $row['title'] . ', ';
echo 'Author: ' . $row['author'] . '
';
}

?>

For additional information, please refer to the PHP and MySQL web sites. A Google search of a specific problems also often turns up useful results!

  • 0 Korisnici koji smatraju članak korisnim
Je li Vam ovaj odgovor pomogao?

Vezani članci

How to Configure Software for MySQL Database Access

You'll often be asked to supply your MySQL database information in order to complete a software...

How to Create a MySQL Database

All hosting accounts allow you to use and create one or more MySQL databases. You can create...

How to Manage My MySQL Databases

To Create and Modify Users The easiest way to manually manage your database is from the cPanel....

Powered by WHMCompleteSolution