2014-08-30 62 views
1

我使用XAMPP与PHP和PHPMyAdmin作为接口,试图从一个数据库,onlineportal查询信息,并在一个操作中插入另一个数据库androidchatterdatabase。鉴于下面的代码,它不允许从$dbOnlinePortal查询信息,就好像我将查询语句从$dbOnlinePortal->query()更改为$db->query(),它显示结果,但在从onlineportal.types和onlineportal.campaigns中选择时返回'0'。从Android应用程序访问MySQL中的另一个数据库

我可以在PHP索引文件中启动另一个new MySQL实例,因此可以同时连接两个数据库吗?还是有更好的方式来访问一台服务器上的多个数据库比我在做什么?

$dbHost = "localhost"; 
$dbUsername = "root"; 
$dbPassword = ""; 
$dbName = "androidchatterdatabase"; 
$dbName2 = "onlineportal"; 

$db = new MySQL($dbHost,$dbUsername,$dbPassword,$dbName); 
$dbOnlinePortal = new MySQL($dbHost,$dbUsername,$dbPassword,$dbName2); 


case "clickSpecial": 
     if ($userId = authenticateUser($db, $username, $password)) 
     { 
      if (isset($_REQUEST['specialId'])) 
      { 
       // Get data 
       $specialId = $_REQUEST['specialId'];  
       $timePeriod = $_REQUEST['timePeriod']; 
       $cityLocationId = $_REQUEST['cityLocationId']; 

       // Get the restaruant id, which will then serve to get the 
       // selected restaurant categories 
       $sqlRestaurantCategorization = 
         "SELECT distinct category_id 
         FROM onlineportal.types 
         WHERE user_id IN (SELECT DISTINCT user_id FROM onlineportal.campaigns WHERE id = '".$specialId."');"; 

       // Get the categeories of the restraurant  
       if($getRestaurantCategorization = $dbOnlinePortal->query($sqlRestaurantCategorization)) 
       { 
        // Insert those into the table 
        while($rowRestaurantCategorization = $dbOnlinePortal-> fetchObject($getRestaurantCategorization)) 
        { 
         $sql22 = "INSERT INTO `users_click` (`usersId`, `restaurantCategoryId`, `timePeriod`, `cityLocationId`, `clickedDt`) 
               VALUES('".$userId."','".$rowRestaurantCategorization->restaurantCategoryId."','".$timePeriod."','".$cityLocationId."',NOW());";     

         error_log("$sql22", 3 , "error_log"); 
         if ($db->query($sql22)) 
         { 
           $out = SUCCESSFUL; 
         }    
         else 
         { 
           $out = FAILED; 
         }     

        } 

       } 
       else 
       { 
        $out = FAILED; 
       }  

      } 
      else 
      { 
       $out = FAILED; 
      } 

     } 
     else 
     { 
      $out = FAILED; 
     } 

    break; 
+0

所以你说'$ dbOnlinePortal->查询($ sqlRestaurantCategorization)'的结果是'0'?拥有两个'新MySQL'就不会成为问题。 – 2014-08-30 04:08:12

+0

是的,当使用$ db从online.types拉的结果,然后插入user_clicks为“0” – Sauron 2014-08-30 05:44:04

回答

1

的语法插入到另一个数据库使用插入语句是这样的:

USE `old_database`; 
INSERT INTO `new_database`.`new_table`(`column1`,`column2`,`column3`) 
SELECT `old_table`.`column2`, `old_table`.`column7`, `old_table`.`column5` 
FROM `old_table` 

编辑:

你尝试做这样的:

INSERT INTO `users_click` (`usersId`, `restaurantCategoryId`, `timePeriod`, `cityLocationId`, `clickedDt`) 
        VALUES ('".$userId."','(SELECT distinct category_id 
              FROM onlineportal.types 
              WHERE user_id IN (SELECT DISTINCT user_id FROM onlineportal.campaigns WHERE id = '".$specialId."'))','".$cityLocationId."',NOW());"; 
+0

不,因为我发布的代码,我需要从一个数据库中选择,并使用该结果作为插入到另一个数据库的一部分。如何从案例服务器调用访问另一个数据库? – Sauron 2014-08-30 05:42:22

+0

我想我必须看到一些数据才能完成此操作 – MB34 2014-08-30 13:15:16

+0

代码位于'$ sql22'的插入语句中 – Sauron 2014-08-30 15:18:38

0

只是一个细节:如果您使用WAMP或MAMP,即本地服务器,则打开2或更多的数据库将工作。但是,在生产模式下,您必须检查是否可能,因为它取决于您的网站提供商。 在许多情况下,当您有两个不同的帐户时,每个帐户都有一个数据库,出于安全原因,您将无法从第一个帐户的脚本打开第二个数据库(第二个帐户)。如果这两个数据库是两个完全不同的web服务,这肯定是不可能的。

因此,在编码之前进行一个小测试,以避免以后发现问题。

如果您无法从其他帐户访问一个数据库,唯一安全的方法是将脚本放在帐户2上,您将从帐户1中的脚本拨打电话。从脚本1接收到“订单” ,脚本2将查询DB-2,准备答案,将其发送回脚本1,脚本1将对其进行解码并将其用于DB1查询。

+0

我打算将所有内容保留在同一个帐户上,即使在生产模式下也是如此。但是,我将拥有多个数据库,但我绝不会拥有多个帐户,因为数据需要分离,但仍可从Android应用程序和Rails应用程序访问。 – Sauron 2014-09-02 15:33:17

相关问题