Viewing file: 12.php (1.09 KB) -rw-rw-rw- Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
<?php
function & connectToDb ($host,$dbUser,$dbPass,$dbName) {
// Make connection to MySQL server
if (!$dbConn = @mysql_connect($host, $dbUser, $dbPass))
return false;
// Select the database
if ( !@mysql_select_db($dbName) )
return false;
return $dbConn;
}
$host='localhost'; // Hostname of MySQL server
$dbUser='harryf'; // Username for MySQL
$dbPass='secret'; // Password for user
$dbName='sitepoint'; // Database name
// Connect to MySQL
$dbConn=& connectToDb($host,$dbUser,$dbPass,$dbName);
// A query to select all articles
$sql="SELECT * FROM articles ORDER BY title";
// Run the query, identifying the connection
$queryResource=mysql_query($sql,$dbConn);
// Fetch the number of rows selected
$numRows=mysql_num_rows($queryResource);
echo ( $numRows . " rows selected<br />" );
// Fetch rows from MySQL one at a time
while ($row=mysql_fetch_array($queryResource,MYSQL_ASSOC)) {
echo ( 'Title: '.$row['title'].'<br />' );
echo ( 'Author: '.$row['author'].'<br />' );
echo ( 'Body: '.$row['body'].'<br />' );
}
?>
|