A valid SQL-command won't run in PHP
I ran the following code in PHPAdmin's terminal:
SELECT COUNT( * )
FROM User
WHERE email = 'ijp'
and got result 0.
Then part of my PHP pro开发者_运维问答gram goes as follows:
$email = clean_input($_POST['email']);
$query = "SELECT COUNT(*) FROM user WHERE '.$email.'=email";
echo $query;
$result = mysqli_query($link, $query);
echo $result;
mysqli_close($link);
I have checked that I logging into the database went fine. Still the output is
SELECT COUNT(*) FROM User WHERE email='ijp'
Catchable fatal error: Object of class mysqli_result could not be converted to string in .../html/register.php on line 49
How can I run MySQL-commands in PHP?
You are passing the $result
resource to echo
which expects a string. You need to use one of the mysqli_fetch_*
functions to read the value from the $result
and then print it.
mysqli_fetch()
is deprecated, use mysqli_fetch_array
or mysql_fetch_assoc
or mysql_fetch_all
, for example:
$row = mysqli_fetch_array($result);
if($row) {
echo $row[0];
}
Try using
$query = "SELECT COUNT(*) FROM user WHERE email=\"".$email."\"";
If the value or $email
is 'ijp'
, the value of $query
after
$query = "SELECT COUNT(*) FROM user WHERE '.$email.'=email";
Would be...
SELECT COUNT(*) FROM user WHERE ijp=email
Use '$email'
instead of '.$email.'
all the above answers are all correct but one thing was ommited... you need to differentiate between a table called "User" and one called "user" - they are case-sensitive on *NIX systems (Linux and the like)
精彩评论