php - mysql中的链接资源不稳定

标签 php mysql sql

同一个mysql资源可以用于多个查询吗?

 $queries=array($q1, $q2);
 $link=mysql_connect(...);
 mysql_select_db(...);

 for($i = 0; $i < count($queries); $i++) {

      echo "Link is of type " . gettype($link) . "<br />";

      if(is_resource($link)) {
           mysql_query($queries[$i], $link);
      } else {
           echo "Did not execute query. <br/>";
      }
 }

 disconnect($link);

输出:

link is of type resource
Successfully executed query
link is of type resource
Did not execute query.

编辑:我想从输出中指出第一个查询执行但第二个查询不执行。在这两者之间,链接没有变化。查询的执行方式也无关紧要(恕我直言)。

编辑 2:我的确切代码如下:

$link = mysql_connect("localhost", ........);
mysql_select_db(....);

for ($row = 0; $row < $numRows; $row++) {
    if (($query = buildQuery($mainArr, $rowArr, $row)) === null) {
        echo "Error - could not build query. <br />";
        return;
    }

    echo "Link is of type " . gettype($link) . "<br />"; 

    if (is_resource($link)) {
        if ((mysql_query($query, $link))) {
            if($debug) {
                echo "Successfully executed query <br />";
            }
        } else {
            if ($debug) {
                echo "This comes up when row is " . $row . "<br />";
                echo "Link is of type " . gettype($link) . "<br />";
                echo "Datawrite failed - " . $query . "<br />";
                echo "the official line is " . mysql_error($link) . "<br />";
            }
        }
    } else {
        echo "Did not execute query <br />";
    }
}

disconnect($link);

最佳答案

for 循环的每次迭代都包含对 buildQuery() 的调用(此处未显示)。它会以某种方式影响 $link,还是会改变 $debug 的值?

此外,您的循环引用了此处未定义的 $numRows。循环执行了多少次?

关于php - mysql中的链接资源不稳定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6510435/

相关文章:

php - 如何从 JSON 在 Eloquent ORM 中保存具有相关记录的模型

mysql - 根据最大订单从 2 个不同的表中选择数据

MySQL 根据匹配日期将多行合并为一行

php - 在左连接上显示 AVG

php - Symfony 的可空自定义表单实体

php - .htaccess 使文件夹充当根目录?

SQL Server 表转 json

PHP 回显空白日期时间字段

mysql连接三个表的case语句

sql - 在不同列中显示重复的项目/id