Php 空值上的PDO MSSQL错误查询
我试图从我的数据库表中选择行,但我得到一个错误,说明了这一点Php 空值上的PDO MSSQL错误查询,php,mysql,sql-server,pdo,Php,Mysql,Sql Server,Pdo,我试图从我的数据库表中选择行,但我得到一个错误,说明了这一点 Fatal error: Call to a member function query() on null in.. 我们与数据库的连接显示成功。下面是我的php代码: <?php require_once("dbconn.php"); $db = getConnection(); $input_pid = "870104-07-5448"; $sql = "SELECT * FROM Pat WH
Fatal error: Call to a member function query() on null in..
我们与数据库的连接显示成功。下面是我的php代码:
<?php
require_once("dbconn.php");
$db = getConnection();
$input_pid = "870104-07-5448";
$sql = "SELECT * FROM Pat WHERE PID ='$input_pid'";
$stmt = $db->query($sql);
$row = $stmt->fetchObject();
echo $row->PID;
echo $row->Name;
?>
您永远不会在函数中返回$db
getConnection()
将功能更改为:
function getConnection(){
try {
$hostname = "busctrlctr-pc";
$dbname = "DispenserSystem";
$username = "sa";
$password = "123456";
$db = new PDO ("sqlsrv:Server=$hostname;Database=$dbname", $username, $password);
echo 'We are succesful to connect the database !!'.'<br>'; // successful word
return $db;
} catch (PDOException $e){
echo "connection failed problem is >> ". $e -> getMessage()."\n";
exit;
}
}
函数getConnection(){
试一试{
$hostname=“busctrlctr pc”;
$dbname=“分发系统”;
$username=“sa”;
$password=“123456”;
$db=new-PDO(“sqlsrv:Server=$hostname;Database=$dbname”、$username、$password);
echo“我们成功地连接了数据库!!”。
;//成功的word
返回$db;
}捕获(PDO$e){
echo“连接失败问题为>>”$e->getMessage()。“\n”;
出口
}
}
getConnection()中有什么?发布您的dbconn.php
文件代码检查这看起来您的连接无法建立。@b0s3我已经发布了问题中的db conn代码
function getConnection(){
try {
$hostname = "busctrlctr-pc";
$dbname = "DispenserSystem";
$username = "sa";
$password = "123456";
$db = new PDO ("sqlsrv:Server=$hostname;Database=$dbname", $username, $password);
echo 'We are succesful to connect the database !!'.'<br>'; // successful word
return $db;
} catch (PDOException $e){
echo "connection failed problem is >> ". $e -> getMessage()."\n";
exit;
}
}