
正文
php数据库老是失败 php数据库老是失败怎么解决
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
php连接数据库失败
mysql_connect();
php数据库老是失败你先用php文件调用这个填上你php数据库老是失败的信息参数
看看连接成不成功,然后看下mysql_error 出来什么
相关问答
Q1: PHP登陆注册页在本地测试时,出现连接数据库失败的情况怎么办?
1、检查环境正常
使用mysql -u root -p 可以进入MySQL操作界面
直接使用/usr/local/php5/bin/php /web/test.php执行可以连上数据库
2、打开hosts加入
复制代码代码如下:127.0.0.1 qttc
使用qttc当主机连接也正常,唯独就不认localhost。
3、localhost连接方式不同导致
为了了解PHP连接数据库时,主机填写localhost与其它的区别阅读了大量资料,最后得知:
当主机填写为localhost时mysql会采用 unix domain socket连接
当主机填写为127.0.0.1时mysql会采用tcp方式连接
这是linux套接字网络的特性,win平台不会有这个问题
4、解决方法
在my.cnf的[mysql]区段里添加
复制代码代码如下:
protocol=tcp
保存重启MySQL,问题解决!
Q2: 连接php显示数据库失败怎么回事啊?
连接字串中php数据库老是失败的用户名和密码错误。php数据库老是失败你查一下test.phpphp数据库老是失败的第三行看看就知道php数据库老是失败了。
Q3: php连接数据库的类,但是选择数据库的时候总是出错,怎么回事呢?
构造函数错咯
function __construct($host,$user,$pass,$database){
$this - host=$host;
$this - user=$user;
$this - pass=$pass;
$this - database=$database;
// echo $db;
$conn = mysql_connect($host,$user,$pass);
$db = mysql_select_db($this - database,$conn);
if($db){
echo "数据库成功";
}else{
echo "数据库失败";
}
}
//给php数据库老是失败你改咯哈这个类
class register{
private $host; //The host address
private $user; //The user
private $pass; //The password
private $database; //The database
private $conn;
//Connect with the database
function __construct($host,$user,$pass,$database){
$this - host=$host;
$this - user=$user;
$this - pass=$pass;
$this - database=$database;
$conn = mysql_connect($host,$user,$pass)or die("连接失败!");
mysql_select_db($this - database,$conn) or die("选择数据库失败!");
$this-conn=$conn;
}
//Add a user
function addUser($data){
$dataObj = $data;//类里边不要把变量写得太死
$userInfo = json_decode($dataObj,true); //change the data from the type of json to array.
$username = $userInfo[0];
$password = $userInfo[1];
$repassword = $userInfo[2];
//The sql to add the new user
@$sql_addUser = EOF
insert into users values("","$username","$password","$repassword");
EOF;
// var_dump($sql_addUser);break;
$result = mysql_query($sql_addUser,$this-conn);//指定连接php数据库老是失败,少些麻烦
// var_dump($result);break;
if($result){
echo 1;
}else{
echo 0;
}
}
}
Q4: php连接数据库失败怎么办
首先你可以把链接失败的错误编码或者错误代码展示出来,然后根据错误编码去手册上面找对应编码错误的原因及解决的办法。
关于php数据库老是失败和php数据库老是失败怎么解决的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。







