I don't know why but I'm suddenly getting killed with undefined variable errors. I'm using php5 and yes, I know that mysqli is better table manners but mysql is still supposed to work. Can you see any reason why I would be getting an undefined variable for mysql_insert_id in the below? The information is being put into the database, and the first field is "bid int(11) auto increment primary key" and it's being set.

I'd rather use a mysql_insert_id to get the newly created book id (bid) so that I can make a link to a page (book.php?bid=$bid) where the user can enter information about this book using the book id (bid), rather than perform a query to get it.

require_once('dbconnect.php');

$category_id = "1";
$title = "The Joys of Code Writing";

$query = "INSERT INTO books (category_id, title)
VALUES ('$category_id', '$title')";
$result = mysql_query ($query) or die(mysql_error());
if (mysql_affected_rows() == 1)
{
	$bid = @$mysql_insert_id();

echo "<p><a href='book.php?bid=$bid'>Enter Book Info</a></p>";
}
else
{
	echo"<p>Could not enter information.</p>";
}

    Byecause [font=monospace]$mysql_insert_id[/font] starts with a dollar sign...

      Right - without the dollar sign. $bid = @mysql_insert_id(); OR $bid = mysql_insert_id(); without the error suppression operator. You get so involved looking for the BIG mistakes you often look over the simple ones.

      Thanks

        The error suppression operator should be used only very rarely; it's not just a way of ignoring errors.

        David P wrote:

        I know that mysqli is better table manners but mysql is still supposed to work

        Yes, to give people time to switch to the replacement before the old extension is removed.

          ya know, suppressing an error means that you intend to handle that error yourself, not that you're going to ignore it.

          (Just like knowing that ext/mysql is outdated means you shouldn't be writing any new code with it.)

          Maybe something like

          ($bid = @mysql_insert_id()) || ($bid = 0);

          Anyway, have you actually found a case where this is needed (where it generates an error that needs to be suppressed)? You're already checking that [font=monospace]mysql_affected_rows() == 1[/font] (BTW, I'd recommend using [font=monospace]===[/font] instead), so [font=monospace]mysql_insert_id()[/font] should be able to return a value without any problems...?

            traq wrote:

            so mysql_insert_id() should be able to return a value without any problems...?

            ...and that will only trigger a warning if the connection is invalid - so how did either of the two previous database commands work without any problem?

            The only uses I've found for @ have been around the stream-handling functions, when I don't have any assurances about whether files exist or are readable/writable or are not inclined to suddenly vanish.

              The undefined variable problem I was having was due to my having inadvertently put a dollar sign in front of mysql_insert_id --> $bid = @$mysql_insert_id(); The script worked fine up to that point then stopped. I do have an error reporting system that emails me when an error occurs.

              Yes, I have been reading up on the new extension(i) and I will be using it for new scripts.

                You're missing the point. You only use the "@" operator to suppress error messages if you have a specific need to do so (for one thing, you shouldn't be displaying error messages to the user at all, so why would you need to be suppressing them?)

                What is your specific need to suppress possible error messages in that specific place?

                  6 days later

                  I'm sure I was following a tutorial that used it and I left it in without giving it a thought even though I later added an error reporting script that emails me errors messages should they occur. Error messages aren't shown to the public. I turned it off in the php.ini file.

                  I ordered a book about Php 5 and MySQL 5 that details mysqli. It should be here in a couple weeks but until then I have to try and keep the websites I have running. I have a fair number of websites and several are on the same server and my hosting company updated MySQL and Php so I have to take them one at a time.

                  In fact, I've just come across another undefined variable:

                  Use of undefined constant num - assumed 'num'

                  $query = "SELECT COUNT(id) as num FROM directory WHERE cat_id='$cat_id'";
                  $results = mysql_fetch_array(mysql_query($query));
                  $total_pages = $results[num];

                    Oops, just caught it - $total_pages = $results['num']; - left the single quotes out.

                      David P wrote:

                      I'm sure I was following a tutorial that used it and I left it in without giving it a thought

                      That's the first mistake right there. If you follow a tutorial without thinking, then you're doing it wrong.

                        You're right of course, but when you're new to it you tend to just follow along with it and if what you're wanting to do works you move on. It's only when it stops working do you come back to it and try to figure out why and make changes. However, when I learn something new I'll go back and update things, as I'm going to be doing with mysqli.

                          David P wrote:

                          but when you're new to it you tend to just follow along with it and if what you're wanting to do works you move on.

                          You might. I guess the education system I was raised in taught me differently, including skills like research and critical thinking.

                            Write a Reply...