how does mysql_errno work?

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I'm having trouble capturing the error messages from my code which
deals with MySql. In the constructor, I was getting my site's
configuration info, and assigning the name of the database to a class

function McDatastoreConnectorMySql() {
$controllerForAll = & getController();
$this->resultsObject = & $controllerForAll->getObject("McResults",
$this->notifyObject = &

if (is_object($this->resultsObject)) {
if (is_object($this->notifyObject)) {
$config = getConfig();
if (is_array($config)) {

$persistent = $db_persistent;
$db = $db_database;
$this->db = $db_database;
$server = $db_server;
$user = $db_user;
$password = $db_password;
$port = $db_port;

I was then using the variable $this->db and giving it to the function
mysql_errno(). Apparently this is the wrong thing to do. I've changed
my code so that now I'm handing it a link? That doesn't quite make
sense to me - if there is a problem with the database connection, then
there will be no link, yes?

if (stristr($query, 'SELECT') || stristr($query, 'SHOW') ||
stristr($query, 'EXPLAIN') || stristr($query, 'DESCRIBE')) {
$this->pp_queryid = @ mysql_query($query, $this->pp_linkid);
if (is_resource($this->pp_queryid)) {
$this->pp_firstquery = 1;
return $this->pp_queryid;
} else {
if (mysql_errno($this->pp_linkid) > 0) {
$errmsg = 'Query error: ' . mysql_error();
$this->resultsObject->error("In query(), in
McDatastoreConnectorMySql, we were not able to run our query.  $errmsg
.. The query was: '$query' .", "McDatastoreConnectorMySql");
            } else {

Re: how does mysql_errno work? wrote:
Quoted text here. Click to load it

Correct. When a link resource is available, you can pass it as an argument
to the mysql_errno(). When there isn't, just skip it.

You can even skip it in most cases, when you have a link resource available.
See the manual for more info.



Re: how does mysql_errno work?

Alright, I looked here:

and clearly the identifier is optional, so I'll just leave it out. Why
would anyone ever need it?

When I was using the database name I got:

Warning: Supplied argument is not a valid MySQL-Link resource in
on line 164

Re: how does mysql_errno work?

Quoted text here. Click to load it

Because some pages use more than one MySQL connection at the
same time, and need to check for errors on the correct connection.

Why use more than one connection?  Several reasons include:

- The purpose of the code is to migrate, merge, or compare data
  from one database to another.
- Databases needed are on different servers, or, for future-proofing,
  may be on different servers in the future.
- No one login provides sufficient privileges to do what is needed.
- Switching databases before each query, or specifying it in the
  query, is a pain.

                    Gordon L. Burditt

Re: how does mysql_errno work?


Quoted text here. Click to load it

You need it if you run multiple connections to database servers


Site Timeline