douduiti3040 2016-08-10 20:32
浏览 36

使用php和ajax根据第一个字母选择数据库项时出错

I want to create a simple program which function is: The user chooses a letter from a simple form and then the program generates all the elements from my "prestashop" named database which start with this letter. I created the database in phpmyadmin and created the code in php and in ajax. But I'm not sure if this piece of my code really makes the selection of the elements which start with the letter chosen from the user.An error occurs anyway.enter image description here Here is my php code:

<?php
$q = intval($_GET['q']);/* 54-th row*/

$con = mysqli_connect('localhost','root','','prestashop');
if (!$con) {
    die('Could not connect: ' . mysqli_error($con));
}

mysqli_select_db($con,"prestashop");
$sql="SELECT * FROM prestashop WHERE marca LIKE '".$q."'";
$result = mysqli_query($con,$sql);

echo "<table>
<tr>
<th>Marca</th>
<th>Descrizione</th>
</tr>";
while($row = mysqli_fetch_array($result)) {/* 70-th row*/
    echo "<tr>";
    echo "<td>" . $row['marca'] . "</td>";
    echo "<td>" . $row['descrizione'] . "</td>";
    echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>

Is there something I should change to make this works?

</div>
  • 写回答

1条回答 默认 最新

  • duanchui1279 2016-08-10 20:42
    关注

    Well, first of, correct syntax for "like" in mysql is:

    LIKE '%foo%'
    LIKE '%foo'
    LIKE 'foo%'
    

    Secondly, according to PHP documentation regarding mysqli_query:

    Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE.

    So you will need to check what it returns and if it is false, show some error for example.

    评论

报告相同问题?

悬赏问题

  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序