Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if ID in database exists

Tags:

php

mysql

get

Why won't this send the user to user.php if the ID don't exist in the table?

<?php
include 'db_connect.php';
$id_exists = false;
$url_id = mysql_real_escape_string($_GET['id']);
$sql = "SELECT id FROM members WHERE id='$url_id'";
$result = mysql_query($sql);
 
if ($result == $url_id)
{
        $id_exists = true;
}
 
else if ($result != $url_id)
{
        $id_exists = false;
        header("location:user.php");
        exit();
}
 
?> 
like image 865
Kaizokupuffball Avatar asked Dec 04 '22 20:12

Kaizokupuffball


2 Answers

$url_id = mysql_real_escape_string($_GET['id']);
$sql = "SELECT id FROM members WHERE id='$url_id'";
$result = mysql_query($sql);

if(mysql_num_rows($result) >0){
   //found
}else{
   //not found
}

Try something like this :). http://php.net/manual/en/function.mysql-num-rows.php

like image 131
MrE Avatar answered Dec 07 '22 08:12

MrE


Please, read the docs for mysql_query. You have to fetch the result after making the query. That said, you should probably use PDO instead of mysql_*.

like image 26
mfonda Avatar answered Dec 07 '22 08:12

mfonda