简体   繁体   English

如何将此 SQL 转换为准备好的语句?

[英]How do I convert this SQL to prepared statement?

I need help with converting this SQL to Prepared Statement.我需要帮助将这个 SQL 转换为 Prepared Statement。 This is for my search bar.这是我的搜索栏。 I hope I'll be able to receive some help as I am a beginner in this.我希望我能得到一些帮助,因为我是这方面的初学者。

This is my SQL这是我的 SQL

$conn = mysqli_connect('localhost','root','','my_db');
            
$mysql = "SELECT * FROM catetable";
$bike_list = mysqli_query($conn,$mysql);

$catesql = "SELECT catename FROM catetable";
$cate_list = mysqli_query($conn,$catesql);

And this is what I would like to change to Prepared Statement这就是我想更改为 Prepared Statement

if (isset($_GET['search']))
{
    
    $search = $_GET['search'];
    
    $searchlist = array();
    $lowersearchlist = array();
    $i = 0;
    while ($one_cate = mysqli_fetch_assoc($cate_list))
    {
        $searchlist[$i] = $one_cate['catename'];
        $lowersearchlist[$i] = strtolower($one_cate['catename']);
        $i++;
    }
    if (in_array($search,$searchlist) || in_array($search,$lowersearchlist))
    {
        header("Location:feature.php");
    }
    else
    {
        header("Location:index.php?error=true");
        
    }
}

Write a query that matches the parameter in the WHERE clause.编写一个与WHERE子句中的参数匹配的查询。 MySQL normally defaults to case-insensitive comparisons, so you don't need to fetch all the rows to compare them exactly and case-insensitively. MySQL 通常默认为不区分大小写的比较,因此您无需获取所有行来精确比较它们且不区分大小写。

if (isset($_GET['search'])) {
    $stmt = $conn->prepare("SELECT COUNT(*) AS c FROM yourTable WHERE catename = ?");
    $stmt->bind_param("s", $_GET['search']);
    $stmt->execute();
    $result = $stmt->get_result();
    $row = $result->fetch_assoc();
    if ($row['c'] > 0) {
        header("Location: feature.php");
    } else {
        header("Location: index.php?error=true";
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM