Jump to content

Warning: array_shift() [function.array-shift]: The argument should be an array i


risestar

Recommended Posts

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?

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.

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.

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);
}

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

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." 

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.

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"

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.