如何在SQL查询中传递PHP变量,这是由以前的SQL查询引起的?

时间:2015-10-19 05:55:08

标签: php mysql sql-server having nested-queries

我正在研究一个PHP项目,我遇到了一个问题,我无法传递PHP变量来执行先前执行的查询所产生的SQL查询。

<?php 
    include"config.php"; 
    $sqli = mysql_query("select * from police_data where p_id='$name_perm'",$conn); 
        //name_perm is again a variable that stores the police id. It works very fine.

    while($rows = mysql_fetch_array($sqli))
    {
        echo $rows['location'].'<br>';
        $e = intval($rows['location']+ 6);
        $f = intval($rows['location']- 6);
        echo $e.'<br>'; // I received the location in INT format
    }   echo $f.'<br>';

    $sql = mysql_query("select * from data_get having LatLong between 90 and 95",$conn) ; 
        // This query gives me result. But instead of 90 and 95 I want to pass $e and $f.
        // When I tried so it doesn't provide any result. 

    while($row = mysql_fetch_array($sql))
    {
        echo '<table>';

        echo '<tr>';
        echo '<td><img height="300" width="240" src="data:image;base64,'.$row['Image']. '"></td>';
        echo '<td>'.$row['LatLong'].'</td>';
        echo '<td>'.$row['month'].'</td></tr>';
        echo '</table>';
    }   
?>

以上代码用于检索某个范围之间的图像数据。

4 个答案:

答案 0 :(得分:0)

这将有效:

 $files=$_POST['check_list']; 
 $zipname = 'kmm.zip';
 $zip = new ZipArchive;
 $zip->open('kmm.zip', ZipArchive::CREATE |ZipArchive::OVERWRITE);

 foreach ($files as $file) 
 { 
     $file="audio/$file";
     $zip->addFile($file);
     $zip->close();
 } 

 header('Content-Type: application/zip');
 header("Content-Disposition: attachment; filename=kmm.zip");
 header('Content-Length: ' . filesize($zipname));

但请停止使用mysql_query,它是enter image description here

答案 1 :(得分:0)

试试此代码

mysql_query("select * from data_get having LatLong between $e and $f",$conn) ;

请试试这个: -

while($rows = mysql_fetch_array($sqli))
    {
        echo $rows['location'].'<br>';
        $e = intval($rows['location']+ 6);
        $f = intval($rows['location']- 6);
        echo $e.'<br>'; // I received the location in INT format
        echo $f.'<br>';
        break;
    }  

答案 2 :(得分:0)

你可以这样做:

$where="WHERE LatLong between ".$e." AND ".$f.";


 mysql_query("select * from data_get $where",$conn) ;

答案 3 :(得分:0)

答案是,当您在 函数中传递任何值时,请确保传递的第一个值应小于第二个值。

所以在我的代码中,

<?php 
    include"config.php"; 
    $sqli = mysql_query("select * from police_data where p_id='$name_perm'",$conn); 
        //name_perm is again a variable that stores the police id. It works very fine.

    while($rows = mysql_fetch_array($sqli))
    {
        echo $rows['location'].'<br>';
        $e = intval($rows['location']+ 6);
        $f = intval($rows['location']- 6);
        echo $e.'<br>'; // I received the location in INT format
    }   echo $f.'<br>';

    $sql = mysql_query("select * from data_get having LatLong between $f and $e",$conn) ; 
        // $e = 103 , $f = 91
        // so, mysql_query("select * from data_get having LatLong between $e and $f",$conn) ; is wrong. Hence $e and $f had to swapped.   

    while($row = mysql_fetch_array($sql))
    {
        echo '<table>';

        echo '<tr>';
        echo '<td><img height="300" width="240" src="data:image;base64,'.$row['Image']. '"></td>';
        echo '<td>'.$row['LatLong'].'</td>';
        echo '<td>'.$row['month'].'</td></tr>';
        echo '</table>';
    }   
?>