PHP and MYSQL

Associate
Joined
4 Mar 2003
Posts
1,484
Hi All,

could do with a little help, i've coded some php to pull results from mysql database, the problem is i keep getting the following error:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /content/StartupHostPlus/p/r/propertywherehouse.com/web/products.php on line 48

Code:
<?php

	// This page will list all of the items
	// from the items table. Each item will have
	// a link to add it to the cart

	include("db.php");
	
	// Get a connection to the database
	$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);
	$result = mysql_query("select * from 'propwh'");
	?>
		
		<table width="750" cellspacing="0" cellpadding="0" border="0">
			<tr>
				<td width="25%" height="25" bgcolor="#CCCCCC">
					<font face="verdana" size="1" color="white">
						&nbsp;&nbsp;<b>Product</b>
					</font>
				</td>


				<td width="30%" height="25" bgcolor="#CCCCCC">
					<font face="verdana" size="1" color="white">
						&nbsp;&nbsp;<b>Image</b>
					</font>
				</td>


				<td width="10%" height="25" bgcolor="#CCCCCC">
					<font face="verdana" size="1" color="white">
						<b>Price</b>
					</font>
				</td>
				<td width="35%" height="25" bgcolor="#CCCCCC">
					<font face="verdana" size="1" color="white">
						<b>Description</b>
					</font>
				
			</tr>
			</table>
<br>
			<table width="750" cellspacing="0" cellpadding="0" border="0">
			
			<?php
			while($row = mysql_fetch_array($result))
			{
			?>
				<tr>
					<td width="25%" height="25">
						<font face="verdana" size="1" color="black">
							<?php echo $row["Location"]; ?>
						</font>
					</td>

					 <td width="30%" height="25"> 
<font face="verdana" size="1" color="black">
                        <a href="<?php echo $row['ref']; ?>" target"_blank" ><img border="0" img src="<?php echo $row["itemImage1"]; ?> "></a> 
</font>                    
</td>  




					<td width="10%" height="25">
						<font face="verdana" size="1" color="black">
							£<?php echo $row["itemPrice"]; ?>
						</font>
					</td>
					<td width="35%" height="25">
						<font face="verdana" size="1" color="black">
							<?php echo $row["itemDesc"]; ?>
						</font>
					</td>

					
				</tr>
				<tr>
					<td width="100%" colspan="5">
						<hr size="1" color="black" NOSHADE>
					</td>
				</tr>
			<?php
			}
		?>
			<tr>
				<td width="100%" colspan="4">
					<font face="verdana" size="1" color="black">
						<a href="cart.php">Your Shopping Cart &gt;&gt;</a>
					</font>
				</td>
			</tr>
		</table>

</center>

</body>

</html>

line 48 is:

while($row = mysql_fetch_array($result))


any ideas?

Thanks for your help in advance.
 
i don't know what's wrong. i'm using a similar query on a page of mine, and using your method works fine. :p

i think your php is fine. time to check the db/query perhaps? :)
 
Last edited:
marc2003 said:
i don't know what's wrong. i'm using a similar query on a page of mine, and using your method works fine. :p

i think your php is fine. time to check the db/query perhaps? :)

you were right lol

$result = mysql_query("select * from 'propwh'");

I removed the quotes so it looked like

$result = mysql_query("select * from propwh");

it now works :D
 
Single quotes are not used for field/table etc. names in recent SQL standards, nor any dialects I know of.

MySQL uses backticks (`) which is what you may have been trying to emulate.
 
psyr33n said:
Single quotes are not used for field/table etc. names in recent SQL standards, nor any dialects I know of.

MySQL uses backticks (`) which is what you may have been trying to emulate.
Yup. This is true... At first I didn't glance close enough to differentiate between ' and `. Irony being neither are needed really.
 
jdickerson said:
Yup. This is true... At first I didn't glance close enough to differentiate between ' and `. Irony being neither are needed really.

if the table name has a SQL command in it such as select then the `` brackets are required.

"SELECT * FROM select" would cause an issue for example.
 
When testing queries, to get a better idea of problems when one isn't working is to do the following:

Code:
$result = mysql_query("select * from 'propwh'") or die(mysql_error());

This will kill the script and let you know of any errors MySQL finds with the query. Using this, you may have found your initial problem rather quickly.

Obviously for production scripts you wouldn't use this; rather you would write your own die() function which lets you know of errors in a less verbose way.
 
Back
Top Bottom