简体   繁体   中英

Cannot connect to Database with mysqli, mysqli_connect, or PDO

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());

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) }

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?

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)

UPDATE:

Entire file: https://dl.dropbox.com/u/6688861/phpconn.txt

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.

Your connection is fine in that last result. You would not get the MySQL server information back if your connection didn't work. Where are you getting the undefined variable error with relation to this call? Might be helpful to have more context.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM