risestar Posted February 2, 2012 Share Posted February 2, 2012 I am having some problems getting a query correct. Basically I have two tables, one with listings and another with categories. In the listings table I have a column called shortdescription. I am trying to pull the shortdescription from the listings table with the query $shortdesc = array_shift(mysql_fetch_row(mysql_query("select shortdescription from links where category = '".$scat->id."' "))); The shortdecription display properly on the pages display the listings, however I am getting the following error on any pages that only display the parent categories Warning: array_shift() [function.array-shift]: The argument should be an array in /home/...path/file.php on line 1462 The listings id numbers begin at 75+ because the initial parent category id ends at 74. The query seems to be searching for listing ids below 75 and spitting out an error because it is not finding them. Any ideas on how to eliminate this error and/or stop the query from looking for non-existant data? Quote Link to comment https://forums.phpfreaks.com/topic/256240-warning-array_shift-functionarray-shift-the-argument-should-be-an-array-i/ Share on other sites More sharing options...
trq Posted February 2, 2012 Share Posted February 2, 2012 Chaining your function calls together like this is a terrible idea. You have absolutely no error handling in place. One of these method calls could (and has) failed but because they all feed into each other like that you have mo way of checking to find out where the error is. Quote Link to comment https://forums.phpfreaks.com/topic/256240-warning-array_shift-functionarray-shift-the-argument-should-be-an-array-i/#findComment-1313619 Share on other sites More sharing options...
risestar Posted February 2, 2012 Author Share Posted February 2, 2012 As recommended on another forums i tried $sql="select shortdescription from links where category = '".$scat->id."' "; $result=mysql_query($sql) or die(mysql_error()); $row=mysql_fetch_row($result); $shortdesc = array_shift($row); but it did not make a difference, same error. Quote Link to comment https://forums.phpfreaks.com/topic/256240-warning-array_shift-functionarray-shift-the-argument-should-be-an-array-i/#findComment-1313687 Share on other sites More sharing options...
AyKay47 Posted February 2, 2012 Share Posted February 2, 2012 perhaps your query is not returning any rows in its results set. Add a little more error handling. $sql="select shortdescription from links where category = '".$scat->id."'"; $result=mysql_query($sql) or die(mysql_error()); $row=mysql_fetch_row($result); if(!$row) { echo "no rows grabbed"; } else { $shortdesc = array_shift($row); } Quote Link to comment https://forums.phpfreaks.com/topic/256240-warning-array_shift-functionarray-shift-the-argument-should-be-an-array-i/#findComment-1313689 Share on other sites More sharing options...
risestar Posted February 2, 2012 Author Share Posted February 2, 2012 http://www.quadcorral.com/placestoride/directory/United_States/United_States.html is the page Yes, that is the problem, it is grabbing rows for the individual listings, but the main categories, which have no shortdescriptions, of course have no rows to grab. So I need to either refine a way to grab only rows from the individual listings or a way to filter out the main and subcategories As you can see, its looking for rows in the main categories, which there are none, but if you click on a category, you will see the individual listings which have rows Quote Link to comment https://forums.phpfreaks.com/topic/256240-warning-array_shift-functionarray-shift-the-argument-should-be-an-array-i/#findComment-1313769 Share on other sites More sharing options...
ManiacDan Posted February 2, 2012 Share Posted February 2, 2012 Ok, so do you see what AK is saying? You have to break your code into multiple lines so you can say "if there were no rows returned, then don't try to display a row that doesn't exist." Quote Link to comment https://forums.phpfreaks.com/topic/256240-warning-array_shift-functionarray-shift-the-argument-should-be-an-array-i/#findComment-1313778 Share on other sites More sharing options...
risestar Posted February 2, 2012 Author Share Posted February 2, 2012 I think so. Are you talking about something like a mysql_num_rows? Quote Link to comment https://forums.phpfreaks.com/topic/256240-warning-array_shift-functionarray-shift-the-argument-should-be-an-array-i/#findComment-1313789 Share on other sites More sharing options...
AyKay47 Posted February 2, 2012 Share Posted February 2, 2012 if you are expecting rows returned from your query but are not getting them, something is wrong with your query. Review the query, figure out why it is not grabbing any rows, then redefine the query to the correct one. Quote Link to comment https://forums.phpfreaks.com/topic/256240-warning-array_shift-functionarray-shift-the-argument-should-be-an-array-i/#findComment-1313796 Share on other sites More sharing options...
risestar Posted February 2, 2012 Author Share Posted February 2, 2012 No the problem is that it's trying to pull rows when there aren't any Quote Link to comment https://forums.phpfreaks.com/topic/256240-warning-array_shift-functionarray-shift-the-argument-should-be-an-array-i/#findComment-1313799 Share on other sites More sharing options...
digibucc Posted February 2, 2012 Share Posted February 2, 2012 are there not any because it's not performing the query properly, or are there actually no rows to be pulled? either way, they are still right. you need error handling to move forward. something to say "if row is empty, don't grab it" Quote Link to comment https://forums.phpfreaks.com/topic/256240-warning-array_shift-functionarray-shift-the-argument-should-be-an-array-i/#findComment-1313804 Share on other sites More sharing options...
ManiacDan Posted February 2, 2012 Share Posted February 2, 2012 There are two possible problems: 1) There SHOULD be a row, and there isn't one: Fix your query. 2) There shouldn't be a row. Your code expects there to be a row all the time, but now there's a situation in which not having a row is legitimate. Fix your code the way you've already been shown. Quote Link to comment https://forums.phpfreaks.com/topic/256240-warning-array_shift-functionarray-shift-the-argument-should-be-an-array-i/#findComment-1313809 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.