您好,欢迎访问一九零五行业门户网

php一个基础有关问题 想请问上大家

php一个基础问题 想请教下大家。
php code$conn=mysql_connect(localhost,root,123456);if(!$conn){ die('could not connect:'.mysql_error());}else{ echo '链接正常
';}mysql_select_db(zuitu_db,$conn);$sql=select * from category order by id desc; // $sql=select * from 'category';这样写有错吗?$res=mysql_query($sql);$i=1;$strtemp='';/* 下面这个while循环,为何读不出数据?*/while($row=mysql_fetch_array($res)){ if($i==1) { $strtemp=$row[name]; } else { $strtemp+='
'.$row[name]; } // ($i==1)?$strtemp=$row[name]:strtemp+='
'.$row[name]; 这样写有误吗? $i+=1;}echo $strtemp;//以上内容 请大家帮忙指正,多谢!
------解决方案--------------------
strtemp+='
'.$row[name];
变量必须加 $, 你这样写会被认为是语法错误,因为strtemp只能被理解为常量,而常量只能通过define()函数来赋值。
另外php的字符串连接符不是 + ,是 . 
$strtemp .= '
'.$row[name];
题外话,建议你在 每个mysql_query()后面加一个判断,如果数据库查询有错,也能及时找到根源
------解决方案--------------------
$sql=select * from 'category';这样写有错吗?
$sql=select * from `category`;
($i==1)?$strtemp=$row[name]:strtemp+='
'.$row[name]; //这样写有误吗?
($i==1)?$strtemp=$row[name]:$strtemp.='
'.$row[name];
$strtemp = ($i==1)?$row[name]:$strtemp.'
'.$row[name];
------解决方案--------------------
*
下面这个while循环,为何读不出数据?
*/
while($row=mysql_fetch_array($res)){
if($i==1)
{
$strtemp=$row[name];
}
else
{
$strtemp.='
'.$row[name];
}
------解决方案--------------------
同意三楼的方法, $strtemp+='
'.$row[name]; 这句用了+号,php会把$strtemp作为int型数据进行处理,而不是字符串
------解决方案--------------------
探讨
$sql=select * from 'category';这样写有错吗?
$sql=select * from `category`;
($i==1)?$strtemp=$row[name]:strtemp+='
'.$row[name]; //这样写有误吗?
($i==1)?$strtemp=$row[name]:$strtemp.='
'.$……
其它类似信息

推荐信息