0

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)

2
  • Be careful using datatypes as column names. Commented Jul 19, 2012 at 13:44
  • You are working with only one result, put the mysql_fetch_array directly inside while in place of $request. Commented Jul 19, 2012 at 13:53

3 Answers 3

2

Try This

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

    echo $r['float'];

}
Sign up to request clarification or add additional context in comments.

Comments

2

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

4 Comments

also, try properly concatenating your query string like so: "select * from floats where floor(float) = ".$floor
Not if you're just matching the integer part
I wasn't really saying that directly to you ethr. Your question (user107..) looks like it is asking us to query all floats that have the same whole number when you remove the decimals. This solution would find those.
I agree; using LIKE forces MySQL to convert the column to a string for comparison which will affect performance
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.