1

If I do this in PHP, it works fine and loops as expected:

$rs = mysql_query($sql);
while ($row = mysql_fetch_assoc($rs)){
    writeme("UserID: " . $row["UserID"]);
}

But I keep wanting to abstract this out into a function I have called ExecuteQuery:

function ExecuteQuery($sql){
$result = mysql_query($sql);

if ($result) {
    if($result != 1){
        return mysql_fetch_assoc($result); // return recordset
    }
}else{
    $message  = 'Invalid query: ' . mysql_error() . "<br>";
    $message .= 'Whole query: ' . $sql;
    echo $message;
    die();
}

}

This function works great in 2 out of 3 scenarios:

1- Works great for a query that returns 1 row, and I can access like this:

$rs = ExecuteQuery($sql);

$foo = $rs["UserID"];

2- Works great for a sql statement that returns no records, like an UPDATE or DELETE.

3- But when I try to get back a recordset that returns multiple records, and then loop through it, I get an infinite loop and my browser crashes. Like this:

$rs = ExecuteQuery($sql);
while ($row = $rs){
    writeme("UserID: " . $row["UserID"]);
}

How can I modify my while loop so it advances to each new record in the recordset and stops after the last record? I'm sure it's a dumb little thing, but I'm not expert with PHP yet. I'd really like my ExecuteQuery function to be able to handle all 3 scenarios, it's very handy.

2
  • Haven't seen a good solution yet...still hoping. Commented Sep 19, 2012 at 21:29
  • didnt the solution I provided work? Commented Sep 20, 2012 at 12:41

3 Answers 3

1

try foreach($rs as $row){ instead of while ($row = $rs){

Sign up to request clarification or add additional context in comments.

6 Comments

This will not fetch the next row. It will simply iterate over the column values in a single row of results.
why wouldn't it fetch the next row, thats what foreach does, loops through the entire array. php.net/manual/en/control-structures.foreach.php
I think you have the params backward. I get this: Warning: Invalid argument supplied for foreach()
$rs being the resultset. foreach($rs as $row){ writeme("UserID: " . $row["UserID"]); } doesn't work?
OK, looks like the original post was edited. It used to be foreach($row as $rs). The new (edit) way sort of works, but it iterates way too many times, and I think it's actually looping every character of every string or something odd.
|
0

mysql_fetch_assoc() only returns one row of the result. To get the next row, you need to call mysql_fetch_assoc() again. One thing you could do is have your ExecuteQuery function return an array of arrays:

$rows = array();
while ($row = mysql_fetch_assoc($result) !== false) {
   $rows[] = $row;
}
return $rows;

Also, you should not use the mysql_* functions as they are deprecated. Try using PDO or mysqli_* instead.

Comments

0

Don't use while, use foreach:

$rs = ExecuteQuery($sql);
foreach ($rs as $row){
    writeme("UserID: " . $row["UserID"]);
}

1 Comment

I thought you were on to something, but now my looper goes about 20 times when there are only 2 records. I think it's looping every character in every string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.