Mysql query - dieing with no error message

I’m going a little nuts, I’ve got the following chunk of code that keeps kicking up errors with out actually telling me what’s wrong.

I’ve commented most of it… but it’s the $result query that’s a problem and the then if(!result) part seems unreliable.

Love some suggestions.

	

/// working fine.........
$insert = mysql_query("INSERT INTO articles (id, title, body) VALUES ('','$_POST[title]','$_POST[body]')")or die( 'Error: ' . mysql_error());
$taggit1 = mysql_insert_id();


$tags  = $_POST['tags'];
$pieces = explode(",", $tags);
foreach ($pieces as $l){
$l = trim($l);  
/// echo's out - so I have a value.
echo $l;

//////// this query dies and kicks up no error  ////////

$result = mysql_query("SELECT id FROM tags WHERE tag = '$l'");
$row = mysql_fetch_array($result)or die( 'Error: ' . mysql_error());

///if there is a result here I'm good.
$taggit2 = $row[0];

//// there isn't always anything in the database so sometimes we have no result. ////

if (!$result) {
$insert2 = mysql_query("INSERT INTO tags (id, tag) VALUES ('','$l')")or die( 'Error: ' . mysql_error());
$taggit2 = mysql_insert_id();
echo 'there was no result - so I inserted a new tag';
}



$result = mysql_query("SELECT id FROM tags WHERE tag = '$l'");
$row = mysql_fetch_array($result)or die( 'Error: ' . mysql_error());

Put the ‘or die’ part on the first line, and to have even more info show the actual query in the error message as well:


$query = "SELECT id FROM tags WHERE tag = '$l'";
$result = mysql_query($query) or die( "Error: " . mysql_error() . " in query $query");
$row = mysql_fetch_array($result);

I wish it gave me more information. Just prints out the “Error:” text - and nothing further. Sorry I didn’t include that in my example here, I’d stripped it out because I’d been playing around with it.

Did you try the changes I made? You can’t get just “Error:” with my code.

Thank you - I did get it working. I’m tired, can’t believe I was looking at that the whole time and missed it.

I’m having an issue with the if there is no result part: (!result)

I think there must be a value there even when there is nothing to get… but I’ll deal with that in the morning after some sleep. Thanks.

A couple of considerations:

  1. Is the value of ‘tag’ in the tags table unique? If so, why don’t you get rid of that id column and make tag the primary key?

  2. $result is false only when the query ends with an error. To see if anything has been selected, use mysql_num_rows().

  3. If you want to check if the tag already exists to avoid inserting duplicate tags, see consideration 1, or create a unique index on ‘tag’. That way you can skip the select, and do an insert right away (which may fail with a duplicate key error, but you can manage the error displaying in a different way (not using ‘or die’) and ignore the duplicate key error.

  4. $_POST[‘tags’] is user input. So before using it’s content in a mysql_query, you must sanitize it. It’s a string, so put it through mysql_real_escape_string().

Thanks for the advice - it’s all good, but this is a closed system for my use only so I took some shortcuts.