【问题标题】:How to pass PHP variables in SQL query resulted from the previous SQL query?如何在 SQL 查询中传递上一个 SQL 查询产生的 PHP 变量?
【发布时间】:2015-10-19 05:55:08
【问题描述】:

我正在处理一个 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>';
    }   
?>

以上代码是检索某个范围内的图像数据。

【问题讨论】:

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


    【解决方案1】:

    这将起作用:

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

    但是请停止使用mysql_query,它是deprecated

    【讨论】:

    • 我已经用过那个代码并试过了,但是id没有给我任何结果
    • 然后回显查询前的值,检查是否正确?
    • 做到了。它完美地给了我价值。
    • 好的。 PHP 变量以我指定的方式传递。您的本地设置和值似乎存在一些问题。请检查您的 sql 查询、变量、值和数据库等。
    • 谢谢@Sumit Pandey,您能否详细说明这些问题可能是什么。因为我检查了一切。一切都是正确的。只是这些变量没有被传递。不知道为什么。因为如果你看到我的代码,第一个sql查询` $sqli = mysql_query("select * from police_data where p_id='$name_perm'",$conn); ` 在第 3 行工作得很好。
    【解决方案2】:

    你可以这样做:

    $where="WHERE LatLong between ".$e." AND ".$f.";
    
    
     mysql_query("select * from data_get $where",$conn) ;
    

    【讨论】:

    • 我试过你的代码,但仍然没有结果。我将其修改为,$where = "WHERE LatLong between '.$e.' AND '.$f.' "; $sql = mysql_query("select * from data_get $where",$conn) ;,但仍然没有结果
    • 回显你的查询并在sql命令中点击它,看看是否有值。(检查$e和$f值是否显示)
    • 它甚至在确切的 SQL 查询之前显示了 $e 和 $f 的结果` $sql = mysql_query("select * from data_get 的 LatLong 在 90 和 95 之间",$conn) ; `
    • 那么可能是 $e 和 $f 之间不匹配的行的值
    • 我查过了。如果我静态指定 $e 和 $f 的值(即它的整数值),那么它会给我结果。
    【解决方案3】:

    试试这个代码

    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;
        }  
    

    【讨论】:

    • 我已经用过那个代码并试过了,但是id没有给我任何结果
    • 请打印您的 mysql 查询并在 (phpmyadmin) 或其他 MySQL 客户端运行@JijoNair
    • 如果您的表上有可用数据,请检查 LatLong 数据类型是否为 int ??? @JijoNair
    • 它的浮动。但我使用 INTVAL 函数对其进行了转换。
    • 请打印您的查询和过去的评论@JijoNair
    【解决方案4】:

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

    所以在我的代码中,

    <?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>';
        }   
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-16
      • 1970-01-01
      • 2016-04-22
      • 2016-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多