以下是我嘗試創建數據庫連接的一段代碼。請在下面的代碼中看到我提到的問題發生的地方。此外,我還提到了一個工作正常的連接代碼。PHP中的數據庫連接 - 意外錯誤
請讓我知道我可以打電話給我的dbconfig.php
,這樣它的行爲就像那段能夠成功連接的代碼。
感謝
<?php
//Including Header file for the connectivity to the database
require_once('Connections/dbconfig.php');
mysql_select_db($database_dbconfig, $dbconfig);
// If I use the following line of code for connectivity then it works perfectly fine:
//$dbh = new PDO('mysql:host=localhost;dbname=rare','root','');
$dbh= $dbconfig;
$q = 'select resident_id,u_first,u_last from z_events group by resident_id';
/*
The following error will occur when I try to make a connection from the header file:
Fatal error: Call to a member function prepare() on a non-object in C:\Users\QAD\Downloads\CAR\index12 - Copy.php on line 200
*/
$user = $dbh->prepare($q);
$user->execute();
?>
dbconfig.php
<?php
# FileName="Connection_php_mysql.htm"
# Type="MYSQL"
# HTTP="true"
$hostname_dbconfig = "localhost";
$database_dbconfig = "rare";
$username_dbconfig = "root";
$password_dbconfig = "";
$dbconfig = mysql_pconnect($hostname_dbconfig, $username_dbconfig, $password_dbconfig) or trigger_error(mysql_error(),E_USER_ERROR);
?>
爲什麼你想混合PDO和'mysql_ *'???? – PeeHaa
我不知道它,因爲我正在對別人項目 –
這就是爲什麼我們有[文檔](http://www.php.net/manual/en/):-) – PeeHaa