How to check if value exists in MySQL Database in PHP?
database, mysql, php
Solution
If database is Fily_Registrations then the query is wrong. Try this:
$email = mysql_real_escape($_GET["email"]);
$DataBase = "Fily_Registrations";
mysql_connect("server", "username", "password") or die(mysql_error());
mysql_select_db($DataBase) or die(mysql_error());
$query = "SELECT * FROM users WHERE email = '{$email}'";
$result = mysql_query($query);
echo (mysql_num_rows($result) == 0) ? 'NO' : 'YES';
mysql_query returns false if query is not correct. Read first https://www.php.net/mysql_query and consider using PDO (https://www.php.net/pdo) instead of normal mysql_query.
Problem
I am new to PHP but I like to create a script that checks if an email is in my MySQL Database, the Database name is `"Fily_Registrations"`, the table is "users" and the value is called `"email"`. So basically if an the email `"hi@all.com"` exists in my database and I call the php script like `"http://path/to/php.php?email=hi@all.com"` it should echo out `"YES"` if it don't exists it should echo out `"NO"`. This is how fare I am now, but it always echoes out "NO": ``` <?php $email = $_GET["email"]; $DataBase = "Fily_Registrations"; mysql_connect("server", "username", "password") or die(mysql_error()); mysql_select_db($DataBase) or die(mysql_error()); $string = sprintf("SELECT '$DataBase' FROM users WHERE email = '$email'"); $query = msql_query($string); if($query == false) { echo("No"); } else { echo("Yes"); } ?> ``` Does anyone know how to fix this?