php连接MSSQL问题 5
按照这个来连接mssql并查询,为什么会报错呢数据库连接成功Fatalerror:Calltoamemberfunctionfetch()onbooleaninD:\My...
按照这个来连接mssql并查询,为什么 会报错呢
数据库连接成功
Fatal error: Call to a member function fetch() on boolean in D:\Myenv\Apache24\htdocs\HelloHBuilder\test.php on line 11
<?php
try{
$conn = new PDO("sqlsrv:Server=39.*.*.**;Database=webtest","webtest","**");
echo "数据库连接成功";
}catch(PDOException $e){
echo "数据库连接失败".$e->getMessage();
}
$sql = "select count(*) from webtest";
$res = $conn->query($sql);
while ($row = $res->fetch()){
print_r($row);
}
?> 展开
数据库连接成功
Fatal error: Call to a member function fetch() on boolean in D:\Myenv\Apache24\htdocs\HelloHBuilder\test.php on line 11
<?php
try{
$conn = new PDO("sqlsrv:Server=39.*.*.**;Database=webtest","webtest","**");
echo "数据库连接成功";
}catch(PDOException $e){
echo "数据库连接失败".$e->getMessage();
}
$sql = "select count(*) from webtest";
$res = $conn->query($sql);
while ($row = $res->fetch()){
print_r($row);
}
?> 展开
2个回答
展开全部
<?php
$mysql_conf = array(
'host' => '127.0.0.1:3306',
'db' => 'test',
'db_user' => 'root',
'db_pwd' => 'joshua317',
);
$pdo = new PDO("mysql:host=" . $mysql_conf['host'] . ";dbname=" . $mysql_conf['db'], $mysql_conf['db_user'], $mysql_conf['db_pwd']);//创建一个pdo对象
$pdo->exec("set names 'utf8'");
$sql = "select * from user where name = ?";
$stmt = $pdo->prepare($sql);
$stmt->bindValue(1, 'joshua', PDO::PARAM_STR);
$rs = $stmt->execute();
if ($rs) {
// PDO::FETCH_ASSOC 关联数组形式
// PDO::FETCH_NUM 数字索引数组形式
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
var_dump($row);
}
}
$pdo = null;//关闭连接
?>
更多追问追答
追问
这个是连接mysql的呀。
我需要连接mssql
php版本是5.5
追答
那你有开启php_pdo_mssql.dll扩展吗。在phpinfo中可查看,如果没开可以去打开尝试一下
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询