Php MySQL实时查询
我想知道是否有人可以帮助我,我正在尝试将一个实时搜索整合到我的网站,我已经找到了一个教程,外观方面的事情现在正在工作,有人可以告诉我如何编辑下面的PHP,从一个表中提取信息 桌上电影 要为每个结果回显的字段-电影、描述、图像 目前,它正在成功地从两个表中提取信息,一个显示搜索内容,另一个为类别分隔符提供信息,我需要的是删除类别方面并从单个表中提取信息 抱歉,我的PHP知识非常有限,希望这能最好地描述这个问题Php MySQL实时查询,php,mysql,Php,Mysql,我想知道是否有人可以帮助我,我正在尝试将一个实时搜索整合到我的网站,我已经找到了一个教程,外观方面的事情现在正在工作,有人可以告诉我如何编辑下面的PHP,从一个表中提取信息 桌上电影 要为每个结果回显的字段-电影、描述、图像 目前,它正在成功地从两个表中提取信息,一个显示搜索内容,另一个为类别分隔符提供信息,我需要的是删除类别方面并从单个表中提取信息 抱歉,我的PHP知识非常有限,希望这能最好地描述这个问题 <p id="searchresults"> <?php // PHP
<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>
我想:
<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>
替换
SELECT * FROM search s INNER JOIN categories c ON s.cat_id = c.cid WHERE name LIKE '%" . $queryString . "%' ORDER BY cat_id LIMIT 8
与
这将是一个良好的开端
你能从那里继续下去吗
旁白-您在上面的描述中为字段名编写了“Movie”-我使用了小写字母“M”,因此您可能需要更改它
[编辑]在回复评论#1时:
让我们从简单开始-将if($query){…}
替换为
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 />';
}
}
if($query){
而($result=$query->fetch_object()){//这一行循环遍历所有结果
回显“图像”。//>;//检查“图像”上的大写字母I
回显“”.$result->movie.”;
echo$result->description.“
”;
}
}
基本上,您只需将$result->myFieldName
放入while循环中,即可获得所需的数据。
希望这足够让您继续=]嘿,谢谢您的回复,下面的代码让我很困惑,因为它引用了另一个表,我不确定要删除什么?
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 />';
}
}