Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Disappearing act?

Status
Not open for further replies.

kehers

Programmer
Aug 31, 2005
56
A2
found out that when a bulk of codes comes between a mysql query and the mysql_fetch_array function, the result of the mysql_query statement dispappears ...as in something like below:
Code:
//db connection

$result = mysql_query($sql_statement);

//some long lines of codes, statements, loops, etc
//...that just must come in here
//eg,using mysql_num_rows to do some stunts

while(list($columnA, $columnB) = mysql_fetch_array($result)){
//this wont just work
//unless i requery the $sql_statement 
//...immediately b4 the while loop
//i.e adding ds: $result = mysql_query($sql_statement);
}
is there any way to keep $result alive no matter the distance between its declaration and use? or are my obsevations wrong?
 
I would do a search for $result to find out where it is getting reset at.
The amount of code between
$result = mysql_query($sql_statement)
and your
while(list($columnA, $columnB) = mysql_fetch_array($result))

makes no difference. The result set could be empty to begin with, $results may change before you mysql_fetch_array, but it just does not disappear :)


 
I agree with KingCrab that the most likely situation is that $result is manipulated somwhere. $result is just a resource handle (like a file handle) that allows the retrieval of the data.

What error is displayed? You say it 'disappears', which is not very descriptive. It could just be that there is no result.
Make sure to check if the query suceeds by adding a die() clause.
 
i do my queries like this:
Code:
<?php

$q = mysql_query("SELECT colA, colB, colC FROM tbl");
    while(list($colA,$colB,$colC) = mysql_fetch_row($q)){
       //do something here
    }
?>
Don't know if this will be of any help?

Regards,

Martin

Computing Help And Info:
 
I would add some error checking to the query executing line:
Code:
$q = mysql_query("SELECT colA, colB, colC FROM tbl") OR die('Query failed: '.mysql_error());

That will catch any SQL errors.
You could also forgoe the list() construct and retrieve the data using mysql_fetch_assoc which will return an associative array that is keyed by column name:
Code:
while ($row = mysql_fetch_assoc($q)){
   echo $row['colA'];
   // etc.
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top