警告:mysqli_query()期望参数2为字符串,给定对象

时间:2019-06-25 19:39:46

标签: php mysqli

有人可以帮助我解决此错误吗

  

警告:mysqli_query()期望参数2为字符串,对象在第25行给定..

<?php 
session_start();
include('includes/dbcon.php');

$query = mysqli_query($con, "SELECT * FROM reservation WHERE r_date='".$date."'

if (!mysqli_query($con,$query))  
{
$query = mysqli_query($con, "SELECT * FROM combo where combo_id=1");
$row=mysqli_fetch_array($query);
$price=$row['combo_price'];
$payable=$pax*$price;

3 个答案:

答案 0 :(得分:1)

您的代码有多个问题。缺少;,重复调用mysqli_query,SQL注入并且没有错误检查。

在文件顶部启用异常,而不是使用if检查查询是否成功。使用准备好的语句,最好以面向对象的方式使用。

session_start();
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); //  this line enables exceptions
include 'includes/dbcon.php';

$stmt = $con->prepare('SELECT * FROM reservation WHERE r_date=?'); // ? is a placeholder for binding data
$stmt->bind_param('s', $date); // bind data to SQL statement as a string(s)
$stmt->execute();
$reservations = $stmt->get_result();

// if your SELECT found some record then loop on the result set fetching each row one by one
while ($row = $reservations->fetch_assoc()) {
    $combos = $con->query("SELECT * FROM combo where combo_id=1"); // if there is no data to be bound then we can use query
    $row = $combos->fetch_assoc(); // fetch the matching combo row
    $price = $row['combo_price'];
    $payable = $pax * $price;
}

答案 1 :(得分:0)

<?php 
session_start();
include('includes/dbcon.php');

// you're missing some syntax here.. 
// also your $query IS your query so it should be $query = "SELECT * FROM ";

$query = mysqli_query($con, "SELECT * FROM reservation WHERE r_date='".$date."'
// you don't need this above line.. it does it all right here...
if (!mysqli_query($con,$query))  
{
$query = mysqli_query($con, "SELECT * FROM combo where combo_id=1");
$row=mysqli_fetch_array($query);
$price=$row['combo_price'];
$payable=$pax*$price;
// missing closing brackets. }

答案 2 :(得分:-1)

您的名为query的变量应仅是您的... query

  

$ result = mysqli_query($ con,“ SELECT * FROM Reservation WHERE r_date ='”。$ date。“'”;

即使您认为自己将获得记录,也可以使用mysqli_fetch_array函数 将始终返回一个数组。因此,您需要选择数组中的第一项,然后选择键或索引。

  

$ price = $ row [0] ['combo_price'];

一些代码惯例。不要将所有内容都放入IF中。因为如果失败,$ payable将是不确定的,并引发错误。在脚本顶部对其进行初始化。另外,您还需要存储mysqli_query的返回值,因为您需要释放用于它的内存。

  

mysqli_free_result($ result);