PDA

View Full Version : Need help interpreting a mySQL function please



kuau
07-22-2008, 06:55 PM
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...


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 :)

Nile
07-22-2008, 09:38 PM
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?

kuau
07-22-2008, 09:51 PM
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:


$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 :)

Nile
07-22-2008, 10:05 PM
Ok, I see. But you should only use the @ when you're putting it as a condition in an if statement.

kuau
07-22-2008, 10:42 PM
Why? What does @ mean?

thetestingsite
07-22-2008, 10:45 PM
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:



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

kuau
07-22-2008, 10:54 PM
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 :)

thetestingsite
07-22-2008, 10:58 PM
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.

kuau
07-22-2008, 11:07 PM
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 :)

Nile
07-22-2008, 11:26 PM
I'm happy to help you Kuau. :)

kuau
07-22-2008, 11:28 PM
Thanks... I need a lot of help haha :)

Nile
07-23-2008, 01:30 PM
Wait, can I ask you one more question... I'll ask you anyways.
You said that you saw the @ on a site, which site exactly?

kuau
07-24-2008, 10:33 PM
I'll tell you privately, if you tell me why you want to know. I didn't see it "on a site." I saw it in the code for a site I was working on. I have no desire to insult or embarrass anyone. I got stuck cleaning up the mess this guy made so he is not my favorite person and that's why I referred to it as sloppy code.

I just dug out one of those files at random and sure enough that "@" sign is on every single query. Here is a chunk of the code and in 15 lines he used it 3 times. I had never laid eyes on php before so had no reason to question what he did. This is how I learned the little I know about php - by cleaning up his code. It's a case of the blind leading the blind, I'm afraid.


$sql = "SELECT MAX(Cust_No) FROM $table_cus";
$result1 = @mysql_query($sql,$connection) or die("Couldn't execute $sql query.");
$Cust_Num = mysql_result($result1, 0, 0)+1;

// Get Order Number

$sql = "SELECT MAX(Order_No) FROM $table_ord";
$result2 = @mysql_query($sql,$connection) or die("Couldn't execute $sql query.");
$Order_Num = mysql_result($result2, 0, 0)+1;

// Count Activities in Cart

$sql = "SELECT COUNT(Cust_ID) FROM $table_car WHERE Cust_ID = '$Cust_ID'";
$result = @mysql_query($sql,$connection) or die("Couldn't execute $sql query.");
$Max_Item = mysql_result($result, 0, 0);

Nile
07-24-2008, 10:42 PM
Ok, I was just wondering.