if/else statement help. Pulling from database

Why won’t the if statement at the end work? $bet continues to be .001 no matter what ‘col’ is in the ‘table’.

[php]
$servername = “localhost”;
$username = “user”;
$password = “pass”;
$dbname = “dbname”;

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$sql = “SELECT col FROM table WHERE username=‘user1’”;
$result = $conn->query($sql);

if ($result > 0) {
$bet = 0.001;
} else {
$bet = 0.000;
}

[/php]

Have you checked what $result contains?

Try this

[php]if (count($result))
{
//do something;
}else{
//do something else
}[/php]

[size=10pt]* You should still do what [member=71845]JimL[/member] said so you understand WHY it didn’t work. Keep in mind, $result is an Array.[/size]

What if he doesn’t want to know the number of results from his query? Perhaps, the values of that column are supposed to be integers. So, maybe Column X contains, the number 67.

Basically it would be

if 67 > 0 { //do code }

The count() function returns the number of elements in an array, since results are returned from the DB you would really be checking how many results were returned. It doesn’t appear that the OP, wants the number of results rendered, but rather the value.

You dont seem to get it. It’s not looking for the “actual” count number, it is only looking to see if there is any number at all. Any number, no matter what it is will be greater than zero. If there are no results, there will be no Count.

yeah, I don’t get your answer. ;D

I cant really explain it an simpler than the last sentence but I will try…

Query says to DB, I want this info.

Mr. Application tells Mr. Count, I just want to know if any records matched my query. I dont care how many there are. I just want to know if there are some.

If there are none, Mr.Count says there are no records that match (Zero records returned)

If there ARE records returned , Mr.Count tells Mr. application, "Yeah there is one or more records that match.

Unless Mr. Application asks Mr. Count to tell him how many records there are, Mr. Count just says there are records returned in Mr. $Results Array

try something like this, (i have been working on stuff like this for my chat, so i know what i am doing ish -_-)
[php]$servername = “localhost”;
$username = “user”;
$password = “pass”;
$dbname = “dbname”;

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$sql = “SELECT col FROM table WHERE username=‘user1’”;
$result = $db->query(“SELECT col FROM table”) or die($db->error);
if($result->num_rows)
{
$rows = $result->fetch_all(MSQLI_ASSOC);
if($rows[‘bet’] > 0){
$bet = 0.001;
} else {
$bet = 0.000;
}
}
[/php] im assuming that bet is in ur table, which from the looks of it, there is no reason for the database unless it is displayed to everyone. but as they say… “cheff dont judge” -_- so ill just leave it. [‘bet’] tells the field of the inputed number. for me i use “inchat”, but here is a pic of what the field part looks in myphpadmin for me http://i.gyazo.com/e9f600dcfb4df28e2614f5dd11c7692a.png what ever u have set as the field for the bet, you should put inside the [] in my if statement that contains [php]$rows[‘bet’][/php] sorry if i confused u

edit: just found out someone who can teach this via video: https://www.thenewboston.com/videos.php?cat=87&video=19824 this is the newboston teaching u the values and what everything returns. also fetch_all isnt needed, i should have used fetch_assoc instead since its just one value

Sponsor our Newsletter | Privacy Policy | Terms of Service