This is a discussion on how does mysql_errno work? within the PHP Language forums, part of the PHP Programming Forums category; I'm having trouble capturing the error messages from my code which deals with MySql. In the constructor, I was ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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 variable: ---------------------------- function McDatastoreConnectorMySql() { $controllerForAll = & getController(); $this->resultsObject = & $controllerForAll->getObject("McResults", "McDatastoreConnectorMySql"); $this->notifyObject = & $controllerForAll->getObject("SingletonNotify", "McDatastoreConnectorMySql"); if (is_object($this->resultsObject)) { if (is_object($this->notifyObject)) { $config = getConfig(); if (is_array($config)) { extract($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->notifyObject->notify("McDatastoreConnector", "databaseQuerySuccess"); $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"); $this->notifyObject->notify("McDatastoreConnector", "databaseQueryError"); $this->error(); } } } else { ------------------------- |
|
|||
|
lkrubner@geocities.com wrote:
> 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? > 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. JW |
|
|||
|
Alright, I looked here:
http://www.php.net/mysql_errno 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 /home/httpd/vhosts/publicdomainsoftware.org/httpdocs/ppExtras/McDatastoreConnectorMySql.php on line 164 |
|
|||
|
.oO(lkrubner@geocities.com)
>Alright, I looked here: > >http://www.php.net/mysql_errno > >and clearly the identifier is optional, so I'll just leave it out. Why >would anyone ever need it? You need it if you run multiple connections to database servers simultaneously. Micha |
|
|||
|
>http://www.php.net/mysql_errno
> >and clearly the identifier is optional, so I'll just leave it out. Why >would anyone ever need 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 |