Jump to content

Recommended Posts

Hi,

 

I made an attachments feature for a forum software, and each post can have a maximum of one attachment.

 

However, the way I check to see if the post has an attachment isn't completely working just now. Though it works when the post does have an attachment, when the post doesn't have an attachment it returns errors.

 

$sql = "SELECT *
	   FROM attachments
	   WHERE post_id = ". $posts[$i]['post_id'] ."
";
$result = $db->query($sql);
while($row = $db->fetch_assoc($result))
{
	$attachments[0] = $row;
}

 

Each post has a unique ID, and if it has an attachment I check to see if it has a row in the attachments table of the database with the "post_id" column set to the post ID. Not every post has an attachment though, and therefore an according row in the attachments table (which returns an error).

 

How can I stop an error from being returned when it doesn't have an attachment?

 

Thank you.

Link to comment
https://forums.phpfreaks.com/topic/117837-unset-variable-errors/
Share on other sites

By checking how many rows are returned...

 

$sql = "SELECT *
	   FROM attachments
	   WHERE post_id = ". $posts[$i]['post_id'] ."
";
$result = $db->query($sql);
if($db->num_rows($result) > 0){ //of course, add in your num_rows function here.
//Its greater then 0, that means we have an attachment.
while($row = $db->fetch_assoc($result))
{
	$attachments[0] = $row;
}
}

I'm new to OOP projectfear and I'm confused why everyone writes those one liner functions when you could just say

 

$object_var->mysql_num_rows($r);

 

is that not the same thing? or do OOP people have a superiority complex and like to rename the functions to what they want?

Well really, you could expand the num_rows() function that I have up there and make it more then just one line.

 

You can call the function whatever you wish, it's just I personally find it easier to make it a bit smaller. Heck I know people that use the mysql_num_rows function as "nr".

 

@Blade: As I said, you can make it more then one line. ;)

 

Oh and I really am not an OOP pro, I'm just okay at it. :P

so its to give you flexibility should you need to return the integer from a different method?

 

It gives you more flexibility in my opinion.

$result=Db->query("select query here", 1);

You could have Db->query return an already fetched result if you passed '1'.  Then you don't need another line to fetch it. 

Of course you only use that when you only expect 1 row to be selected.  (hope that made sense)

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.

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