PHP头条
热点:

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.='
'.$……

www.phpzy.comtrue/phprm/38089.htmlTechArticlephp一个基础有关问题想请问上大家 php一个基础问题 想请教下大家。 PHP code $conn=mysql_connect("localhost","root","123456");if(!$conn){ die('could not connect:'.mysql_error());}else{ echo '链接正常 ';}mysql_selec...

相关文章

PHP之友评论

今天推荐