Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在阵列中存储MySQL数据库连接_Php_Mysql_Arrays_Object - Fatal编程技术网

Php 在阵列中存储MySQL数据库连接

Php 在阵列中存储MySQL数据库连接,php,mysql,arrays,object,Php,Mysql,Arrays,Object,在查看一些现有代码时,我意识到我一直在打开和关闭远程数据库连接,以获取数百台设备的信息。我现在试图通过将数据库连接存储在一个数组中并在创建之前按名称检查特定连接是否已经存在来解决这个问题 $mysql_connections = array(); $devices = mysql_query("SELECT * FROM devices ORDER BY name ASC", $dp_conn); while($row = mysql_fetch_array($devices)) {

在查看一些现有代码时,我意识到我一直在打开和关闭远程数据库连接,以获取数百台设备的信息。我现在试图通过将数据库连接存储在一个数组中并在创建之前按名称检查特定连接是否已经存在来解决这个问题

$mysql_connections = array();

$devices = mysql_query("SELECT * FROM devices ORDER BY name ASC", $dp_conn);

while($row = mysql_fetch_array($devices))
{
    $whmcs_site = $row['whmcs_site'];
    $whmcs_id = $row['whmcs_id'];

    /* WHMCS Service */
    if ($whmcs_site != "" && $whmcs_id != "")
    {
        $site = get_site_details($whmcs_site, $dp_conn);

        if (in_array($site['name'], $mysql_connections))
        {
            echo "Connection already exists</br>";
            $whmcs = $mysql_connections[$site['name']];
        }
        else
        {
            echo "No connection exists...creating it named" . $site['name'] . " </br>";

            $whmcs = whmcs_connect($site);
            $mysql_connections[$site['name']] = $whmcs;
        }

只有两个可能的MySQL数据库,因此在存储了这两个数据库之后,in_数组检查应该返回true。由于某些原因,它从未找到现有的条目,因此它继续在每次迭代中生成新的连接。有人能指出这是什么问题吗?

您需要检查array\u key\u exists或类似项,而不是在\u array中,因为您正在比较键。

您需要检查array\u key\u exists或类似项,而不是在\u array中,因为您正在比较键。

您需要检查array\u key\u exists或类似项,因为您正在比较密钥,所以不在\u数组中。

您需要检查数组\u密钥是否存在或类似,因为您正在比较密钥,所以不在\u数组中。

您是否进行过任何调试尝试?代码执行与预期有什么不同?为什么您仍在使用不推荐的mysql_*函数?您是否尝试过任何调试?代码执行与预期有什么不同?为什么您仍在使用不推荐的mysql_*函数?您是否尝试过任何调试?代码执行与预期有什么不同?为什么您仍在使用不推荐的mysql_*函数?您是否尝试过任何调试?代码执行与预期有什么不同?为什么您仍然使用不推荐的mysql_*函数?非常感谢。如果允许的话,我会在6分钟内接受。我将不得不回去阅读这些,以记住未来的区别。非常感谢。如果允许的话,我会在6分钟内接受。我将不得不回去阅读这些,以记住未来的区别。非常感谢。如果允许的话,我会在6分钟内接受。我将不得不回去阅读这些,以记住未来的区别。非常感谢。如果允许的话,我会在6分钟内接受。我将不得不回去阅读这些,以记住未来的区别。