簡體   English   中英

Mysqli_query 執行失敗並拋出錯誤

[英]Mysqli_query fails to execute and throwing an error

我正在從我的 php 表中檢索數據。 我的代碼在這里:

<?php 
$con = require('config.php');
?>

<html>
<head><title>Php Practise Web</title></head>
    <body>
        <?php 
            $sqlQuery = 'SELECT * FROM  customers';
            $customersData = mysqli_query($con,$sqlQuery);
                if(!$customersData){
                    die('Error in retrieving :'.mysqli_connect_error($con));
                    }
                    else{
                 while($dataRow = mysqli_fetch_array($customersData, MYSQLI_NUM)){?>
        <table>
            <tr>
                <thead>Customer Name<thead>
                <thead>Customer Email<thead>
                <thead>Customer Username<thead>
                <thead>Customer Age<thead>
            </tr>
            <tr>
                <td><?php echo $dataRow['c_name']?></td>
                <td><?php echo $dataRow['c_email']?></td>
                <td><?php echo $dataRow['c_username']?></td>
                <td><?php echo $dataRow['c_age']?></td>
            </tr>
        </table>
    <?php 
                 }
            }
    ?>
    </body>
  </html>

但它拋出了這樣的錯誤這是我的 config.php 代碼

<?php 
$con = mysqli_connect("localhost","root","","phppractise");

if(mysqli_connect_errno()){
    echo "Failed to connect to Mysql : ".mysqli_connect_errno();
}
else{
    echo "Database Connected";
  }

?>

在此處輸入圖片說明

實際上,從一開始我就與我的數據庫建立連接,然后從名為“customers”的表中檢索數據。 我正在 mysqli_query 中傳遞連接變量和查詢變量,但它仍然拋出錯誤。請告訴我我的代碼在哪里錯誤?

您需要從此處刪除$con =

$con = require('config.php');

這就是您收到該錯誤的原因。

做就是了:

require('config.php');

或包含文件。

include('config.php');

該變量與您的連接相同。

為了嘗試解釋這里發生的事情,它很可能循環遍歷require()和連接中的變量賦值,這意味着它從發布的錯誤中返回1作為布爾值(整數)。

  • 這是我最好的假設,這對我來說是有道理的。

取自錯誤圖像:

警告:mysqli_query() 期望參數 1 是給定的 mysqli 整數

為了使用這樣的語法( $con = require('config.php'); ),您需要使用一個類/方法,然后從那里return它的一個實例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM