CDubYaa
CDubYaa

Reputation: 45

Use Negative numbers in mysql php if statement?

I have the following code with an if statement and the value is negative one is some rows of data. Yet when I use this statement it reads the -1 value as 1.

if ($row ['bedrooms'] == '-1')
{
    echo "<td class='propdescres'>Studio</td></tr>";
}
else
{
    echo "<td class='propdescres'>" . $row['bedrooms'] . "</td></tr>";
}

I tried adding

$studio = $row ['bedrooms']*'-99';

And changing my statement to:

if ($studio == '99')
{
    echo "<td class='propdescres'>Studio</td></tr>";
}
else
{
    echo "<td class='propdescres'>" . $row['bedrooms'] . "</td></tr>";

The following is the query I am using to get the data:

$result = mysqli_query($con, "SELECT realty.id, realty.unit_num, realty.address, realty.price, realty.bedrooms, realty.bathrooms, realty.metadesc FROM realty

The field type is tinyint(3) and when I query the database for bedrooms = -1 it returns vales but in my echo results in php all the -1 values are 1.

Can somebody please give me a little insight as to what I am doing wrong or how I can retrieve negative values?

Thank you.

Upvotes: 0

Views: 1938

Answers (2)

LIAL
LIAL

Reputation: 1634

Try to use so:

$someNegVal = '-1';
if (isset($row['bedrooms']) &&  $row['bedrooms'] == -1*abs($someNegVal)) {
//....
}

Upvotes: 1

icecub
icecub

Reputation: 8793

This should do the trick:

if((int)$row['bedrooms'] == -1){
    echo "<td class='propdescres'>Studio</td></tr>";
} else {
    echo "<td class='propdescres'>" . $row['bedrooms'] . "</td></tr>";
}

Although (int) will cast the variable to an integer no matter what, you should first confirm that the variable actually returns an integer in the first place.

Upvotes: 0

Related Questions