Sunday, December 19, 2010

retrieving_data.php

"; ?>
echo "Intro: Let's do a little generic database work!

";
$user = "root";
$passwd = "";

// Connecting, selecting database
$connect = mysql_connect('localhost', 'root', '')
or die('Could not connect: ' . mysql_error());
echo 'Step 1: Connected successfully!
';

$db = 'samples';
mysql_select_db($db) or die('Could not select database ('.$db.') because of : '.mysql_error());
echo 'Step 2: Connected to ('.$db.') successful!
';

// Performing SQL query
$query = 'SELECT * FROM tbl_users';
$result = mysql_query($query) or die('Query failed: ' . mysql_error());


// Printing results in HTML
echo "

Let's fetch data...
";
echo "
\n";

$count = 0;
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "\t \n";
foreach ($line as $col_value) {
echo "\t\t \n";
}
$count++;
echo "\t \n";
}

echo "
$col_value
\n";

if ($count < 1) {
echo "

No rows were found in this table.

";
} else {
echo "

".$count." rows were found in this table.

";
}

// We will free the resultset...
mysql_free_result($result);

// Now we close the connection...
mysql_close($connect);
?>

No comments:

Post a Comment