英文:
Uncaught TypeError: mysqli_query(): Argument #1 ($conn) must be of type mysqli, PDO given
问题
以下是您要翻译的内容:
我在使用PDO连接时遇到了问题,我已经花了几个小时来修复它,但它仍然无法正常工作,我想显示数据库中可用的数据。我遇到了许多关于预期类型为'mysqli'但找到'PDO'的问题。
connection.php
<?php
$servername = "localhost";
$username = "root";
$password = "";
$conn = new PDO("mysql:host=$servername;dbname=///", $username, $password);
// 将PDO错误模式设置为异常
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
view.php
<?php
include("../config/connection.php");
$name_surname = $_GET['name_surname'];
if ($name_surname) {
$sql = "SELECT * FROM problems_water WHERE name_surname = $name_surname";
$result = mysqli_query($conn, $sql);
while ($problems_water = mysqli_fetch_array($result)) {
?>
<h3>标题:</h3>
<p><?php echo $row["name_surname"]; ?></p>
<h3>描述:</h3>
<p><?php echo $row["phone_number"]; ?></p>
<h3>作者:</h3>
<p><?php echo $row["address"]; ?></p>
<h3>类型:</h3>
<p><?php echo $row["area"]; ?></p>
<h3>类型:</h3>
<p><?php echo $row["created_at"]; ?></p>
<?php
}
}
?>
请注意,您的代码存在一些问题,尤其是在使用PDO连接的情况下尝试使用mysqli查询。您需要将连接方式统一为PDO或mysqli,以确保一致性。
英文:
I'm having a problem with my PDO connection, I've been fixing it for hours but it doesn't work, I want to show the data available in the database. I have a lot of problems with Expected type 'mysqli'. Found 'PDO'.
connection.php
<?php
$servername = "localhost";
$username = "root";
$password = "";
$conn = new PDO("mysql:host=$servername;dbname=///", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
view.php
<?php
include("../config/connection.php");
$name_surname = $_GET['name_surname'];
if ($name_surname) {
$sql = "SELECT * FROM problems_water WHERE name_surname = $name_surname";
$result = mysqli_query($conn, $sql);
while ($problems_water = mysqli_fetch_array($result)) {
?>
<h3>Title:</h3>
<p><?php echo $row["name_surname"]; ?></p>
<h3>Description:</h3>
<p><?php echo $row["phone_number"]; ?></p>
<h3>Author:</h3>
<p><?php echo $row["address"]; ?></p>
<h3>Type:</h3>
<p><?php echo $row["area"]; ?></p>
<h3>Type:</h3>
<p><?php echo $row["created_at"]; ?></p>
<?php
}
}
?>
答案1
得分: 1
你现在有:
$conn = new PDO("mysql:host=$servername;dbname=///", $username, $password);
所以 $conn
是一个PDO连接,必须使用PDO函数,而不是 mysqli
,因为它是一个完全不同的驱动程序。你还应该使用预处理语句以防止SQL注入攻击。以下更改应该使你的代码正常运行。将以下代码:
$result = mysqli_query($conn, $sql);
替换为:
$stmt = $conn->prepare('SELECT * FROM problems_water WHERE name_surname = ? ');
$stmt->execute([$name_surname]);
并将以下代码:
while ($problems_water = mysqli_fetch_array($result)) {
替换为:
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
文档链接:
https://www.php.net/manual/en/pdo.prepare.php<br>
https://www.php.net/manual/en/pdostatement.execute.php<br>
https://www.php.net/manual/en/pdostatement.fetch.php<br>
或者,你也可以将驱动程序切换为mysqli,但我不建议这样做。坚持使用PDO。
https://www.php.net/manual/en/mysqli.construct.php
(另外,你的输出中有一个错误,$row
在你的 while
循环中没有定义,所以它会引发错误通知,而不提供结果。)
英文:
You have:
$conn = new PDO("mysql:host=$servername;dbname=///", $username, $password);
so $conn
is a PDO connection and PDO functions must be used, not mysqli
which is an entirely different driver. You also should use prepared statements so you aren't open to SQL injections. The following changes should allow your code to function. Replace:
$result = mysqli_query($conn, $sql);
with:
$stmt = $conn->prepare('SELECT * FROM problems_water WHERE name_surname = ? ');
$stmt->execute([$name_surname]);
and replace:
while ($problems_water = mysqli_fetch_array($result)) {
with:
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
Documents:
https://www.php.net/manual/en/pdo.prepare.php<br>
https://www.php.net/manual/en/pdostatement.execute.php<br>
https://www.php.net/manual/en/pdostatement.fetch.php<br>
Alternatively, you could swap the driver to be mysqli but I would not recommend that. Stick with PDO.
https://www.php.net/manual/en/mysqli.construct.php
(You also had an error in your outputting $row
wasn't defined in your while
loop so it would have thrown error notices and not given results.)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论