MYSQL PHP - get float LIKE $float

半腔热情 提交于 2019-12-02 20:31:55

问题


I want to get rows with floats like my $float.

I used this code:

$float = $_GET['float'];

$requst = mysql_fetch_array(mysql_query("SELECT * FROM floats WHERE float LIKE'$float.%%%%%%%'"));

then I echoed all the rows with while:

while ($r = $request) {

    echo $a['float'];

}

but the page doesn't show anything. (YES, I typed in the address bar ?float=34 and there are floats like 34.****** in the table.)

What is the problem?

(PHP version 5.2, MYSQL version 5.0)


回答1:


Try This

$requst = mysql_query("SELECT * FROM floats WHERE float LIKE'$float.%%%%%%%'");
while ($r = mysql_fetch_array($request)) {

    echo $r['float'];

}



回答2:


How about select * from floats where floor(float) = $floor;?




回答3:


Your variable already has a decimal point in it.

"SELECT * FROM floats WHERE float LIKE '$float%'"

If your float is 34.567 your statement executes as find like '34.567.%' which isn't finding any results.

Edit: how about this then?

"SELECT * FROM floats WHERE abs(float-$float)<1;"

That will bring in anything within 1?

Having said that, although you can keep floats in mysql, it might be safer to keep them as decimals with a limited number of points after the decimal.



来源:https://stackoverflow.com/questions/11562165/mysql-php-get-float-like-float

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!