Check if a value exists in MySQL with php -


i've been looking around stackoverflow , wasn't able ever find way that'd work. have simple php application

//database credentials $servername = "localhost"; $username = "root"; $password = "password"; $database = "database";  // create connection database $db = new mysqli($servername, $username, $password, $database);  // check connection errors if ($db->connect_error) {     die("<h1>connection database failed: " . $db->connect_error) . "</h1>"; };  $username = $json['statuses'][0]['user']['screen_name']; $userid = $json['statuses'][0]['user']['id_str'];  $sql = "select * log userid='" . $userid . "' limit 1";  if ($db->query($sql)->num_rows > 0) {     echo "<h4>this user exists</h4>"; } else {     //put userid database     $sql = "insert log (userid) values ('" . $userid . "')";      if ($db->query($sql) === true) {         echo "<h4>added " . $username . " database</h4>";     } else {         echo "error: " . $sql . "<br>" . $db->error;     } } 

currently seems hit or miss. it'll work sometimes, other times record exist, , it'll still insert userid again creating duplicates.

phpmyadmin

like said @tadman code bad. data variable $json directly inserted query - not good...

simple test:

i set :

 $userid = "111111111a"; 

query:

 $sql = "select * log userid='111111111a' limit 1"; 

return true because, user doesn't exists in db,

or

 $userid ='111111111\' or \'1=1'; 

query:

 $sql = "select * log userid='111111111' or '1=1' limit 1"; 

return true because 1=1 true.

if column userid int type, $userid value converted 111111111 , inserted log table


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -