Home >Backend Development >PHP Tutorial >php连接mysql不成功,该怎么解决

php连接mysql不成功,该怎么解决

WBOY
WBOYOriginal
2016-06-13 11:49:421161browse

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)<br /> {<br /> 	echo "mysqli_real_connect error!";<br /> 	exit(0);<br /> }


这个判断写的有问题,不存在$ret才会报错。

if(!$ret)
 {
  echo "mysqli_real_connect error!";
  exit(0);
 }

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn