Mysql准备的语句喜欢的问题

时间:2019-02-08 03:35:54

标签: php mysql ajax mysqli prepared-statement


我正在尝试制作一个实时搜索表单,每当用户键入一个字母时,就会调用一个ajax函数并在数据库中搜索结果

Ajax被成功调用,但是,我唯一的问题是我的查询就像是WHERE col = value not LIKE一样工作,我必须键入col全名才能获得结果,否则我没有任何结果。

这是我的PHP代码:

if (isset($_POST['search'])) {
$tagname = $_POST['search'];
$mysqli->set_charset('utf8');
$stmt = $mysqli->prepare("SELECT *,(SELECT COUNT(tagid) FROM sectags WHERE sectags.tagid = tags.tagid) FROM tags WHERE tag LIKE ?");
$stmt->bind_param('s', $tagname);
$stmt->execute();    // Execute the prepared query.
$stmt->store_result();
$stmt->bind_result($id, $tag, $imghref, $tagcount);
echo '<ul>';


while ($stmt->fetch()) {
    ?>
    <!-- Creating unordered list items.
         Calling javascript function named as "fill" found in "script.js" file.
         By passing fetched result as parameter. -->
    <li onclick='fill("<?php echo $tag ?>")'>
        <a>
            <!-- Assigning searched result in "Search box" in "search.php" file. -->
            <?php echo $tag; ?>
    </li></a>
    <?php
}
?>
</ul>
<?php
}


从数据库中选择的其他参数将在以后使用。
谢谢。

1 个答案:

答案 0 :(得分:2)

您需要将通配符包含在绑定到参数的值中,以使const path = require("path"); const HtmlWebpackPlugin = require("html-webpack-plugin"); const CleanWebpackPlugin = require("clean-webpack-plugin"); const webpack = require("webpack"); module.exports = { mode: process.env.NODE_ENV || "development", module: { rules: [ { test: /\.css$/, use: ["style-loader", "css-loader"] }, { test: /\.html$/, loader: "html-loader" }, { test: /\.(png|svg|jpg|gif|woff|woff2|eot|ttf|otf)$/, use: ["file-loader"] }, { test: /\.(js|jsx)$/, exclude: /node_modules/, use: ["babel-loader"] } ] }, resolve: { extensions: ["*", ".js", ".jsx"] }, entry: "./src/main.jsx", output: { filename: "build.js", path: path.resolve(__dirname, "dist") }, devtool: process.env.NODE_ENV == "development" ? "inline-source-map" : undefined, devServer: { contentBase: "./dist", hot: true }, plugins: [ new CleanWebpackPlugin(["dist"]), new HtmlWebpackPlugin({ template: "./index.html" }), new webpack.HotModuleReplacementPlugin() ] }; 正常工作,例如

LIKE
相关问题