PHP连接mysql数据库没反应,怎么回事?

我用的是WAMP5,用phpmyadmin可以连接数据库并正常操作,但是在php里就不行,怎么回事啊?我新手。

连接代码如下:
PHP code
$conn =mysql_connect("localhost", "root", "") or die("数据库链接错误");
$result=mysql_select_db("info", $conn);
mysql_query("set names 'GBK'"); //使用GBK中文编码;
if($result){

echo "建立完成";


}else{

echo "建立错误".mysql_error();

}


在另一个php文件中插入数据代码如下:
PHP code
<?php
/*
 * Created on 2011-6-9
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 include("conn.php");
 
 if($_POST['submit']){
      $sql="insert into test (id,user,password) values('','$_POST[user_name]','$_POST[PW]')";
     mysql_query($sql);
     echo "成功";
 }
   
 
 ?>
 
  <form action="add_user.php" method="post">
  
  用户:<input type="text" name="user_name" /><br/>
  
  密码:<input type="text" name="PW" /><br/>
  <input type="submit" name="submit" value="登陆"/>
  
  </form>


既不显示连接成功也不显示错误,这怎么回事啊?

作者: taoran12   发布时间: 2011-06-09

include语句前加上下面一句,看看输出什么错误信息:
PHP code
error_reporting(E_ALL);


另外,代码中这一句显然有问题:
PHP code
$sql="insert into test (id,user,password) values('','$_POST[user_name]','$_POST[PW]')";
//
$sql="insert into test (id,user,password) values('','$_POST[\"user_name\"]','$_POST[\"PW\"]')";

作者: T5500   发布时间: 2011-06-09

数据库用户名密码对不?
有对应的库和表吗?
可以echo下$conn,$result看看库有没有连上。
还有你的变量名$conn和文件名同名,可能会有问题

作者: adin283   发布时间: 2011-06-09

id如果是auto increment的话就不用为null了,如果是not null的话你这插入语句可能有问题。如版主所说,你那个取$_POST中,放的应该是""括起来的字符串。而且,使用完了之后,数据库连接木有关闭。我是新手,有什么不对的地方,请大家纠正。

作者: se_marui_blog   发布时间: 2011-06-09