我使用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