SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Addict
    Join Date
    May 2004
    Location
    USA
    Posts
    342
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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.

    PHP Code:

    /// 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';


  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,501
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Code PHP:
    $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:
    Code PHP:
    $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);

  3. #3
    SitePoint Addict
    Join Date
    May 2004
    Location
    USA
    Posts
    342
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by guido2004 View Post
    Code PHP:
    $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:
    Code PHP:
    $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.

  4. #4
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,501
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by stma View Post
    Just prints out the "Error:" text - and nothing further.
    Did you try the changes I made? You can't get just "Error:" with my code.

  5. #5
    SitePoint Addict
    Join Date
    May 2004
    Location
    USA
    Posts
    342
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  6. #6
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,501
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    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().

  7. #7
    SitePoint Addict
    Join Date
    May 2004
    Location
    USA
    Posts
    342
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by guido2004 View Post
    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.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •