Skip to content

Instantly share code, notes, and snippets.

@m0k1
Created May 15, 2015 07:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save m0k1/ee836fdac188f99e9b01 to your computer and use it in GitHub Desktop.
Save m0k1/ee836fdac188f99e9b01 to your computer and use it in GitHub Desktop.
Check if ID exist in database
<?php
$mysql_server = "localhost";
$mysql_user = "username";
$mysql_password = "password";
$mysql_dbname = "db";
$mysqlcon = new mysqli($mysql_server, $mysql_user, $mysql_password, $mysql_dbname);
if ($mysqlcon->connect_error) {
die("Connection failed: " . $mysqlcon->connect_error);
}
$id = mysqli_real_escape_string($mysqlcon,$_GET['id']);
$query = "SELECT id FROM members WHERE id='$id'";
if ($mysqlcon->query($query) != TRUE) {
echo "Error: " . $query . "<br>" . $mysqlcon->error;
}
if ($result = mysqli_query($mysqlcon, $query))
{
$row_cnt = mysqli_num_rows($result);
if($row_cn > 0)
{
echo "The ID ".$id." is found in database (exist)";
}
else
{
echo "The ID ".$id." is not found in database (not exist)";
}
mysqli_free_result($result);
}
mysqli_close($mysqlcon);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment