Forums

PHP

This topic is locked

mysql_fetch_array leaving off 1 result??

Posted 29 Oct 2002 20:17:36
1
has voted
29 Oct 2002 20:17:36 Justin B posted:
I am trying to display the results from a MySQL query into an html table with multiple columns, but it always leaves off one result! (ex: if I have 4 products, it will only display 3)

here is my code:

<?
$query_rs_products = sprintf("SELECT product_name, thumb_url FROM products WHERE active_status = 1 and category_id = %s";

$rs_products = mysql_query($query_rs_products, $vms_conn) or die(mysql_error());

$displaycols = 4;
$column = 0;

while ($myrow = mysql_fetch_array($rs_products)) {
if ($column == 0) {
echo "opening table row tag"
}

//begin html data
//all data here is between table data tags
//end of html data

$column++;
if ($column == $displaycols) {
echo "closing table row tags";
$column=0;
}
}
if($column != 0 || $column != $displaycols){
echo "closing table row tag";
}


Please help <img src=../images/dmxzone/forum/icon_smile.gif border=0 align=middle>!

Cheers,
Justin

Replies

Replied 30 Oct 2002 16:44:52
30 Oct 2002 16:44:52 Brent Colflesh replied:
Dear Justin,
What about changing:

if ($column == $displaycols) {

to

if ($column &gt; $displaycols) {

?

Regards,
Brent

Replied 30 Oct 2002 22:42:44
30 Oct 2002 22:42:44 Tim Green replied:
The problem is in the query... you're using:-

$query_rs_products = sprintf("SELECT product_name, thumb_url FROM products WHERE active_status = 1 and category_id = %s";

and asking for a string parameter %s to be replaced by a value. Problem is, you're not specifying the value... so your query will be sent literally as is to the server. The query format should be:-

$query_rs_products = sprintf("SELECT product_name, thumb_url FROM products WHERE active_status = 1 and category_id = %s",$myVariable);

Where $myVariable should contain the value you want to use for comparison against category_id.

Syntactically your code isn't wrong, which is why it apparently works... but doesn't yield results.

Hope this helps


Tim Green

Extension & PHP TalkZone Manager
<font size=1>-------------------------------------------
<i>Please read the Forum FAQ before posting
a question to this TalkZone.</i>
-------------------------------------------
www.UDzone.com : A dynamic Dreamweaver,
Ultradev and Fireworks site for developers
by developers.
-------------------------------------------</font id=size1>
Replied 30 Oct 2002 23:57:50
30 Oct 2002 23:57:50 Justin B replied:
Brent and Tim,

//Brent
The 'if ($column == $displaycols) {' is only for adding the opening table row tag which is working ok.
//end message to brent (see below for plea to both)

//Tim
I actually do have the %s variable included in the query, I just forgot to copy that into my post.

So my query looks like:

$query_rs_products = sprintf("SELECT product_id, product_name, thumb_url FROM products WHERE active_status = 1 and category_id = %s", $colname_rs_products);
//end message to tim (see below for plea to both)

Thank you both for the replies, but it still won't work! Any other suggestions?

Grateful, Frustrated, and Confused,
Justin
Replied 31 Oct 2002 00:08:18
31 Oct 2002 00:08:18 Justin B replied:
It finally works!!

I think this must have been the problem.

I began the repeat area with:
while ($myrow = mysql_fetch_array($rs_products)) {

but higher up in the page I had this line of unnecessary code:
$row_rs_products = mysql_fetch_assoc($rs_products);

Did calling mysql_fetch_assoc($rs_products); cause $rs_products to increment? It would make sense if that is the case.

Anyhow, it's working and thank you both for the replies.

By the way, what is the difference between mysql_fetch_assoc() and mysql_fetch_array()?

Cheers,
Justin

P.S. sorry for spelling 'greatful', 'grateful' (see closing of last post)

Reply to this topic