Property in Panama - Myspace Layouts - Find services - Debt Consolidation - Jigsaw Puzzles

PDA

View Full Version : Can't query database


MichaelLewis
Apr 3rd 2008, 4:44 pm
I have a database with a table called "testtbl" and a field called "UserID".
I have entered data in this table (54 rows) but I can't extract and print the data. I don't get any error messages either.

Can you tell me what's wrong with my code?

mysql_connect(localhost,$username);
mysql_select_db($database) or die ("Unable to select the database.");
$query = "SELECT UserID FROM testtbl";
$Results = mysql_db_query($database,$query);
mysql_close();
print("$Results<br>");

Thanks,
Michael

goldensea80
Apr 3rd 2008, 8:06 pm
First, your mysql connection does not seem good
Second, you cannot print the $Result directly by that way,
Try this:

<?php

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Not connected : ' . mysql_error());
}

// make foo the current db
$db_selected = mysql_select_db($database, $link);
if (!$db_selected) {
die ('Can\'t use ' .$database .':'. mysql_error());
}

$query = "SELECT UserID FROM testtbl";
$Results = mysql_query($query);

while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
echo "<br>".$row['UserID'];
}

?>