mySQL Probs

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tommy1987
Forum Commoner
Posts: 92
Joined: Tue Feb 21, 2006 8:35 pm

mySQL Probs

Post 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
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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:

Code: Select all

echo $row[subRegName]
you should have

Code: Select all

echo $row['subRegName']
(see the link in my sig for why $foo[bar] is wrong)
tommy1987
Forum Commoner
Posts: 92
Joined: Tue Feb 21, 2006 8:35 pm

Post 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,.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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 :oops:

Must go find me a brain...

Mac
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

twigletmac wrote:Teach me right to debug my own code and look at someone else's at the same time :oops:

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..
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
Post Reply