在一个PHP脚本中使用多个数据库

时间:2015-09-09 13:27:35

标签: php mysql database

这第一部分在我的其他剧本中运作得非常好。但在此示例中,脚本只是跳过此部分并转到结尾并执行最后一个查询..

我不知道我做错了什么,因为我做的完全一样,但它不起作用。这是完整的脚本。

for ($i = 1; $i <= $numofservers; $i++)
{   
     if($servers[$i][1] == $server)
     {
         $srv = $i;
         $connect = mysql_connect($servers[$srv][3], $servers[$srv][4], $servers[$srv][5]) or die('Could not connect: ' . mysql_error());
         mysql_select_db($servers[$srv][6]);
     }
}

$sql = mysql_query("SELECT * FROM db_users WHERE username = '".$nick."'") or die('Nick na serveru Neexistuje!' . mysql_error());
$sql = mysql_fetch_array($sql);            
$balance = $sql['donate_balance'];
$steamid = $sql['steam_id'];
$balance = $balance + $price_sms;
$money = mysql_query("UPDATE db_users SET `donate_balance`='". $balance ."' WHERE steam_id = '".$steam_id."'");


<?php
   $db_site_name = ''; // db connection name
   $db_site_password = '';
   $db_site_username = 'root'; // username of connection
   $db_site_ip = '127.0.0.1:3306';
   $link = mysql_connect($db_site_ip, $db_site_username, $db_site_password) or die('Could not connect: ' . mysql_error());
   mysql_select_db($db_site_name) or die('Not connected<br>');

   $serversquery = mysql_query("SELECT * FROM db_servers ORDER BY id");
   $numofservers = 0;

   while ($servers_array = mysql_fetch_array($serversquery))
   {
      $numofservers++;
      $servers[$numofservers] = array(
         $servers_array['id'],
         $servers_array['name'],
         $servers_array['about'],
         $servers_array['server_ip'],
         $servers_array['server_username'],
         $servers_array['server_password'],
         $servers_array['server_name'],
         $servers_array['server_admin_rank'],
         $servers_array['donate_factor']
    );
   }

   if (isset($_GET["request"])){
      $request_sms = ($_GET["request"]);
      $status = ($_GET["status"]);
      $message = ($_GET["message"]);
      $request = mysql_query("UPDATE `sms_confirm` SET `status` = '".$status."' WHERE `sms_id` = '".$request_sms."'");
      $findid = mysql_query("SELECT * FROM `sms_confirm` WHERE `sms_id` = '".$request_sms."' LIMIT 1") or die(mysql_error()); ;
      $query = mysql_fetch_array($findid);   
      if ($query['status'] == 'DELIVERED' AND $query['added'] == 'false'){
         $price_sms = $query['cena'];
         $nick = $query['nick'];
         $server = $query['server'];

         for ($i = 1; $i <= $numofservers; $i++)
         {   
         if($servers[$i][1] == $server)
         {
         $srv = $i;
         $connect = mysql_connect($servers[$srv][3], $servers[$srv][4], $servers[$srv][5]) or die('Could not connect: ' . mysql_error());
         mysql_select_db($servers[$srv][6]);
         }
         }
         $sql = mysql_query("SELECT * FROM db_users WHERE username = '".$nick."'") or die('Nick na serveru Neexistuje!' . mysql_error());
         $sql = mysql_fetch_array($sql);            
         $balance = $sql['donate_balance'];
         $steamid = $sql['steam_id'];
         $balance = $balance + $price_sms;
         $money = mysql_query("UPDATE db_users SET `donate_balance`='". $balance ."' WHERE steam_id = '".$steam_id."'");

         $link = mysql_connect($db_site_ip, $db_site_username, $db_site_password) or die('Could not connect: ' . mysql_error());
         mysql_select_db($db_site_name) or die('Not connected<br>');

         //this executes fine
         $request = mysql_query("UPDATE `sms_confirm` SET `added` = 'true' WHERE `sms_id` = '".$request_sms."'");         

      }
      }   
      //Header("HTTP/1.1 204 NO_CONTENT");
      //Header("Content-type: text/plain");
   ?>

1 个答案:

答案 0 :(得分:1)

首先,不要使用mysql_*函数。它已被弃用,将被删除。

如果您想使用多个数据库连接,则必须将链接传递给与您的数据库联系的每个函数。

例如,检查here(第二个参数)。

示例代码

$link = mysql_connect($db_site_ip, $db_site_username, $db_site_password);
mysql_select_db($db_site_name, $link);

mysql_query($query, $link);