php - 从 Android 应用程序访问 MySQL 中的另一个数据库

标签 php android mysql multiple-databases

我将 XAMPP 与 PHP 和 PHPMyAdmin 一起用作界面,并尝试从一个数据库、在线门户查询信息,并在一个操作中插入另一个数据库、androidchatterdatabase。鉴于下面的代码,它不允许从 $dbOnlinePortal 查询信息,就好像我将查询语句从 $dbOnlinePortal->query() 更改为 $db->query(),它显示结果,但在从 onlineportal.types 和 onlineportal.campaigns 中选择时返回“0”。

我可以在 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;

最佳答案

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

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());";

关于php - 从 Android 应用程序访问 MySQL 中的另一个数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25578830/

相关文章:

php - jquery.countdown 日期时间问题

php - 找不到套接字传输 "https"

php - 帮助我的正则表达式

php - codeigniter 事件记录数据库查询

java - 试图将位图发布到 facebook

MySQL 更新时出现 group by 错误

php - 如何修复 PHP amqp?

android - E/SQLiteLog﹕ (1) near "TABLEaccount": syntax error

android - 如何访问 webview 中包含的 html 元素?

php - 数据库不更新行