Uncaught TypeError: mysqli_query(): Argument #1 ($conn) must be of type mysqli, PDO given

huangapple go评论64阅读模式
英文:

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

&lt;?php
$servername = &quot;localhost&quot;;
$username = &quot;root&quot;;
$password = &quot;&quot;; 

$conn = new PDO(&quot;mysql:host=$servername;dbname=///&quot;, $username, $password);
// set the PDO error mode to exception
$conn-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

view.php

&lt;?php
            include(&quot;../config/connection.php&quot;);
            $name_surname = $_GET[&#39;name_surname&#39;];
            if ($name_surname) {
                $sql = &quot;SELECT * FROM problems_water WHERE name_surname = $name_surname&quot;;
                $result = mysqli_query($conn, $sql);
                while ($problems_water = mysqli_fetch_array($result)) {
                 ?&gt;
                 &lt;h3&gt;Title:&lt;/h3&gt;
                 &lt;p&gt;&lt;?php echo $row[&quot;name_surname&quot;]; ?&gt;&lt;/p&gt;
                 &lt;h3&gt;Description:&lt;/h3&gt;
                 &lt;p&gt;&lt;?php echo $row[&quot;phone_number&quot;]; ?&gt;&lt;/p&gt;
                 &lt;h3&gt;Author:&lt;/h3&gt;
                 &lt;p&gt;&lt;?php echo $row[&quot;address&quot;]; ?&gt;&lt;/p&gt;
                 &lt;h3&gt;Type:&lt;/h3&gt;
                 &lt;p&gt;&lt;?php echo $row[&quot;area&quot;]; ?&gt;&lt;/p&gt;
                 &lt;h3&gt;Type:&lt;/h3&gt;
                 &lt;p&gt;&lt;?php echo $row[&quot;created_at&quot;]; ?&gt;&lt;/p&gt;
            
                 &lt;?php
               }
            }
           ?&gt;

答案1

得分: 1

你现在有:

$conn = new PDO(&quot;mysql:host=$servername;dbname=///&quot;, $username, $password);

所以 $conn 是一个PDO连接,必须使用PDO函数,而不是 mysqli,因为它是一个完全不同的驱动程序。你还应该使用预处理语句以防止SQL注入攻击。以下更改应该使你的代码正常运行。将以下代码:

$result = mysqli_query($conn, $sql);

替换为:

$stmt = $conn-&gt;prepare(&#39;SELECT * FROM problems_water WHERE name_surname = ? &#39;);
$stmt-&gt;execute([$name_surname]);

并将以下代码:

while ($problems_water = mysqli_fetch_array($result)) {

替换为:

while ($row = $stmt-&gt;fetch(PDO::FETCH_ASSOC)){

文档链接:

https://www.php.net/manual/en/pdo.prepare.php&lt;br>
https://www.php.net/manual/en/pdostatement.execute.php&lt;br>
https://www.php.net/manual/en/pdostatement.fetch.php&lt;br>

或者,你也可以将驱动程序切换为mysqli,但我不建议这样做。坚持使用PDO。

https://www.php.net/manual/en/mysqli.construct.php

(另外,你的输出中有一个错误,$row 在你的 while 循环中没有定义,所以它会引发错误通知,而不提供结果。)

英文:

You have:

$conn = new PDO(&quot;mysql:host=$servername;dbname=///&quot;, $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-&gt;prepare(&#39;SELECT * FROM problems_water WHERE name_surname = ? &#39;);
$stmt-&gt;execute([$name_surname]);

and replace:

while ($problems_water = mysqli_fetch_array($result)) {

with:

while ($row = $stmt-&gt;fetch(PDO::FETCH_ASSOC)){

Documents:

https://www.php.net/manual/en/pdo.prepare.php&lt;br>
https://www.php.net/manual/en/pdostatement.execute.php&lt;br>
https://www.php.net/manual/en/pdostatement.fetch.php&lt;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.)

huangapple
  • 本文由 发表于 2023年7月14日 00:05:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76681387.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定