Problem pulling data from a database table using PHP

Status
Not open for further replies.

kiacovin71

New Member
Messages
1
Reaction score
0
Points
0
this code:


<html>
<head>
<title>php</title>
</head>
<body>
<h1>Show Database </h1>
<?
//make the database connection
$conn = mysql_connect("localhost", "clocky_time", "********");
mysql_select_db("clocky_clock", $conn);

//create a query
$sql = "SELECT * FROM clock_clock";
$result = mysql_query($sql, $conn);

print "<table border = 1>\n";

//get field names
print "<tr>\n";
while ($field = mysql_fetch_field($result)){
print " <th>$field->name</th>\n";
} // end while
print "</tr>\n\n";

//get row data as an associative array
while ($row = mysql_fetch_assoc($result)){
print "<tr>\n";
//look at each field
foreach ($row as $col=>$val){
print " <td>$val</td>\n";
} // end foreach
print "</tr>\n\n";
}// end while

print "</table>\n";
?>
</body>
</html>

which I changed from some code that works on my friends godaddy account is giving me the error:

"Show Database


Warning: mysql_fetch_field(): supplied argument is not a valid MySQL result resource in /home/clocky/public_html/showalldata.php on line 20

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/clocky/public_html/showalldata.php on line 26
"

Any ideas? Besides that I really like x10hosting
 
Last edited by a moderator:

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
Warning: mysql_fetch_field(): supplied argument is not a valid MySQL result resource in /home/clocky/public_html/showalldata.php on line 20

Look at what it is telling you. Count lines or use the Code Editor in cPanel or note the place you call mysql_fetch_field is

Code:
while ($field = mysql_fetch_field($result))

It is saying that $result is not a valid result. Where did it come from?

Code:
$result = mysql_query($sql, $conn);

mysql_query returns a result object on SELECT queries (even if there are no matches), unless there is an error. Hence you have an error.

To display the problem, always test the return value of the query.

Code:
$result = mysql_query($sql, $conn);
if (!$result) {
    echo('MySQL error: ' . mysql_error());
}
 
Status
Not open for further replies.
Top