Cant get data to show from mysql.

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
twcast
Forum Newbie
Posts: 4
Joined: Tue Jul 13, 2010 1:23 am

Cant get data to show from mysql.

Post by twcast »

I have some code so far, but it is not working. It seems as if it should, I have been over it 100 times and cannot find a problem.
As you can see, I am quite a newbie...

Code: Select all

$result = mysql_query("SELECT * FROM inv_items ORDER BY RAND() LIMIT 0,5");
$i=0;
$img=array();
$id=array();
$name=array();
while($row= mysql_fetch_array($result)){
$img1 = strstr($row[desc_picture],':');
$img2 = substr($img1,1);
$result1= mysql_query("select filename from images where id=$img2");
$row6= mysql_fetch_array($result1);
$img[i] = substr($row6[filename], 0, strpos($row6[filename],'[')-1);
$id[i]=$row[id];
$name[i]=$row[desc_name];
$i++;
I am not getting any errors, but no data will show when I add them to a table such as:

Code: Select all

<img src=\"/images/products/{$img[0]}T.jpg\"/></td><td valign='middle'>
<p>{$name[0]}</td><td valign=\"middle\">
<input type=\"text\" value=\"1\" name=\"additem_{$id[0]}\" class=\"inpAddToCartQty\" /></td><td valign=\"middle\">
<input type=\"submit\" value=\"Add to Cart\" class=\"inpAddToCartBtn\" />
<input type=\"hidden\" value=\"{$id[0]}\" class=\"rowItemId\" />
ALL advice is very much appreciated
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Cant get data to show from mysql.

Post by Celauran »

Since it isn't specified in the code you posted, have you actually established a connection to the database? Have you tried adding

Code: Select all

or die(mysql_error())
to the end of your queries?
twcast
Forum Newbie
Posts: 4
Joined: Tue Jul 13, 2010 1:23 am

Re: Cant get data to show from mysql.

Post by twcast »

Yes, I have a database connection.

If I use this:

Code: Select all

	
$result = mysql_query("SELECT * FROM inv_items ORDER BY RAND() LIMIT 0,5");

while($row = mysql_fetch_array($result))
while($row1 = mysql_fetch_array($result))
while($row2 = mysql_fetch_array($result))
while($row3 = mysql_fetch_array($result))
while($row4 = mysql_fetch_array($result))    
then I will at least show a description, but I am trying to add images to the RAND() up above to get all the info I need for my pre-cart.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Cant get data to show from mysql.

Post by califdon »

Among other things, you should read this, about using ORDER BY RAND():
http://www.titov.net/2005/09/21/do-not- ... rom-table/

When a query isn't producing expected results, you must trace what your code is actually doing. It's always a good idea to assign your query text to a variable so you can easily print it out to debug what it is really sending to the database engine. In other words, don't do this:

Code: Select all

$result = mysql_query("SELECT * FROM inv_items ORDER BY RAND() LIMIT 0,5");
do this:

Code: Select all

$sql = "SELECT * FROM inv_items ORDER BY RAND() LIMIT 0,5";
$result = $result = mysql_query($sql);
Then, for debugging, you can add the line:

Code: Select all

echo $sql;  // debugging
But I think there's a contradiction in using ORDER BY RAND() and LIMIT 0,5. Besides the advice in the above reference to not use ORDER BY RAND() at all, the LIMIT 0,5 tells the database engine that you specifically want to begin with the first row ("0") and return the next 5 rows. I'm not 100% certain, but I think that conflicts with asking for a random sort. I would suggest you follow the advice in that reference, which explains several options.
Post Reply