从表中获取值时出现数据库错误

时间:2021-12-19 01:50:12

Hi I am using ajax for fetching values from database. My connection with database is ok, but while I am trying to show the result it's showing me an error. How can I solve this? TIA

您好我使用ajax从数据库中获取值。我与数据库的连接没问题,但是当我试图显示结果时,它显示了一个错误。我怎么解决这个问题? TIA

my config file:

我的配置文件:

<?php
define('DB_HOST', 'localhost');
define('DB_NAME', 'steptwor_sscamera');
define('DB_USERNAME','root');
define('DB_PASSWORD','');
$con = mysqli_connect(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME);
if( mysqli_connect_error()) echo "Failed to connect to MySQL: " . mysqli_connect_error();

ajax.php :

<?php

require_once 'config.php';
if(!empty($_POST['type'])){
    $type = $_POST['type'];
    $name = $_POST['name_startsWith'];
    $query = "SELECT category_name, product_name, amount FROM v_product_list UPPER($type) LIKE '".strtoupper($name)."%'";
    $result = mysqli_query($con, $query);
    $data = array();
    while ($row = mysqli_fetch_assoc($result)) {
        $name = $row['category_name'].'|'.$row['product_name'].'|'.$row['amount'];
        array_push($data, $name);
    }   
    echo json_encode($data);exit;
}

error:

<br />
<b>Warning</b>:  mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in <b>F
:\xampp\htdocs\sscamera\ajax.php</b> on line <b>10</b><br />
[]

2 个解决方案

#1


You've missed WHERE:

你错过了WHERE:

    $query = "SELECT category_name, product_name, amount FROM v_product_list WHERE UPPER($type) LIKE '".strtoupper($name)."%'";

#2


Missing WHERE. Try with -

缺少WHERE。尝试 -

$query = "SELECT category_name, product_name, amount FROM v_product_list WHERE UPPER($type) LIKE '".strtoupper($name)."%'";

#1


You've missed WHERE:

你错过了WHERE:

    $query = "SELECT category_name, product_name, amount FROM v_product_list WHERE UPPER($type) LIKE '".strtoupper($name)."%'";

#2


Missing WHERE. Try with -

缺少WHERE。尝试 -

$query = "SELECT category_name, product_name, amount FROM v_product_list WHERE UPPER($type) LIKE '".strtoupper($name)."%'";