简体   繁体   中英

Sql query empty result on php code

$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.

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 ) { 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM