首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在PHP搜索框中显示MYSQL数据

如何在PHP搜索框中显示MYSQL数据
EN

Stack Overflow用户
提问于 2013-04-01 00:10:33
回答 5查看 14.2K关注 0票数 0

你好,我在这项任务中遇到了很大的问题

我在xampp上建立了一个名为search_test的数据库,其中有名字和姓氏作为字段。我已经设置了一个php表单,这样当用户输入一个名字时,比如说Andre,它会返回数据库中的所有andres。有一个问题,它一直告诉我没有搜索结果,即使我知道数据库里有数据,这里的代码应该是一个叫做index.php的php页面

代码语言:javascript
运行
复制
    <?php
    mysql_connect("localhost","michael","xcA123sd") or die(mysql_error());
    mysql_select_db("search_test") or die ("could not find db"); 
     $output ='';
    if (isset ($_POST['search']));
        $searchq = $_POST['search'];
    $query = mysql_query("SELECT * FROM members WHERE firstname LIKE '%searchq%'" ) or die("could not search");
    $count = mysql_num_rows($query);
    if($count == 0){
        $output = 'There was no search results !';
        }else{
        while($row = mysql_fetch_array($query)){
        $fname = $row['firstname'];
        $output .='<div> '.$fname.'</div>';
        }

        }
}


?>

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>search</title>
</head>
<body>
<form action="index.php" method="post">
<input type="text" name="search" placeholder="search for members"/> 
<input type="submit" value=">>"/>
</form>
<?php print("$output);?>
</body
</html> 

例如,我键入andre in,就会得到响应

没有搜索结果!

有没有人能帮帮忙

EN

Stack Overflow用户

发布于 2013-04-01 00:18:29

问题来自您代码的这一行

$query = mysql_query("SELECT * FROM members WHERE firstname LIKE '%searchq%'“)

变量searchq后面没有$

票数 2
EN
查看全部 5 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15731470

复制
相关文章

相似问题

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