getMessage() .-6ren">
gpt4 book ai didi

PHP PDO 没有在我的网页上显示任何数据

转载 作者:行者123 更新时间:2023-11-29 02:22:36 25 4
gpt4 key购买 nike

我最近尝试将我的过程式 MySQL 查询转换为 PDO 语句。我从 php 官方文档中复制了以下代码,并向其中添加了我的参数。它没有在页面中显示任何结果。

<?php
$dsn = 'mysql:host=localhost;dbname=database';
$user = 'user';
$pass = 'pass';
try {
$dbh = new PDO($dsn , $user, $pass);
$dbh = null;
} catch (PDOException $e) {
print "An error has occurred. Please contact support. <br/>" . $e->getMessage() . "<br/>";
die();
}

$value = 'user1';

$stmt = $dbh->prepare("SELECT * FROM table where username = ?");
if ($stmt->execute(array($value))) {
while ($row = $stmt->fetch()) {
print_r($row);
}

?>

最佳答案

    Try this:-

<?php

$dsn = 'mysql:host=localhost;dbname=databasename';

$user = 'user';

$pass = 'password';

try {
$dbh = new PDO($dsn , $user, $pass);

} catch (PDOException $e) {

print "An error has occurred. Please contact support. <br/>" .

$e->getMessage() . "<br/>";

die();

}

$value = 'user1';

$stmt = $dbh->prepare("SELECT * FROM table where column= ?");
if ($stmt->execute(array($value))) {
while ($row = $stmt->fetch()) {
print_r($row);
}
}
?>

关于PHP PDO 没有在我的网页上显示任何数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29209785/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com