PDA

View Full Version : Could Not Execute QUERY


sir pannels
01-08-2003, 01:05 PM
hmm wel topic says it all .. also it says...
Warning: Supplied argument is not a valid MySQL-Link resource in c:\phpdev\www\public\game\viewfleet2.php on line 7
heres the code.. could anyone find why its not workin? or give me some ideas.
thanks.

<?
$dbc = mysql_connect('localhost', '', '') or die("Couldn't connect to database");
mysql_select_db('GAME') or die("Couldn't select database");

$query = "SELECT * FROM fleet WHERE id=$id";

$rs=mysql_query($query,$conn)
or die("Could not execute query");

echo ("<table align='center' border=\"1\" cellpadding=\"2\" bordercolor=\"#0080C0\">");
echo ("<tr>");
echo ("<th bgcolor=\"#FFFFFF\"><font face=\"Terminal\" color=\"#FF0000\">Vessel Name</font></th>");
echo ("<th bgcolor=\"#FFFFFF\"><font face=\"Terminal\" color=\"#FF0000\">Description</font></th>");
echo ("<th bgcolor=\"#FFFFFF\"><font face=\"Terminal\" color=\"#FF0000\">Cost</font></th>");
echo ("<th bgcolor=\"#FFFFFF\"><font face=\"Terminal\" color=\"#FF0000\">Your Count</font></th>");
echo ("</tr>");

while($row= mysql_fetch_array($rs) )
{
$list .= "<tr>";
$list .= "<td align=\"center\"><font face=\"Verdana\"><small>".$row["vesselname"]."</small></font></td>";
$list .= "<td align=\"center\"><font face=\"Verdana\"><small>".$row["vesseldescription"]."</small></font></td>";
$list .= "<td align=\"center\"><font face=\"Verdana\"><small>".$row["vcost"]."</small></font></td>";
$list .= "<td align=\"center\"><font face=\"Verdana\"><small>".$row["vcount"]."</small></font></td>";

$list .= "</tr>";
}
$list .= "</table>";
echo($list);

?>

cheers
P:thumbsup:

Spookster
01-08-2003, 03:27 PM
$rs=mysql_query($query,$conn)

to

$rs=mysql_query($query)

sir pannels
01-08-2003, 04:02 PM
Thanks Spooks :)

Kiwi
01-08-2003, 04:02 PM
Or change it to:
$rs=mysql_query($query,$dbc)

If you supply a second argument to the mysql_query argument, it has to be a databse connection identifier -- in this case, you asigned that identifier to $dbc.

LaundroMat
01-08-2003, 04:26 PM
Isn't this the culprit?

$query = "SELECT * FROM fleet WHERE id=$id";

Shouldn't it be:

$query = "SELECT * FROM fleet WHERE id=" . $id;

Kiwi
01-08-2003, 04:30 PM
No. When you use double quotes in php, it parses and inserts the variable names (there's a technical term for it, but I can't remember what it is).

Depending in the datatype of id in mySql, there could need to add quotes to the query string:
$query = "SELECT * FROM fleet WHERE id='$id'";

I think this is necessary if id isn't a numerical datatype, but I suspect it is.

<edit>
If you're worried about the query itself, the best check I've come up with is to put the query into a variable, echo the variable, then run the query in sql directly. It is usually a lot easier to decipher the sql errors that way.

In this case, that's not really the problem, so it won't help.
</edit>

Ökii
01-08-2003, 06:45 PM
to clear up the $var in SELECT call issues.

using either single quotes around the $var or bouncing out of the double quoted string is only neccessary when the value of $var contains a space

$var = 8
"SELECT * FROM `table` WHERE var=$var" = good
"SELECT * FROM `table` WHERE var='$var'" = good
"SELECT * FROM `table` WHERE var=".$var = good

$var = "a string with spaces"
"SELECT * FROM `table` WHERE var=$var" = bad
"SELECT * FROM `table` WHERE var='$var'" = good
"SELECT * FROM `table` WHERE var=".$var = good