php将SQL查询结果赋值给变量

2012-03-25 12:12 a786013819 | 分类:数据库DB | 浏览1393次

$sql = "select field1 from pre_common_member_profile where uid='$username'";
得到的结果是这样的:
field1
1234
怎么把1234赋值给变量

 

你是只取这一个值还是要取数组。只取一个的话。
$sql = "select field1 from pre_common_member_profile where uid='$username'";
$query = mysql_query($sql);
$bianliang = mysql_result($query,0);
echo $bianliang;
取数组的话。
$sql = "select field1 from pre_common_member_profile where uid='$username'";
$query = mysql_query($sql);
while($row = mysql_fetch_array($query))
{
echo $row["field1 "];
}
完毕。这些操作sql的基础知识,去看看php100的视频教程吧。给分。

提问者评价

非常感谢,也谢谢其他回答的人!

 

///////////////////////////////////////////////////////////////////////////////////////////

 

php如何判断SQL语句的查询结果是否为空?

$sql =mysql_query( "select * from tv_video where title like '%$keyword%' limit $offset,$PageSize");

$result=mysql_fetch_array($sql);

if(!empty($result)){

while($result=mysql_fetch_array($sql)){echo "hello word!"}

if(!empty($result)){

echo "记录为空";

}

 

测试结果为:无论记录是否为空,都会输出"hello word!"与"记录为空",也就是两个条件都成立,这就让我很费解了,到底怎样判断一个SQL返回结果是否为空?

 

$result =mysql_query( "select * from tv_video where title like '%$keyword%' limit $offset,$PageSize");

if (mysql_num_rows($result) < 1) echo '记录集为空';

 

 

$result =mysql_query( "select * from tv_video where title like '%$keyword%' limit $offset,$PageSize");

if(count($result)<0)

{

echo "查询无数据!";

 

}

 

一楼的方法已经测试通过 ,三楼的估计应该不行,count值怎么也不会小于0吧,换成1应该也可以,四楼的mysql_affected_rows函数还真没见过,改天也测试下。

posted on 2013-10-31 19:44  为人民服务  阅读(2869)  评论(0编辑  收藏  举报