2013-04-19 69 views
-3

我已經運行在我的MySQL數據庫的查詢:我怎麼一個MySQL查詢轉換爲一個PHP查詢

select email from main where phno=1234567890 

,並返回這個(http://i.imgur.com/YiVQ4NN.png

因此,這裏是我在PHP的嘗試:

$search_query="select email from main where phno = 5756407850"; 
$to = mysql_query($search_query); 
echo "hello " . mysql_query($search_query);` 

,我的成績在Web瀏覽器是hello Resource id #34

我怎樣才能在電子郵件部分提取什麼?在這種情況下,只是「HGH」字符串?

答:

$search_query="select email from main where phno = 5756407850"; 
$result = mysql_query($search_query); 
$array = mysql_fetch_array($result); 
$to = $array[0]; 
echo "hello " . $to; 
+4

你如何仔細閱讀初學者的精細手冊? http://www.php.net/manual/en/mysql.examples-basic.php也不要錯過http://www.php.net/manual/en/mysqlinfo.api.choosing.php。 – deceze

+3

[**請不要在新代碼中使用'mysql_ *'函數**](http://bit.ly/phpmsql)。他們不再被維護[並且被正式棄用](http://j.mp/XqV7Lp)。看到[**紅框**](http://j.mp/Te9zIL)?學習[*準備的語句*](http://j.mp/T9hLWi),並使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/ mysqli) - [這篇文章](http://j.mp/QEx8IB)將幫助你決定哪個。如果你選擇PDO,[這裏是一個很好的教程](http://j.mp/PoWehJ)。 – ceejayoz

回答

1

$來將只包含從查詢ResultSet對象。

你應該使用mysql_fetch_array它拆分成一個數組,並使用0號元素,或使用mysql_result命令從$拉動零元素

細節上的這些位置:

然而,這種方法已被棄用......你應該看看PDO的文檔建議。

0
$search_query="select email from main where phno = 5756407850"; 
$to = mysql_query($search_query); 
echo "hello " . mysql_query($search_query);` 

你已經分配了此請求mysql_query($ SEARCH_QUERY)到變量$爲什麼不使用,而是和你不能只是呼應的結果直接您已經通過,你必須使用FETCH_ASSOC或fetch_array結果迭代,順便說一句歡迎新會員。

0
<?php 
$search_query = mysql_query("SELECT email FROM main WHERE phno='5756407850'"); 

//do simple count check in your database 
$count = mysql_num_rows($search_query); 
//if the count is greater than 0; if there is a match 
if($count > 0){ 
while($row = mysql_fetch_array($search_query)){ 
$to = $row["phno"];/*this here will grab the number in a variable*/ 


}//end while statement 
}//end if statement 


//will echo the variable that has the number in it onto the php page 
echo $to; 
?> 

嘗試上面的代碼, 對不起,我發現你的問題有點難以理解/不同於我做什麼,但應該爲你工作。

通常當人們想要使用php從數據庫中獲取某些細節時,他們首先會進行一個查詢,在單獨的頁面上列出該數據庫的所有條目供用戶選擇,然後他們將在下一個

("SELECT * FROM db_table WHERE selected_id='$selected_id'); 

然後抓住從該表中的所有數據,並把它們放到PHP變量和echo出來

,如果我不是做太大的意義,然後通過各種手段,我強烈建議http://shop.oreilly.com/product/9780596006303.do因爲它確實教了很多非常容易理解的方式的PHP和MySQL

+0

我想添加這本書,我推薦也使用「mysqli」而不是mysql查詢。哪一個很重要,因爲如果使用「mysql」查詢,每個人都有一個牛肉,並且在此使用「mysqli」很重要天 –