Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8551619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:14:06+00:00 2026-06-11T14:14:06+00:00

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

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T14:14:07+00:00Added an answer on June 11, 2026 at 2:14 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This rule works fine for http://foo.com/page/contact RewriteRule ^page/([^/\.]+)/?$ index.php?page=$1 [L] But what I want
We currently have a working php mail script, this works fine and as we
I have PHP script works without problems, like this : while($row4 = mysql_fetch_array($result4)) {
When using the PHP Sphinx client this works: SphinxClient::query ('*', 'index1'); But when I
I have a cron job set up to run testemail.php. This works fine, every
I have this page where I am doing testing http://www.comehike.com/test_fb_connect.php The button works fine
I tested this on firefox and it works fine, but in IE it doesn't
There is a website in Joomla, the NavigationPath ist domain.tld/index.php/produkte/produkt1.html this work fine. But
This works and is tested. PHP Code: $data_stats = json_decode($ret); $projected_points = $data_stats->body->player_stats->42550689->FPTS I
Can anyone convert this curl command that works in command line to php code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.