English 中文(简体)
Pphp 外币密钥数据库的 pphp 搜索引擎
原标题:php search engine for a database with foreignkeys
  • 时间:2012-05-21 13:49:09
  •  标签:
  • php
  • mysql

hi 所有我正在尝试创建一个php搜索页面,该页面将从 Mysql 数据库中提供一份书籍列表,然后当点击书名时,该书名将提供一份与它们关系表上的书籍列表。我略微挣扎着代码,希望有人能伸出援手

-这是我的搜索.php文件

  <?php
    $i=0;
    $column_name =  title ; // column to search by
    $k =$_GET[ k ];
    $terms = explode(" ",$k);
    //connect before calling mysql_real_escape_string
    mysql_connect("localhost","","");
    mysql_select_db("test");
    $query ="SELECT id,title,author 
    FROM  books WHERE";
    foreach ($terms as $each){
        $i++;
        $each =  %  . $each .  % ; // add wildcard
        $each = mysql_real_escape_string($each); // prevent sql injection
        if($i==1)
            $query .= " $column_name LIKE  $each  ";
        else
            $query .= " OR $column_name LIKE  $each  ";

    }

    echo  QUERY:   . $query;

    $query = mysql_query($query) OR DIE(mysql_error());

//Code below is for using the relationships table assuming you have a column name id that
//references to the relationships table. Also, you should add a index on the column id.

$results = "";

while($row = mysql_fetch_array($query)) {
  $results .=  <li>
                <a href="book-relationships.php?id= .$row[ relationshipid ]. "> .$row[ title ].   author:  .$row[ author ]. </a>
              </li> ;
}

$results =  <ul>  . $results .  </ul> ;

echo $results;
最佳回答

删除“; 从此行中删除 :

FROM books WHERE ";                 ";

您必须申报 $i

$i = 0;

为了防止 sql 注入,您可以使用:

foreach ($terms as $each){
        $i++;
        $each =  %  . $each .  % ; // add wildcard
        $each = mysql_real_escape_string($each); // prevent sql injection
        if($i==1)
            $query .= " $keywords LIKE  $each  ";
        else
            $query .= " OR $keywords LIKE  $each  ";

    }

另外, 确定用户无法将变量设置到不存在的表格

完整代码

<?php
    $i=0;
    $column_name =  title ; // column to search by
    $k =$_GET[ k ];
    $terms = explode(" ",$k);
    //connect before calling mysql_real_escape_string
    mysql_connect("localhost","","");
    mysql_select_db("test");
    $query ="SELECT id,title,author 
    FROM  books WHERE";
    foreach ($terms as $each){
        $i++;
        $each =  %  . $each .  % ; // add wildcard
        $each = mysql_real_escape_string($each); // prevent sql injection
        if($i==1)
            $query .= " $column_name LIKE  $each  ";
        else
            $query .= " OR $column_name LIKE  $each  ";

    }

    echo  QUERY:   . $query;

    $query = mysql_query($query) OR DIE(mysql_error());

//Code below is for using the relationships table assuming you have a column name id that
//references to the relationships table. Also, you should add a index on the column id.

$results = "";

while($row = mysql_fetch_array($query)) {
  $results .=  <li>
                <a href="book-relationships.php?id= .$row[ id ]. "> .$row[ title ].   author:  .$row[ author ]. </a>
              </li> ;
}

$results =  <ul>  . $ results .  </ul> ;

echo $results;
问题回答

暂无回答




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签