0

I trying to check if a username is already in use in my db but it's not echoing anything (blank page when i run ) , i want to get a true or false response

<?php

 mysql_connect("localhost", "root", "") or die(mysql_error()); 

 mysql_select_db("data1") or die(mysql_error()); 


$usercheck = "john";

    $sanitizeduser= filter_var($usercheck, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW);

    $check = mysql_query("SELECT EXISTS(SELECT 1 FROM users WHERE username = $sanitizeduser)");

    echo $check;

?>
2
  • Change mysql, to mysqli and see my answer below Commented Feb 5, 2014 at 17:23
  • 1
    any way to just get a true/false response . It user exists - $check = false ; else $check = true; Commented Feb 5, 2014 at 17:38

2 Answers 2

3

You have shuffled with mysqli_query and mysql_ functions

Try this,

$link = mysqli_connect("localhost","root","","data1") or die("Error " . mysqli_error($link));
$usercheck = "john";

$sanitizeduser= filter_var($usercheck, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW);

$result = $link->query("SELECT username FROM users WHERE username = '".$sanitizeduser."' ");

$row_cnt = $result->num_rows;    
if($row_cnt>0){     
    echo "User Exists"; // echo 1;
}else{      
    echo "No User found"; //echo 0;
}
Sign up to request clarification or add additional context in comments.

9 Comments

jsfiddle -Only to test your JavaScript, CSS, HTML or CoffeeScript, not php!. BTW, you can use phpfiddle
tried the code above - seems to work - any way to check if result is true or false . I tried to echo result - got an error message (cannot convert to string)
To retrieve the results, You need use ex: mysqli_fecth_array() function
its not possible to stick with mysql - it has better syntax (edited code in the main post ) - just need a true/false whether the user exists or not
Returns FALSE on Query failure and return TRUE for successful queries, it doesn't mean user exist or not!!
|
2

You are mixing mysql and mysqli:

So your problem is just a typo, just change the function name:

<?php

$link = mysqli_connect("localhost", "root", "password","data1"); 

if (!$link){
    // error handling goes here: mysqli_error()
}
$usercheck = "john";

$sanitizeduser= filter_var($usercheck, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW);

$check = mysqli_query($link,"SELECT EXISTS(SELECT 1 FROM users WHERE username = $sanitizeduser) as check");
?>

Also to display the result you can't just use echo $check, you will have to use something like mysqli_fecth_array for example. See the manual

Example:

while($row = mysqli_fecth_array($check)) {
  echo $row["check"] . "<br>";
} 

5 Comments

Warning: mysqli_select_db() expects exactly 2 parameters, 1 given in C:\xampp\htdocs\folder\checkuser.php on line 5 .
this line ; mysqli_select_db("data1") or die(mysql_error());
@John_Nil you are right, see my update, you will have to pass the connection object to your select_db statement. or even better, pass your db name in the connection
Warning: mysqli_query() expects at least 2 parameters, 1 given in C:\xampp\htdocs\folder\checkuser.php on line 12 Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in C:\xampp\htdocs\folder\checkuser.php on line 15
Ok, i updated my code. the mysqli_query function takes two parameters, the connection object, and the query. Here is what I suggest, When you have an error like this, just look at the function name in the manual.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.