Spec-Zone .ru
спецификации, руководства, описания, API

22.9.2.4. Examples

Copyright 1997-2012 the PHP Documentation Group.

22.9.2.4.1. MySQL extension overview example

Copyright 1997-2012 the PHP Documentation Group.

This simple example shows how to connect, execute a query, print resulting rows and disconnect from a MySQL database.

Example 22.22. MySQL extension overview example

<?php// Connecting, selecting database$link = mysql_connect('mysql_host', 'mysql_user', 'mysql_password')    or die('Could not connect: ' . mysql_error());echo 'Connected successfully';mysql_select_db('my_database') or die('Could not select database');// Performing SQL query$query = 'SELECT * FROM my_table';$result = mysql_query($query) or die('Query failed: ' . mysql_error());// Printing results in HTMLecho "<table>\n";while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {    echo "\t<tr>\n";    foreach ($line as $col_value) {        echo "\t\t<td>$col_value</td>\n";    }    echo "\t</tr>\n";}echo "</table>\n";// Free resultsetmysql_free_result($result);// Closing connectionmysql_close($link);?>