如何将mysql查询转换为php查询

时间:2013-04-19 00:51:42

标签: php mysql

我一直在我的mysql数据库上运行这个查询:

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);`

我在网络浏览器中的结果是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;

3 个答案:

答案 0 :(得分:1)

$ to将只包含查询中的resultset对象。

您应该使用mysql_fetch_array将其拆分为数组并使用第0个元素,或使用mysql_result命令将第0个元素从$拉到

有关这些内容的详细信息:http://php.net/manual/en/function.mysql-result.php

但是,这种方法已被弃用......您应该按照文档的建议来查看PDO。

答案 1 :(得分: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的结果,顺便说一句,欢迎新成员

答案 2 :(得分: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变量中,然后将其回显

如果我没有多大意义,那么无论如何我强烈推荐http://shop.oreilly.com/product/9780596006303.do,因为它确实以非常容易理解的方式教授php和mysql

相关问题