justin7410 Posted January 11, 2014 Share Posted January 11, 2014 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); while($rows=mysql_fetch_assoc($r_query)){ extract($rows); $new_q = 'SELECT * FROM `news` WHERE `id` = '.$news_id; $run_q = mysql_query($new_q); $rows = mysql_fetch_assoc($run_q); extract($rows); 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. Quote Link to comment Share on other sites More sharing options...
Skewled Posted January 11, 2014 Share Posted January 11, 2014 Add or die() to the 2nd query and see what MySQL states. Quote Link to comment Share on other sites More sharing options...
Barand Posted January 11, 2014 Share Posted January 11, 2014 Several points worth mentioning with that script. You only want the news_id in that first query so "SELECT news_id FROM ....". Don't use "SELECT * ", select only what you need . A query returning no rows is not an error. Don't run queries inside loops - use a JOIN to retrieve the data with a single query. 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 Quote Link to comment Share on other sites More sharing options...
Barand Posted January 11, 2014 Share Posted January 11, 2014 Amendment to above ^^ Change fetch_assoc() to fetch_row() Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.