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

时间:2015-05-09 06:59:11

标签: php mysql ajax

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

我的配置文件:

<?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;
}

错误:

<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 个答案:

答案 0 :(得分:0)

你错过了WHERE:

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

答案 1 :(得分:0)

缺少WHERE。试试 -

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