gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 00:10:08 25 4
gpt4 key购买 nike

我将 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com