Forum Moderators: coopster

Message Too Old, No Replies

having issues with mysql fetch array()

         

dungareez

4:23 am on Aug 24, 2007 (gmt 0)

10+ Year Member



Hi all,

I keep having serious issues with using mysql_fetch_array($result) and I don't know why. I have used it many times previously without issue. Here is the code I am using (I have added in a few items while trying to debug it).

function vendorReactivateList()
{
$query = "SELECT * FROM vendors";
$result = mysql_query($query) or die (mysql_error());
$num = mysql_num_rows($result);// used this to verify I was getting results (I am)
$returnValue ="centercontent_____<font size=\"3\">Inactive Vendors</font><br /><br />"; // this is just part of the output

while($row = mysql_fetch_array($result));
{
$name = $row["name"];
$active = $row["active"];
$vID = $row["vendorID"];
if($active!=1)// am only doing this to simplify query for debugging, later this will be eliminated and query will be more specific
{
$returnValue .=$name."&nbsp;&nbsp;&nbsp;&nbsp;";
$returnValue .="<a href=\"#\" onclick=\"gensend('mode=9&vendorID=".$vID."&reactivate=vendor')\";>Reactivate</a><br />";
}
}
$returnValue .=$num;
$returnValue .= mysql_result($result,0,'name');//used this to again verify I am getting an accessible result, I am. I can even iterate through this to bypass my problem with mysql_fetch_array , but I want to solve that problem as it has occurred to me in another spot as well
return $returnValue ;
}

I swear I have looked at the syntax of the mysql_fetch_array syntax many times and can't see anything wrong. I am getting in the loop but it is not going through the array. I am seeing only one result without any values from the query (excluding my test with mysql_result($result,0,'name')). Any help would be very greatly appreciated as this issue is frustrating the heck out of me.

Thanks

vincevincevince

6:10 am on Aug 24, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$returnValue .= mysql_result($result,0,'name');

You cannot use both mysql_result and mysql_fetch_array() - they are not compatible.

From php.net:

Note: Calls to mysql_result() should not be mixed with calls to other functions that deal with the result set.

dungareez

2:46 pm on Aug 24, 2007 (gmt 0)

10+ Year Member



Hi Vince,

Thanks for the response,

I only used the mysql_result($result,0,'name'); after I was having issues with the mysql_fetch_array to verify there was nothing wrong with my query. Even with it removed the mysql_fetch_array is not working.

I did a var_dump($row) and it returned bool(false) (this is with the mysql_result() removed).

I do use the mysql_result() in another part of my code but it is a completely separate query and I am using it as a workaround for this same issue.

Any further input would be greatly appreciated. Thanks again

vincevincevince

2:58 pm on Aug 24, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Can you try changing:
 while($row = mysql_fetch_array($result));
{
$name = $row["name"];
$active = $row["active"];
$vID = $row["vendorID"];
if($active!=1)// am only doing this to simplify query for debugging, later this will be eliminated and query will be more specific
{
$returnValue .=$name."&nbsp;&nbsp;&nbsp;&nbsp;";
$returnValue .="<a href=\"#\" onclick=\"gensend('mode=9&vendorID=".$vID."&reactivate=vendor')\";>Reactivate</a><br />";
}
}

Into:


while($row = mysql_fetch_array($result));
{
print_r($row);
}

Let's find out whether the problem is inside the loop or outside. Tell me how many Array(....) you get.

dungareez

3:26 pm on Aug 24, 2007 (gmt 0)

10+ Year Member



Ok Vince,

I had to convert what you wanted me to do into:

while($row = mysql_fetch_array($result));
{
$returnValue .= print_r($row);
}

and returned the $returnValue because this is an AJAX response that needs to be parsed to be viewed but I think it is essentially the same.

Anyhow my resulting output was: 1

So I know I get in the loop but I should get 4 passes. In fact when I run mysql_num_rows($result); I get a result of 4.

thanks again for your help

vincevincevince

3:31 pm on Aug 24, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you convert print_r like that, then you need to add ,true

print_r($row,true);

The '1' you got was just the 'true' to say that print_r() had worked ...

dungareez

3:57 pm on Aug 24, 2007 (gmt 0)

10+ Year Member



I updated the print_r($row) to print_r($row,true);

Now I get nothing from the loop.

pinterface

4:01 pm on Aug 24, 2007 (gmt 0)

10+ Year Member



while (...)[5][b];[/b][/5] { ... }

Hint: What does a semicolon mean in PHP?

dungareez

4:18 pm on Aug 24, 2007 (gmt 0)

10+ Year Member



Don't I feel stupid. Thanks so much Vince. I can't believe the amount of time I spent looking for the solution when it was staring me in the face like that.

everything working fine now.

I really appreciate you taking the time to look at this for me.

dungareez

4:19 pm on Aug 24, 2007 (gmt 0)

10+ Year Member



thanks pinterFace, sorry I been responding with Vince so much I didn't look at the name right away.

vincevincevince

4:37 pm on Aug 24, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



pinterface - well done :)

jatar_k

4:43 pm on Aug 24, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>> I can't believe the amount of time I spent looking for the solution when it was staring me in the face like that

we all need an extra pair of eyes