Jump to content

How to debug inside a while loop


Recommended Posts

Hey guys,


I am trying to grab and loop data from my DB with a while loop and extract() function.


right now i have something like this:


echo '<div>'; // CONTAINER DIV
echo '<h2>News about '. $db_name. '</h2>';

$query = 'SELECT * FROM `news_to_people` WHERE `people_db_id` = '.$db_id;
$r_query = mysql_query($query);


$new_q = 'SELECT * FROM `news` WHERE `id` = '.$news_id;
$run_q = mysql_query($new_q);

$rows = mysql_fetch_assoc($run_q);
echo '<ul>';
echo '<l1><a href="article?articleid='.$id.'&name='.$db_name.'">'.$title.'<br></l1>';
echo '</ul>';
echo '</div>'; // END TO CONTAINER

Now here i get the loop to fire correctly and execute.


It then loops all 170+ links except in the middle of the loop , there is an error:



Warning: extract() expects parameter 1 to be array, boolean given in/home/justin/public_html/include/demo/center_content.php on line 351



so this means that i have a failed 2nd query somewhere in that it is skipping inside the while loop.


i need to debug and find which query is bringing this error.


my question is what is the best way i can write an if statement to see which query is failing or which query is not returning a proper array. also my if the query was failing wouldn't the error be asking for a proper resource ? this means the query is firing correctly and the extract() function is failing to receive all the rows. meaning that one of the rows must be returning empty or zero ? 


any help is much appreciated.

Link to comment
Share on other sites

Several points worth mentioning with that script.


  1. You only want the news_id in that first query so "SELECT news_id FROM ....". Don't use "SELECT * ", select only what you need .
  2. A query returning no rows is not an error.
  3. Don't run queries inside loops - use a JOIN to retrieve the data with a single query.
  4. mysql_ library has been replaced by mysqli_ and will not be available after v5.4
$query = "SELECT n.id
    , n.title
    FROM news_to_people np
        INNER JOIN news n ON np.news_id = n.id
    WHERE np.people_db_id = $db_id";

$res = $db->query($query);
echo "<div>";
while (list($id, $title) = $res->fetch_assoc()) {
  echo '<ul>';
  echo '<l1><a href="article?articleid='.$id.'&name='.$db_name.'">'.$title.'<br></l1>';
  echo '</ul>';
echo '</div>'; // END TO CONTAINER
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.