dbConnector.php
<?php
class DbConnector {
var $link;
function DbConnector(){
try{
$this->link = new PDO('mysql:host=127.0.0.1;dbname=system', 'root', '123456');
$this->link->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->link->setAttribute(PDO::ATTR_EMULATE_PREPARES,false);
}catch(PDOException $e){
die("this is not connected");
}
}
//*** Function: query, Purpose: Execute a database query ***
// this function fail to connect
function query($query) {
try{
$this->link->prepare($query);
}catch(PDOException $e){
die("fail to prepare");
}
return $this->link->prepare($query);
}
}
?>
check.php
<?php
include("dbConnector.php");
$connector = new DbConnector();
$username ='';
$query = "SELECT air_users FROM USER_NAME WHERE username = ? LIMIT 1";
$result = $connector->query($query);
$result->execute(array($username));
$num = $result->rowCount();
$num = $result->fetch();
echo $num;
// mysql_close();
?>
I get return error
fail to prepare
What did I do wrong? I double checked it but can't figure it out the error, I cannot get it connected to my database.
It just give me a error.
You use a try/catch, which is great, but your issue is that you just echo that there is a problem, not what the problem is.
If you print out the error message from the exception, I'm sure you will get some more info on why it wont prepare:
try {
$this->link->prepare($query);
} catch(PDOException $e){
die($e->getMessage());
}
Or just temporary remove the try/catch blocks and let the exception go off and show in the browser when testing.
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.