首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MySQL实时查询

MySQL实时查询
EN

Stack Overflow用户
提问于 2012-01-28 05:14:36
回答 2查看 940关注 0票数 0

我想知道是否有人可以帮助我正在尝试合并到我的网站现场搜索,我已经找到了一个教程和外观方面的事情现在正在工作,谁能告诉我如何编辑以下PHP从只有一个表的信息。

表-要为每个结果回显的电影字段-电影、描述、图像

目前它正在成功地从2个表中拉出信息,一个显示搜索的内容,另一个为类别分隔符提供信息,我需要的是删除类别方面,并从单个表中拉出信息。

抱歉,我的PHP知识非常有限,希望这能最好地描述这个问题。

代码语言:javascript
复制
<p id="searchresults">
<?php
// PHP5 Implementation - uses MySQLi.
// mysqli('localhost', 'yourUsername', 'yourPassword', 'yourDatabase');
$db = new mysqli('localhost', 'yourUsername', 'yourPassword', 'yourDatabase');

if(!$db) {
    // Show error if we cannot connect.
    echo 'ERROR: Could not connect to the database.';
} else {

    // Is there a posted query string?
    if(isset($_POST['queryString'])) {
        $queryString = $db->real_escape_string($_POST['queryString']);

        // Is the string length greater than 0?
        if(strlen($queryString) >0) {
            $query = $db->query("SELECT * FROM search s INNER JOIN categories c ON s.cat_id = c.cid WHERE name LIKE '%" . $queryString . "%' ORDER BY cat_id LIMIT 8");

            if($query) {
                // While there are results loop through them - fetching an Object.

                // Store the category id
                $catid = 0;
                while ($result = $query ->fetch_object()) {
                    if($result->cat_id != $catid) { // check if the category changed
                        echo '<span class="category">'.$result->cat_name.'</span>';
                        $catid = $result->cat_id;
                    }
                    echo '<a href="'.$result->url.'">';
                    echo '<img src="search_images/'.$result->img.'" alt="" />';

                    $name = $result->name;
                    if(strlen($name) > 35) { 
                        $name = substr($name, 0, 35) . "...";
                    }                       
                    echo '<span class="searchheading">'.$name.'</span>';

                    $description = $result->desc;
                    if(strlen($description) > 80) { 
                        $description = substr($description, 0, 80) . "...";
                    }

                    echo '<span>'.$description.'</span></a>';
                }
                echo '<span class="seperator"><a href="http://www.marcofolio.net/sitemap.html" title="Sitemap">Nothing interesting here? Try the sitemap.</a></span><br class="break" />';
            } else {
                echo 'ERROR: There was a problem with the query.';
            }
        } else {
            // Dont do anything.
        } // There is a queryString.
    } else {
        echo 'There should be no direct access to this script!';
    }
}
?>
</p>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-01-28 05:21:53

替换

代码语言:javascript
复制
SELECT * FROM search s INNER JOIN categories c ON s.cat_id = c.cid WHERE name LIKE '%" . $queryString . "%' ORDER BY cat_id LIMIT 8

使用

代码语言:javascript
复制
SELECT * FROM movies WHERE movie LIKE '%" . $queryString . "%' ORDER BY movie LIMIT 8

会是一个很好的开始。

你能从那里继续下去吗?

另外-你在上面的描述中为字段名写了'Movie‘-我使用了一个小写的'M’,所以你可能需要改变它。

编辑回复评论#1:

让我们从简单的开始-将if ($query) { ... }替换为

代码语言:javascript
复制
if ($query) {
    while ($result = $query ->fetch_object()) { // this line loops through all the results
        echo '<img src="search_images/'.$result->image.' />'; // check capital I on 'image'
        echo '<strong>'.$result->movie.'</strong>';
        echo $result->description.'<br />';
    }
}

基本上,您只需将$result->myFieldName放在while循环中,即可获得所需的数据。希望这足以让你继续前进=]

票数 0
EN

Stack Overflow用户

发布于 2012-01-28 05:19:09

我猜:

代码语言:javascript
复制
<p id="searchresults">
<?php
// PHP5 Implementation - uses MySQLi.
// mysqli('localhost', 'yourUsername', 'yourPassword', 'yourDatabase');
$db = new mysqli('localhost', 'yourUsername', 'yourPassword', 'yourDatabase');

if(!$db) {
    // Show error if we cannot connect.
    echo 'ERROR: Could not connect to the database.';
} else {

    // Is there a posted query string?
    if(isset($_POST['queryString'])) {
        $queryString = $db->real_escape_string($_POST['queryString']);

        // Is the string length greater than 0?
        if(strlen($queryString) >0) {
            $query = $db->query("SELECT * FROM movies  WHERE Movie LIKE '%" . $queryString . "%' ORDER BY Movie LIMIT 8");

            if($query) {
                // While there are results loop through them - fetching an Object.


                while ($result = $query ->fetch_object()) {
                    echo '<a href="'.$result->url.'">';
                    echo '<img src="search_images/'.$result->img.'" alt="" />';

                    $name = $result->movie;
                    if(strlen($name) > 35) { 
                        $name = substr($name, 0, 35) . "...";
                    }                       
                    echo '<span class="searchheading">'.$name.'</span>';

                    $description = $result->description;
                    if(strlen($description) > 80) { 
                        $description = substr($description, 0, 80) . "...";
                    }

                    echo '<span>'.$description.'</span></a>';
                }
                echo '<span class="seperator"><a href="http://www.marcofolio.net/sitemap.html" title="Sitemap">Nothing interesting here? Try the sitemap.</a></span><br class="break" />';
            } else {
                echo 'ERROR: There was a problem with the query.';
            }
        } else {
            // Dont do anything.
        } // There is a queryString.
    } else {
        echo 'There should be no direct access to this script!';
    }
}
?>
</p>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9040174

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档