开发者

No rows selected when everything seems to be normal?

I'm having a pretty weird problem with PHP and MySQL. I'm doing the following in my PHP script:

$query1 = "SELECT * FROM ".$src;
$ans1 = mysql_query($query1,$vitoquenId);
echo "<p>Query: \"".$query1."\"<br/>Error: \"".mysql_error()."\"<br/>Total rows extracted: ".mysql_num_rows($ans1)."</p>";

When doing this, this is the output:

Query: "SELECT * FROM Almacen"
Error: ""
Total rows extracted: 0

The thing is, I'm not supposed to get 0 rows, and the weird thing is that no error is reported. I granted all the permissions for my user, and even tried connecting to the database as root user, but got the same behavior.

When doing the query directly in the MySQL command line I'm getting the following:

mysql> SELECT * FROM Almacen;
+----+-------------+------------+--------+
| id | nombre      | tipo       | status |
+----+-------------+------------+--------+
|  1 | Tienda      | Principal  | Activo |
|  2 | Dep�sito    | Secundario | Activo |
|  3 | Chaguaramos | Secundario | Activo |
+----+-------------+------------+--------+
3 rows in set (0.00 sec)

I've tried using the XAMPP server, and also a configured-by-myself server, and got the same behavior. I've checked every log I've thought of, configured MySQL to log every query and nothing abnormal seems to happen.

The MySQL log shows the following:

110802 16:21:49       101 Connect   victor@localhost on 
                      101 Init DB   saw
                      101 Init DB   sawprueba
                      101 Query SELECT * FROM Almacen
                      101 Quit

The Apache log doesn't show anything, neither the site specific log.

The program I'm writing is a migration tool. It extracts info from one database, processes it, and inserts it into another db. To do so, I need to connect to two databases. I make the connection through two classes using the singleton pattern as suggested in the PHP documentation. You can see the connection file in the github repo.

To make the connection to the db, I call this class like this:

$vitoquen = Vitoquen::singleton();
$newVitoquen = NewVitoquen::singleton();
$vitoquenId = $vitoquen->getId();
$newVitoquenId = $newVitoquen->getId();

What is happening is that for some reason, it does not create two separate connections, but instead create just one connection, and use the second db, the new one, which is empty. I got the clue when I saw the MySQL log:

110802 16:21:49       101 Connect   victor@localhost on 
                      101 Init DB   saw
                      101 Init DB   sawprueba
                      101 Query SELECT * FROM Almacen
                      101 Quit

It shows 101 Init DB twice; one for saw and other for sawprueba. Now I don't understand why it does not create two separate connec开发者_高级运维tions...


Did you select the proper database (also called the scheme) when connecting to your DBMS?

Example:

mysql_select_db("mydatabase", $link);


Let's add some minimal error handling and an extra query to see what mysql "thinks" about the table in question.

$query1 = "SHOW TABLE STATUS LIKE '$src'";
echo '<pre>Debug: query1=', $query1, "</pre>\n";
$ans1 = mysql_query($query1, $vitoquenId)
    or die(mysql_error());
while ( false!==($row=mysql_fetch_assoc($ans1)) ) {
    foreach( $row as $key=>$value ) {
        echo $key,'="', $value, '" ';
    }
    echo "<br />\n";
}

$query1 = "SELECT * FROM ".$src;
echo '<pre>Debug: query1=', $query1, "</pre>\n";
$ans1 = mysql_query($query1,$vitoquenId)
    or die(mysql_error());
echo "<p>Query: \"".$query1."\"<br/>Error: \"".mysql_error()."\"<br/>Total rows extracted: ".mysql_num_rows($ans1)."</p>";


For a Connection to local database :

     $dbhost = 'localhost';
     $dbuser = 'root';
     $dbpass = '';
     $connection = mysql_connect($dbhost, $dbuser, $dbpass);
     mysql_select_db("mydatabase", $connection);
     $query1 = "SELECT * FROM ".$src;
     $ans1 = mysql_query($query1,$vitoquenId);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜