php连接mysql不成功
我是新手,连接一个test2数据库中的test表
create table test
(id int,
name varchar(255)
)
然后在zendsudio中写入代码(代码是按照书上写的) :
$conn=mysqli_init();
if(!$conn)
{
echo "mysqli_init error";
exit(0);
}
$ret=mysqli_real_connect($conn,"localhost","root","840803",
"test2",3306,"test",MYSQLI_CLIENT_FOUND_ROWS);
if($ret)
{
echo "mysqli_real_connect error!";
exit(0);
}
$sql='insert into test values(1,"tom")';
$ret=mysqli_query($conn,$sql);
if(!$ret)
{
echo "mysqli_query error!";
exit(0);
}
$sql='select * from test';
$ret=mysqli_query($conn,$sql);
while(($row=mysqli_fetch_array($ret,MYSQLI_ASSOC))!=NULL)
{
echo "id:".$row['id']."
";
echo "name".$row['name']."
";
}
?>
运行之后会出现 :
mysqli_real_connect error!
这是哪里出问题了,请各位大侠指点一下 ,急,在线等 !
------解决方案--------------------if($ret)
{
echo "mysqli_real_connect error!";
exit(0);
}
连接成功后不进入这里,进入哪里? 是if(!$ret) 吧?
------解决方案--------------------if($ret)
{
echo "mysqli_real_connect error!";
exit(0);
}
这个判断写的有问题,不存在$ret才会报错。
if(!$ret)
{
echo "mysqli_real_connect error!";
exit(0);
}