dpw30157 2011-08-02 21:24
浏览 33
已采纳

php和mysql的奇怪问题。 一切似乎都正常,没有选择行?

im having a pretty weird problem with php and mysql. Im 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, im 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 im 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)

Ive tried using the xampp server, and also a configured-by-myself server, and got the same behavior. Ive checked every log Ive 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 doesnt show anything, neither the site specific log. Please, any clue trying to decipher this would be very apreciated!!! thanks!!! =)

EDIT:

I found the problem. It has something to do with aefxx's answer.

The program im 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 dont understand why it does not create two separate connections... any suggestions???

thank you very much for your help!!! =)

EDIT:

I cant self answer yet, but this is the solution to my problem. Thanks anyways to those who took the time of reading (and some answering) this!

As seen in the documentation of php mysql_connect() function, it receives an optional parameter called new_link, which is false by default.

new_link
If a second call is made to mysql_connect() with the same arguments, no new link will be established, but instead, the link identifier of the already opened link will be returned. The new_link parameter modifies this behavior and makes mysql_connect() always open a new link, even if mysql_connect() was called before with the same parameters. In SQL safe mode, this parameter is ignored.

so, i just needed to add a true as an additional last parameter to the call to mysql_connect() in both connection classes. SOLVED =)

  • 写回答

2条回答 默认 最新

  • 普通网友 2011-08-02 21:31
    关注

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

    Example:

    mysql_select_db("mydatabase", $link);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况