无法使用mysqli,mysqli_connect或PDO连接到数据库 [英] Cannot connect to Database with mysqli, mysqli_connect, or PDO

查看:101
本文介绍了无法使用mysqli,mysqli_connect或PDO连接到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试用php连接到我的数据库,但是完全失败;(我一直得到类似的信息:"PHP注意:未定义的变量:conn"我尝试了以下每个操作:

I have tried connecting with php to my database but have been completely unsuccessful ;( I keep getting something like: "PHP Notice: Undefined variable: conn" I have tried each of the following:

$conn = new PDO("mysql:host=localhost;dbname=ar_data", 'login', 'pass');
$conn = mysqli('localhost', 'login', 'pass', 'dbname') or die("error" :.mysqli_connect_error());
$conn = mysqli_connect('localhost', 'login', 'pass', 'ar_data') or die("error" :.mysqli_connect_error());

尝试了最后一个之后,我在var_dump($conn)上得到了它:

After I tried that last one I got this on a var_dump($conn) :

object(mysqli)#17 (18) { ["affected_rows"]=> int(0) ["client_info"]=> string(78) 
"mysqlnd 5.0.8-dev - 20102224 - $Id: SOME_VALUE_I_AM_CENSORING $" 
["client_version"]=> int(50008) ["connect_errno"]=> int(0) ["connect_error"]=> NULL 
["errno"]=> int(0) ["error"]=> string(0) "" ["field_count"]=> int(0) ["host_info"]=> 
string(20) "localhost via TCP/IP" ["info"]=> NULL ["insert_id"]=> int(0) ["server_info"]=>
string(6) "5.5.25" ["server_version"]=> int(50525) ["stat"]=> string(140) "Uptime: 477847 
Threads: 6 Questions: 202259 Slow queries: 1 Opens: 1454 Flush tables: 1 Open tables: 10 
Queries per second avg: 0.423" ["sqlstate"]=> string(5) "00000" 
["protocol_version"]=> int(10)
["thread_id"]=> int(1506) ["warning_count"]=> int(0) }

我还尝试使用不具有ar_data数据库特权的用户,这将给我一个预期的错误.如果我在mysqli_prepare($conn, $sql)之前执行var_dump($conn),则它将打印NULL.如何连接到数据库?

Also I have tried using users that do not have privileges to the ar_data database and it will give me an error for that as expected. If I do var_dump($conn) right before a mysqli_prepare($conn, $sql) then it will print NULL. How can I connect to the DB?

注意:我已经将名称从conn更改为其他var,以防它被更改但行为仍然相同.我还安装了Drupal 7和Wordpress(我想他们只是在使用基本的msql进行连接)

Notes: I have changed the name from conn to other vars incase it was getting changed but still same behavior. I also have Drupal 7 and Wordpress installed ( I imagine they are just using basic msql to connect)

更新:

整个文件: https://dl.dropbox.com/u/6688861/phpconn .txt

推荐答案

最后一个var_dump表示连接正常.您的问题是您在ValidateForm()函数中引用$conn而没有在该函数中声明global $conn.

The last var_dump indicates that the connection is working fine. Your problem is that you're referring to $conn in the ValidateForm() function without declaring global $conn in that function.

这篇关于无法使用mysqli,mysqli_connect或PDO连接到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆