PDA

View Full Version : Error when trying to retrieve SQL data



mprzybylski
November 25th, 2004, 04:44 AM
I have the following code in a php file on my server:


<?
$server = "localhost";
$username = "reintro_matt";
$password = "*******";
$database = "reintro_testdb";
$table = "ecard";

//connect to database
$connection = mysql_pconnect($server, $username, $password) or die ("Couldn't connect to the database.");
$selection = mysql_select_db($database) or die ("No database selected.");

//make query
$sql = "SELECT * FROM '" . $table ."'";
$result = mysql_query($sql);
$nrows = mysql_num_rows($result);
if($nrows != 0)
{
print "<p>Data for " . $table;
print "<table border=2><tr><th>Name<th>Email\n";
for($j=0;$j<$nrows;$j++)
{
$row = mysql_fetch_array($result);
print "<tr><td>" . $row["from_name"];
print "<td>" . $row["from_email"];
print "\n";
}
print "</table>\n";
}
else print "<p>No Entry for " . $table;
mysql_close($connection);

if ($connection) {
$msg = "success!";
}
if ($selection) {
$msg2 = "selected!";
}

?>

<html>
<head>
<title>Did I get in?</title>
</head>

<body>

<br><br>

<? echo "$msg"; ?><br>
<? echo "$msg2"; ?>

</body>
</html>

the output can be viewed here:

http://www.reintroducing.com/play/query_test2.php

the connection to the database is successful but it's giving me that error and it's not displaying the row of data that i have in my table. any idea what might be the problem?

i'm a newbie to PHP so any help is greatly appreciated. thanks in advance.

mprzybylski
November 25th, 2004, 07:08 AM
forgot to turn on e-mail notification, this is just added here so that i can go ahead and do that, problem is still UNRESOLVED.

mprzybylski
November 25th, 2004, 01:14 PM
problem resolved, thanks to anyone that took a crack at it.

Voetsjoeba
November 25th, 2004, 03:37 PM
That error message means that the result was not valid, so that means that your query failed. Looks to me like it's the quotes that were causing the error. In SQL, quotes enclose a value, not an identifier.

SELECT * FROM 'table';

Here you use table as a value, not as the table identifier. This way, you could as well say SELECT * FROM 2;

So it should either be:

SELECT * FROM `table`; (notice the special ` character)

or just

SELECT * FROM table;