简体   繁体   English

PHP脚本搜索MySQL数据库

[英]PHP script to search MySQL database

I have a script that is supposed to return values from a mysql tables based on search inputs. 我有一个脚本,应该根据搜索输入从mysql表中返回值。 This script is composed of two files. 该脚本由两个文件组成。

search.php search.php

<?php
if ( isset( $_GET['s'])) {
require_once( dirname( __FILE__ ) . '/class-search.php' );
$search = new search();
$search_term = $GET['s'];
$search_results = $search->search($search_term);
}
?>
<!DOCTYPE html>
<html>
<head>
    <title>Search</title>
</head>
<body>
    <h1>Search</h1>
    <div class="search-form">
        <form action="" method="get">
            <div class="form-field">
                <label for="search-field">Search</label>
                <input type="search" name="s" placeholder="Search by name" results="5" value="<?php echo $search_term; ?>">
                <input type="submit" value="Search">
            </div>
        </form>
    </div>
    <?php if ( $search_results ) : ?>
    <div class="results-count">
        <p><?php echo $search_results['count']; ?> results found</p>
    </div>
    <div class="results-table">
        <?php foreach ( $search_results['results'] as $search_result ) : ?>
        <div class="result">
            <p><?php echo $search_result->title; ?></p>
        </div>
        <?php endforeach; ?>
    </div>
    <div class="search-raw">
        <pre><?php print_r($search_results); ?></pre>
    </div>
    <?php endif; ?>
</body>

and class-search.php 和class-search.php

<?php
class search {

private $mysqli;
public function __construct() {
    $this->connect();
}
private function connect() {
    $this->mysqli = new mysqli('HOST', 'USERNAME', 'PASSWORD', 'DATABASE' );
}
public function search($search_term) {
    $sanitized = $this->mysqli->query("
    SELECT * FROM `Apple`
    FROM search
    WHERE Last_Name LIKE '%{$sanitized}%'
    ");
    if ( ! $query->num_rows ) {
        return false;
    }
    while( $row = $query->fetch_object() ) {
        $rows[] = $row;
    }
    $search_results = array(
    'count' => $query->num_rows,
    'results' => $rows,
    );
    return $search_results;
}
}
?>

Within my database I have two tables, but I'm only interested in searching the content of one (Apple). 在我的数据库中,我有两个表,但是我只想搜索一个表(Apple)的内容。 Can somebody help me? 有人可以帮我吗? I can't seem to make this work. 我似乎无法完成这项工作。 No results are returned no matter what I search. 无论我搜索什么,都不会返回任何结果。 As of now I'm only using the Last_Name criteria, but I'd like to add others. 到目前为止,我仅使用Last_Name条件,但是我想添加其他条件。 Here's a link to the screenshot of my table http://imgur.com/a/H3DnG . 这是我的桌子http://imgur.com/a/H3DnG的屏幕截图的链接。

I'd really appreciate any feedback possible. 非常感谢您提供任何反馈意见。 Thank you. 谢谢。

If you check it again, 如果您再次检查,

In search method you're passing $search_term as argument but in the query you're using $sanitized which doesn't exists until the query is executed. 搜索方法中,您将传递$search_term作为参数,但在查询中,您使用的是$sanitized ,该参数在执行查询之前不存在。

You're result set is in $sanitized but you're checking $query for num_rows which don't even exists. 您的结果集在$ sanitize中,但是您正在检查$ query中是否存在num_rows Also, you're returning false in that method so you're not able to identify the actual problem. 另外,您在该方法中返回的是false ,因此您无法确定实际的问题。

public function search($search_term) {
    $sanitized = $this->mysqli->query("
    SELECT * FROM `Apple`
    FROM search
    WHERE Last_Name LIKE '%{$search_term}%'
    ");
    if ( ! $sanitized->num_rows ) {
        //return false;
        retrun [];
    }
    $rows  = [];
    while( $row = $sanitized->fetch_object() ) {
        $rows[] = $row;
    }
    $search_results = array(
    'count' => $query->num_rows,
    'results' => $rows,
    );
    return $search_results;
}

In connect method , add this which will tell whether its getting connected to database or not. connect方法中 ,添加此项将告诉其是否已连接到数据库。

if ($this->mysqli->connect_errno) {
    printf("Connect failed: %s\n", $this->mysqli->connect_error);
    exit();
}

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

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