can someone tell me why mysql_result($)
return me 3 instead of 1 or 0, and if there is a better function to verify if the retrieve function SELECT
return something (if the id exists) and how to store it on a variable. Thank you.
require_once __DIR__ . '/db_connect.php';
$db = new DB_CONNECT();
$select = mysql_query("SELECT `id` FROM `users` WHERE `username`='$username' AND `password`='$password'");
echo mysql_result($select,0);
if (mysql_result($select, 0) == '1'){
echo "True";
}else
echo "False";
I would do it this way:
require_once DIR . '/db_connect.php';
$db = new DB_CONNECT();
$select = mysql_query("SELECT id
FROM users
WHERE username
='$username' AND password
='$password'");
if (mysql_num_rows($select) == '1'){
echo "True";
}else
echo "False";
If you need to return the value of the result then use different approach.
$db = new DB_CONNECT();
$select = mysql_query("SELECT `id` FROM `users` WHERE `username`='$username' AND `password`='$password'");
$result = mysql_result($select);
echo var_dump($result); // will help you debug the your queries,
Also, check if you have set auto increment to your table, maybe you have same username/password with the same id.
You can use mysql_num_rows, it will return the number of rows in the query, i leave you an example here:
public function numberRows($id) {
$result = mysql_query("SELECT * from table WHERE id = '$id'");
$no_of_rows = mysql_num_rows($result);
if ($no_of_rows > 0) {
// there is data
return true;
} else {
// there is no data
return false;
}
}
I think @php NoOb had a point:
require_once __DIR__ . '/db_connect.php';
$db = new DB_CONNECT();
$select = mysql_query("SELECT count(distinct `id`) FROM `users` WHERE `username`='$username' AND `password`='$password'");
echo mysql_result($select,0);
if (mysql_result($select, 0) == '1'){
echo "True";
}else
echo "False";
n.b. DISTINCT and COUNT. the rest should work around this. might help. Cheers.