I'm writing a program that must check if a row with a unique "id" value exists or not in a table, and to either fetch the data from it if it does exist, or create the row if it does not exist.
I am relatively new to the MySQL syntax, and it causes me trouble so far.
What I tried to do is this:
Connect to the database;
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
$conn = mysqli_connect($servername, $username, $password, $dbname) or die("Connection failed: " . mysqli_connect_error());
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
I attempt to check whether a row with a particular unique value of one of the columns exists
$sql = "SELECT EXISTS(SELECT * FROM employee WHERE id=300)";
$result = mysqli_query($conn, $sql) or die("error to check the existence of the row");
if ($result = 1){echo "A row with current date does exist";}
else {echo "A row with current date does not exist";}
The problem with this check is that whether such a "42" value exists in the table or not, or any other value, for that matter, it always seems to return "1".
Obviously, this does not work.
But how is it supposed to be done?
Sorry if the question is stupid.