Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: Need help interpreting a mySQL function please

  1. #1
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default Need help interpreting a mySQL function please

    I have found this tres cool mySQL function to do exactly what I want, but the examples don't show me clearly how to use it in php. I am inserting an event record and need to add the categories associated with that event to another table at the same time, so I need the event_id (auto-incremented) to add to the 2nd table. So this is what I came up with:

    First I insert the event record and then...

    Code:
    query = "SELECT LAST_INSERT_ID() FROM event ";
    	$result = @mysql_query($query,$connection) or die("Couldn't execute $query query. <br> mysql error: ".mysql_error()); 
    	$row = mysql_fetch_array($result);
    	$neweventid = $row['event_id'];
    Is there a more elegant way to accomplish this? (not even sure if this would work). Thanks. e

  2. #2
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Thats exactly how I would do it, below are a few things.
    • You're putting an at('@') sign before the mysql_query. I'd only put this there if this was a condition of an if statement.
    • Why do you have two variable $querys in the die statement?
    Jeremy | jfein.net

  3. The Following User Says Thank You to Nile For This Useful Post:

    kuau (07-22-2008)

  4. #3
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default

    Dear Nile:

    Thanks! Maybe I am finally starting to get the hang of php (miracle).

    I have no idea what the "@" is for. I'm just a mimmick learning from doing what I see, with your help of course. Is it optional? I guess I should remove it. Does the result differ if it is there or not?

    It is not 2 $query's. If it failed, it would say, "Couldn't execute 'SELECT * FROM table' query. The second is just a word.

    Because I figured the last event_id is not an array and is only one thing, I since changed it to this:

    Code:
    $query = "SELECT LAST_INSERT_ID() FROM event ";
    	$result = mysql_query($query,$connection) or die("Couldn't execute $query query. <br> mysql error: ".mysql_error()); 
    	$neweventid = mysql_result($result,0,0);
    Is this better or worse? Thanks, e
    Last edited by kuau; 07-22-2008 at 09:52 PM. Reason: removed @

  5. #4
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Ok, I see. But you should only use the @ when you're putting it as a condition in an if statement.
    Jeremy | jfein.net

  6. The Following User Says Thank You to Nile For This Useful Post:

    kuau (07-22-2008)

  7. #5
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default

    Why? What does @ mean?

  8. #6
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    The @ symbol is simply an error suppression tool, and it's not the best way to get rid of error messages either. The better way is to use the error_reporting function like so:

    Code:
    <?php
    error_reporting(0);
    
    //rest of code here
    ?>
    If the code is written properly, you shouldn't even need to use this, but there may be times where it may be needed.
    Hope this helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  9. The Following User Says Thank You to thetestingsite For This Useful Post:

    kuau (07-22-2008)

  10. #7
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default

    I had no idea it had anything to do with error messages. It was just in the old code I inherited and it seemed to work, so I didn't question it. I WANT to see the error messages so I can fix things! Can I safely remove it from all my code using a search and replace, or could it break things?

    Thanks, e

  11. #8
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    If there are email addresses (or even anything that is important that has the @ symbol), it would mess those up. If you just removed it from the php code, you should be alright. Keep in mind; however, that you any errors that were suppressed will now pop up (if any).

    Hope this helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  12. The Following User Says Thank You to thetestingsite For This Useful Post:

    kuau (07-22-2008)

  13. #9
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default

    I would do a search on "$result = @mysql_query($query,$connection)" because that is what I consistently used everywhere. Maybe I'll change a few manually and see what happens first. Thanks for the advice. Having witnessed this guy's sloppy coding practices, I can see why he used it everywhere. I would rather have the code correct than sweep junk under the carpet.

    Niles, thanks for noticing that! I would have been doing that forever! e

  14. #10
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    I'm happy to help you Kuau.
    Jeremy | jfein.net

  15. The Following User Says Thank You to Nile For This Useful Post:

    kuau (07-22-2008)

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
  •