dongxie2613 2013-09-24 10:36
浏览 25

php代码上的SQL查询空结果

$publication_year = $_GET['publication_year'];
$conn = db_connect('guest');

....

<?php

$query='SELECT DISTINCT publication_id FROM publications ';
if(isset($keyword_label) && isset($publication_year)){
    $query.=' WHERE (publication_key_1="'.$keyword_label.'" OR publication_key_2="'.$keyword_label.'" OR publication_key_3="'.$keyword_label.'" OR 
    publication_key_4="'.$keyword_label.'" OR publication_key_5="'.$keyword_label.'" OR publication_key_6="'.$keyword_label.'" OR
    publication_key_7="'.$keyword_label.'") AND publication_year="'.$publication_year.'"';
}
else if(isset($publication_year)){
    $query.=' WHERE publication_year="'.$publication_year.'"';
}
else if(isset($keyword_label)){
    $query.='WHERE publication_key_1="'.$keyword_label.'" OR publication_key_2="'.$keyword_label.'" OR publication_key_3="'.$keyword_label.'" OR 
    publication_key_4="'.$keyword_label.'" OR publication_key_5="'.$keyword_label.'" OR publication_key_6="'.$keyword_label.'" OR
    publication_key_7="'.$keyword_label.'"';
}
$query.=' ORDER BY publication_year DESC';

$result = $conn->query($query);
if(!$result) {
    echo 'Could not run query: ' . mysql_error();
    echo mysqli_error($conn);
}

returns on my website

Could not run query:

without getting the exact error.

Of course I get an empty result if I do not debug it.

Can you tell me why is this happening?

I am LAMP stack(apache 2.2.2. , MySQL client version: 5.5.32 , PHP 5.3.10-1ubuntu3.8 with Suhosin-Patch (cli) ) and phpmyadmin where I loaded my ready mysql code that is functional except this.

Running the query

SELECT DISTINCT publication_id
FROM publications
ORDER BY publication_year DESC

works just fine and returns the results.

  • 写回答

1条回答 默认 最新

  • dousi6192 2013-09-24 10:49
    关注

    try this

        $query='SELECT DISTINCT publication_id FROM publications WHERE 1=1 ';
     if(isset($keyword_label) && isset($publication_year)){
     $query.=' AND (publication_key_1="'.$keyword_label.'" OR publication_key_2="'.$keyword_label.'" OR publication_key_3="'.$keyword_label.'" OR 
    publication_key_4="'.$keyword_label.'" OR publication_key_5="'.$keyword_label.'" OR publication_key_6="'.$keyword_label.'" OR
    publication_key_7="'.$keyword_label.'") AND publication_year="'.$publication_year.'"';
    }
    if(isset($publication_year)){
    $query.=' AND publication_year="'.$publication_year.'"';
    }
    if(isset($keyword_label)){
    $query.=' AND publication_key_1="'.$keyword_label.'" OR publication_key_2="'.$keyword_label.'" OR publication_key_3="'.$keyword_label.'" OR 
    publication_key_4="'.$keyword_label.'" OR publication_key_5="'.$keyword_label.'" OR publication_key_6="'.$keyword_label.'" OR
    publication_key_7="'.$keyword_label.'"';
    }
    $query.=' ORDER BY publication_year DESC';
    

    EDIT:

    try change this

       if(!$result) {
    

    to this

      if( $result == false ) { 
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化