PDA

View Full Version : array check to empty I ideas


moos3
03-21-2008, 05:10 PM
not sure if this is the best way to check for nothing returned


$post[] = array();
$q = "SELECT title,body,author_id,id,cat_id FROM posts WHERE slug='$slug'";
$rs = mysql_query($q);
if(mysql_num_rows($rs) == 0){
$post = NULL;
}
else{
while($rw = mysql_fetch_array($rs)){
$post['title'] = $rw['title'];
$post['body'] = $rw['body'];
$post['author'] = $this->getAuthor($rw['author_id']);
$post['post_id'] = $rw['id'];
$post['category'] = $this->getCategory($rw['cat_id']);
}
}
return $post;

Right now i'm checking if mysql_num_rows is equal to zero. if so set $post array to NULL. Is this the best way do this?

Fumigator
03-21-2008, 06:11 PM
Is it even necessary? If there are no rows, the "while" loop never runs so $post remains undefined anyway. If the problem you're trying to solve is $post not being an array, I suggest you check to see if $post is an array after it's been returned using is_array(), or maybe even better, keep the code you have but instead of assigning NULL to $post, make it an empty array with $post = array(); But that may lead to other problems... I would probably just make sure it's an array before trying to use it as an array (such as in a foreach() loop).