我使用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;
所以你说'$ dbOnlinePortal->查询($ sqlRestaurantCategorization)'的结果是'0'?拥有两个'新MySQL'就不会成为问题。 – 2014-08-30 04:08:12
是的,当使用$ db从online.types拉的结果,然后插入user_clicks为“0” – Sauron 2014-08-30 05:44:04