Itsproinc
Itsproinc

Reputation: 731

Checking variable in database via php (if statements)

So after a long rest of coding(around 5months) i started to forgot some of the codes and i need help with this one, i cant find in the google with this topic, etc

enter image description here

code :

<?php

$num1 = "0";

$database = mysql_connect('x', 'x', 'x') or die ("Error x01");
mysql_select_db('x') or die ("Error x02");

$SQL1 = "Select * FROM 'server_status' WHERE on = '$num1'";
$result_id1 = @mysql_query($SQL1) or die("DATABASE ERROR!");
$total1 = mysql_num_rows($result_id1);

if($result1){
   echo "Server is under maintenance";
}

?>

right here i have a code where i'am gonna check the variable "on" in "server_status" table in my msql enter image description here

somehow even when i have my "on" variable on 0 (int not bool [join_protection is on int too]) it still gives out the die which is

or die ("DATABASE ERROR!");

i can't find how to fix that i played around with it and not managed to make it work

here's the result enter image description here

i'm looking forward for your answer thanks for passing by and helping me

regards, -itsproinc

Upvotes: 0

Views: 77

Answers (1)

Funk Forty Niner
Funk Forty Niner

Reputation: 74217

You're using single quotes which are not the correct Identifier Qualifiers around your table name, remove them.

FROM server_status

or use ticks: (which resemble quotes, but are not the same).

FROM `server_status`

Plus, you are using a MySQL reserved word, being on for your column name and it needs to be wrapped in ticks.

$SQL1 = "Select * FROM `server_status` WHERE `on` = '$num1'";

Plus, as I stated in comments:

This doesn't help you or die ("DATABASE ERROR!"); this does mysql_error() and remove the @ in @mysql_query it's an error suppressor.


Deprecation notice:

mysql_ is deprecated and will be removed from future PHP releases.

Use mysqli_ or PDO.

Better yet:

Use mysqli with prepared statements, or PDO with prepared statements, they're much safer.

Upvotes: 3

Related Questions