2014-05-24 24 views
-3

我想在php中使用mySql執行多個查詢。但我沒有得到結果。請讓我知道我在代碼中做了什麼錯誤。我是全新的PHP和mySql。這是我下面的PHP代碼:如何在mySql中執行多個查詢

<?php 

//----------------------------------------------------------------------------------- 

$host='localhost'; 
$uname='root'; 
$pwd='welcome'; 
$db='database name'; 

//----------------------------------------------------------------------------------- 

$con=mysql_connect($host,$uname,$pwd) or die("Connection Failed"); 
mysql_select_db($db,$con) or die("database selection failed"); 

//----------------------------------------------------------------------------------- 

$mobile = mysql_real_escape_string($_POST['Mobile']); # Secure the input! 
$password = mysql_real_escape_string($_POST['Password']); 
$time = date("D M d, Y G:i a"); 

//----------------------------------------------------------------------------------- 

// echo $time; 

$flag['code']=0; 
$select="select * from Insert1 where Mobile = '$mobile' AND Password = '$password'"; 
$select . ="update table Insert1 set Time='$time' where Mobile='$mobile',Password='$password',Time is null"; 

$r=mysql_query($select,$con); # This will always return something "true" 

if(mysql_num_rows($r) > 0) { # You want to count rows instead. 
    $flag['code']=1; 
} 
print(json_encode($flag)); 
mysql_close($con); 
?> 
+0

我真的希望那不是你的密碼 –

+0

@RUJordan解決方案plzz ... !!! – user3660303

+0

如何回答沒有具體問題的問題?你不能只是轉儲你的腳本,並說修復它..請閱讀如何提出一個好問題。 –

回答

2

the documentation

的mysql_query()發送一個唯一的查詢(多個查詢不支持),以當前活動與指定的link_identifier關聯的服務器上的數據庫。

mysql_query一次不支持多個查詢,你需要調用它(或者,因爲它已經過時,從PHP,a modern replacement for it被刪除)分別每個查詢

(即使這樣做,您需要用分號分隔字符串中的每個查詢)。

+0

另外,mysql_ *擴展名已被棄用。他應該使用更新的[mysqli_ *](http://www.php.net/manual/en/book.mysqli.php) –

-4

試試這個

$select="select * from Insert1 where Mobile = '$mobile' AND Password = '$password'"; 
$select.=";"; 
$select . ="update table Insert1 set Time='$time' where Mobile='$mobile',Password='$password',Time is null"; 
+4

'嘗試這個'是一個可怕的答案。請解釋你的答案 –