On Thu, 25 Jan 2001, Jacky@lilst wrote:

> $sql1 = "insert into firsttable (firstname, lastname) values('Jack','Chan')"; 
> $resultsql1 = mysql_query($sql1);
> $sqlLastID = "select LAST_INSERT_ID() from firsttable";
> $resultlast = mysql_query($sqlLastID);
> $FirstLast = mysql_result($resultlast,0,0);

<snip>

First of all, let's rewrite your code.

<?PHP
// I'm assuming these two lines are in your code, just not
// in your post.  Pay special note to the $CONN variable.
$CONN = mysql_connect("host", "user", "pass");
mysql_select_db("somedatabase", $CONN);

// Notice here that I don't care about the result mysql
// sends back.  On inserts, it doesn't matter.  Also
// notice that I'm telling mysql which connection to use.
$sql =
  "INSERT INTO firsttable (firstname,lastname)\n".
  "VALUES ('Jack', 'Chan')";
mysql_query($sql, $CONN);

// Now, we'll get your last insert.  Notice that I changed
// your mysql_result into a mysql_fetch_array?  The code is
// just a tad more scalable that way - in case you want the
// ID and something else if you redo your code.
$sql =
  "SELECT LAST_INSERT_ID() AS nLast\n".
  "  FROM firsttable";
$result = mysql_query($sql, $CONN);
$row = mysql_fetch_array($result);

// ALWAYS clean up after a select.  Especially if you do more than
// one in a page.
mysql_free_result($result);

// Here's your last insert id.
$nLastInsert = $row["nLast"];

?>

It's *very important* that you specify which connection you want mysql to
use!  I can't stress this enough.  If more than one connection gets opened
(you use phplib for instance, it opens its own connection), and if you
don't specify which you're using, you'll get random results, and you'll
interfere with the *other* connection.  Just because PHP says the
connection is optional doesn't mean it always is.  Use it anyway.

I say this because you had a later post complaining about how mysql said
you didn't select a database.  Maybe you didn't.

This goes for everyone here.  Not specifying which connection is in use is
*sloppy* and may lead to bugs you can't track down!  At the last place I
worked, we started using phplib after everything was written, and it
*broke everything* because nobody was paying attention to the connections
they opened.  We had to audit the code for days to track everything down.

Do yourself a favor *right now* and grep -r your codebase for all database
related functions. (grep -ir "mysql_" *) and FIX THEM NOW.  That way,
you'll be safe knowing that it's *your* connection that's in use.

And always remember, mysql_error($CONN) is your friend.

Cheers!

-- 
+-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-+
| Shaun M. Thomas                INN Database Programmer              |
| Phone: (309) 743-0812          Fax  : (309) 743-0830                |
| Email: [EMAIL PROTECTED]    AIM  : trifthen                      |
| Web  : www.townnews.com                                             |
|                                                                     |
|     "Most of our lives are about proving something, either to       |
|     "ourselves or to someone else."                                 |
|                                           -- Anonymous              |
+-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-+



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to