[英]MySQL select query not working.
<?php
$hostname='localhost';
$user='root';
$password='';
$connect=mysqli_connect($hostname,$user,$password,'a_railway');
if(!$connect)
{
die('Could not connect');
}
$rs="central";
$rd="central";
$s="A";
$d="B";
$dist=call_dist($rs,$rd,$s,$d,$connect);
echo $dist;
function call_dist($rs,$rd,$s,$d,$connect)
{
$src_dist="SELECT Distance FROM $rs WHERE Station=$s ";
$dest_dist="SELECT Distance FROM $rs WHERE Station=$d ";
$src_dist=mysqli_query($connect,$src_dist);
$dest_dist=mysqli_query($connect,$dest_dist);
$dist=abs($src_dist-$dest_dist);
return $dist;
}
?>
why my Sql queries are not working? 为什么我的Sql查询不起作用? There is no problem in database.
数据库没有问题。 but the queries does not execute.
但查询不会执行。 i want to find the distance in this program which is found out by subtracting values at points A and B.
我想在此程序中找到通过减去点A和B的值得出的距离。
If station
is a varchar, then you need quotes around the string. 如果
station
是varchar,则需要在字符串前后加上引号。
$src_dist="SELECT Distance FROM $rs WHERE Station='$s' ";
However, you should not ever interpolate values in a string like that. 但是,你不应该永远像一个字符串插入值。 Please use prepared statements!
请使用准备好的语句!
Read this reference, it will make your code and life a lot easier: http://php.net/manual/en/mysqli.quickstart.prepared-statements.php 阅读此参考资料,它将使您的代码和工作变得更加轻松: http : //php.net/manual/en/mysqli.quickstart.prepared-statements.php
edit You can also do the math in sql: 编辑您也可以在sql中进行数学运算:
$dist_query = mysqli_query("SELECT ABS(s.Distance - d.Distance) as dist FROM
( SELECT Distance FROM $rs WHERE Station='$s' ) as s,
( SELECT Distance FROM $rs WHERE Station='$d' ) as d");
$dist_result = $dist_query->fetch_assoc();
return $dist_result[0]['dist'];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.