如何在前一个 SQL 查询结果的 SQL 查询中传递 PHP 变量?
How to pass PHP variables in SQL query resulted from the previous SQL query?
我正在处理一个 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>';
}
?>
以上代码是获取某个范围内的图像数据。
这会起作用:
mysql_query("select * from data_get having LatLong between $e and $f",$conn) ;
但是请停止使用mysql_query,它是deprecated
试试这个代码
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;
}
你可以这样做:
$where="WHERE LatLong between ".$e." AND ".$f.";
mysql_query("select * from data_get $where",$conn) ;
答案是,当您在 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>';
}
?>
我正在处理一个 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>';
}
?>
以上代码是获取某个范围内的图像数据。
这会起作用:
mysql_query("select * from data_get having LatLong between $e and $f",$conn) ;
但是请停止使用mysql_query,它是deprecated
试试这个代码
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;
}
你可以这样做:
$where="WHERE LatLong between ".$e." AND ".$f.";
mysql_query("select * from data_get $where",$conn) ;
答案是,当您在 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>';
}
?>