Page 1 of 1
mySQL Probs
Posted: Tue Jun 06, 2006 7:03 am
by tommy1987
Can someone please help, im getting the following errors, with this peice of code:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource
Code:
Code: Select all
<?php
include("include-mysql.php"); // contains connection details..
$query = "SELECT * from $RegionSelected";
//$RegionSelected contains table name which changes
$result = mysql_query($query);
$i = 0;
while ($row = mysql_fetch_array($result)) {
// For all rows
echo $row[subRegName];
// I simply want to list this field followed by two newlines.
echo '<br/><br/>';
$i++;
}
?>
Hope someone can help, thanks a lot, Tom
Posted: Tue Jun 06, 2006 7:34 am
by twigletmac
Try a bit of error handling on the mysql_query() call to see if MySQL gives you a more useful error message:
Code: Select all
$result = mysql_query($query) or die(mysql_error().'<p>'.$query.'</p>');
Mac
P.S. You should always quote index names in arrays so instead of:
you should have
(see the link in my sig for why $foo[bar] is wrong)
Posted: Tue Jun 06, 2006 7:43 am
by tommy1987
Thanks very much for that.. Now I know what the problem is..
It is only getting the first word of the table passed to the page using GET e.g. page.php?inst=Channel Islands
It only gets Channel, hence hte table is not found, how can I make sure it gets the lot? If i was to echo the variable out it prints the whole lot, its only when being used for a table is snips it down to one word..
Thanks,.
Posted: Tue Jun 06, 2006 8:14 am
by twigletmac
You should just need to put some single quotes around the variable in the SQL, so:
Code: Select all
$query = "SELECT * from '$RegionSelected'";
instead of
Code: Select all
$query = "SELECT * from $RegionSelected";
Just like in PHP, in MySQL strings need to be quoted
Mac
Posted: Tue Jun 06, 2006 9:04 am
by twigletmac
onion2k wrote:twigletmac wrote:You should just need to put some single quotes around the variable in the SQL, so:
Code: Select all
$query = "SELECT * from '$RegionSelected'";
instead of
Code: Select all
$query = "SELECT * from $RegionSelected";
Just like in PHP, in MySQL strings need to be quoted
Mac
You sure about that? $RegionSelected is the name of a table, it's not a string. Backticks would be necessary if the table name is a reserved word..
Teach me right to debug my own code and look at someone else's at the same time
Must go find me a brain...
Mac
Posted: Tue Jun 06, 2006 9:05 am
by onion2k
twigletmac wrote:Teach me right to debug my own code and look at someone else's at the same time
Must go find me a brain...
Mac
You were actually right .. hence I deleted my post. Still better to use backticks though.
Better yet, don't make tables with spaces in the name..
Posted: Tue Jun 06, 2006 9:10 am
by twigletmac
onion2k wrote:You were actually right .. hence I deleted my post. Still better to use backticks though.
More luck than judgement there unfortunately...
onion2k wrote:Better yet, don't make tables with spaces in the name..
+1 there - keeps things much simpler
Mac