PHP

usama_rasab27

Member
Messages
208
Reaction score
15
Points
18
What does this mean in PHP:
Code:
if(!$email)
{
}
else{
}

Why is there a ! before $result it comes after mysql_query and how do you echo a single record from a database, I have tried

PHP:
$code = ("SELECT email FROM users WHERE username = 'username', $mysqlconnection"):

echo $code;

But it just gives me a blank white page without errors. I have made all the database connections before this.

Please could someone help please. Thank You.
 

garrettroyce

Community Support
Community Support
Messages
5,609
Reaction score
250
Points
63
the ! (not) operator will return the opposite of the boolean (true/false) value of the operand (in this case $email). I'm guessing $email is a string, so if it is an empty string or the string "0", then $email is FALSE and !$email is TRUE.

PHP:
// WARNING
// all of the mysql_**** functions are DEPRECATED and will be removed from PHP!
// do not use them!
// use mysqli_**** or PDO!

$code = mysqli_query("SELECT email FROM users WHERE username = 'username', $mysqlconnection"); // this is what I think you mean

// note that mysql_query can return FALSE.
// if you echo FALSE, you get nothing
// it can also return a RESOURCE, which is not printable
// using var_dump would give you more information about the actual value of $code

var_dump($code); // will probably print "bool(false)" meaning your query has failed

echo mysqli_error(); // this is probably what you want to know, but you should only use this for testing and not on a real website
 

iSean

Member
Messages
60
Reaction score
3
Points
8
I have had problems with SQL Query syntax with recent updates to PHP etc. It always worked when I added `` around the name of things like so:
PHP:
$code = mysqli_query("SELECT * FROM `users` WHERE `username` = 'username'");

Also, all you are doing is echo'ing that query, not actually processing the query, you need a little but more code for that like so:

PHP:
$code = "SELECT * FROM `users` WHERE `username` = 'username'";
$result = mysqli_query($code);

while($row = mysqli_fetch_assoc($result) {
    echo $row['username'];
}


If you are trying to echo a boolean value ie if the user exists or not you might need this code

PHP:
$code = "SELECT * FROM `users` WHERE `username` = 'username'";
$result = mysqli_query($code);

$number = mysqli_num_rows($result);
if($number == 1) {
    echo "user exists";
}
else {
   echo "user does not exist";
}

I hope this helps.
 
Last edited:

garrettroyce

Community Support
Community Support
Messages
5,609
Reaction score
250
Points
63
Backticks can be used around names of tables, databases, and columns to avoid having to escape characters that are valid in these names, but can alter the query syntax.

For example, if you had a table named "user names", then you should escape it with backticks otherwise your query will fail

Code:
SELECT * FROM user names #invalid syntax; you are selecting from a table called "user" and "names" is part of the query
SELECT * FROM `user names` # valid syntax

The backticks have no effect on the query otherwise. They are an optional part of the SQL syntax.

@iSean there is a syntax error in your first code block; you forgot a quote at the end
 
Top