我想实现一个查询,就查询数据库中像XXX的记录,如果有记录,然后用一个循环语句输出10条记录,如果没有10条就退出循环,我FOR语句搞了很久都没有实现,我输出了10条全是一样的记录代码:
<?php
$keywords=$_POST['keyword'];
if($keywords!=null){
$host="localhost";
$user="root";
$pass="123";
$db="test";
$connection=mysql_connect($host,$user,$pass) or die ("Unable to connect!");
mysql_select_db($db) or die("Unable to select database!");
mysql_query("SET NAMES 'utf-8'"); 
$query="select title from test where title like '%$kewords%' order by id desc";
$result=mysql_query($query) or die("Erro in query:$query.".mysql_erro());
?>
下面的代码怎么写了
我越想就越想不明白了,所以求助了,大哥帮帮我.谢谢
我刚没有分了

解决方案 »

  1.   

    哪个SQL语句是$query="select title,id from test where title like '%$kewords%' order by id desc";
      

  2.   

    大概是类似这样的(接着你写的部分)
    <?php
    $array=array();
    while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
    {
       $array[]=$row;
    }
    ?>
      

  3.   

    哪个语句是写错 ,我要实现的和下面的ASP一样<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
    <!--#include file="conn.asp"-->
    <%
    '-----------------------------------------------------------------
    dim keyword
    keyword=trim(request.Form("keyword")) '接收ajax发送的参数
    if keyword="" then response.End()
    '------------------------------------------------------------------
    set rs=server.CreateObject("adodb.recordset")
    sql="select * from chengyu where chengyu like '"&keyword&"%' order by searchtimes desc"
    rs.open sql,conn,1,1
    '------------------------------------------------------------------
    '--------如果没有找到的话,返回0
    '--------如果找到的话,返回所有匹配的项目
    if not (rs.eof and rs.bof) then
    response.Write("<ul>")
    for i=0 to 9
    if rs.eof then exit for
    response.Write("<meta http-equiv='Content-Type' content='text/html; charset=GB2312' />")
    response.Write("<li value="""&i&""" onclick=""form_submit()"" onmouseover=""mo(this.value)"">"&rs("chengyu")&"<span>搜索"&rs("searchtimes")&"次</span></li>")
    rs.movenext
    next
    response.Write("</ul>")
    end if
    rs.close
    set rs=nothing
    %>
      

  4.   

    用while直接打印出来就可以了
    while($row=mysql_fetch_array($result)){
     echo $row[];}
    mysql_fetch_array返回的是一条数组,当循环完毕的时候也就会退出我刚学的 多多指点
      

  5.   

    SET NAMES 'utf8'
    没有-
      

  6.   

    while ($myrow = mysql_fetch_array($result)){
     for($i=0;$i<10;$i++){
    echo "<td>$myrow[$i]</td>";
    }
    }
      

  7.   

    <?php
    echo 123;
    ?>
    这个我作为新手的时候的第一个php程序。很经典吧。
      

  8.   

    <?php
    $keywords=$_POST['keyword'];
    if($keywords!=null){
    $host="localhost";
    $user="root";
    $pass="123";
    $db="test";
    $connection=mysql_connect($host,$user,$pass) or die ("Unable to connect!");
    mysql_select_db($db) or die("Unable to select database!");
    mysql_query("SET NAMES 'utf-8'"); 
    $query="select title from test where title like '%$kewords%' order by id desc";
    $result=mysql_query($query) or die("Erro in query:$query.".mysql_erro());
    ////////////////////////////////////
    $i=10;
    while(($row = mysql_fetch_object($result)) && $i > 0)
    {
       echo $row->name;
       echo $row->email;
       ....
       $i--;
    }
    ?>
    差不多类似这样吧