1

Errors:

Notice: Trying to get property of non-object in C:\Users\Gustavs\Documents\EasyPHP\test2\register.php on line 16
Notice: Trying to get property of non-object in C:\Users\Gustavs\Documents\EasyPHP\test2\register.php on line 20 Code:

$exists = '';
$result = $connect->query('SELECT username FROM users WHERE username = '.$username.' LIMIT 1');
if ($result->num_rows == 1) {
    $exists = 'u';
}   
$result = $connect->query('SELECT email from users WHERE email = '.$email.' LIMIT 1');
if ($result->num_rows == 1) {
    $exists = 'e';
}

16 line:

if ($result->num_rows == 1) {

20 line:

if ($result->num_rows == 1) {
Rizier123
  • 56,111
  • 16
  • 85
  • 130
  • 2
    Your query failed but you did not check for errors. `if (!$result) echo $connect->error;` This is likely because the variable `$username` is not properly single-quoted inside the SQL string, `$email` likewise. – Michael Berkowski Jan 17 '15 at 19:48
  • If `$username` is a string value then it needs to be quoted as a string value in your query, else SQL will assume that it's a column name in your users table – Mark Baker Jan 17 '15 at 19:49
  • 1
    But better yet is if you use prepared statements/bind variables, when you don't need to use the quotes – Mark Baker Jan 17 '15 at 19:50
  • See [When to use single quotes, double quotes, backticks](https://stackoverflow.com/questions/11321491/when-to-use-single-quotes-double-quotes-and-backticks/11321508#11321508) for info on the quoting, but this issue can be sidestepped while also improving security by using [`prepare()/bind_param()/execute()`](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) in MySQLi. – Michael Berkowski Jan 17 '15 at 19:50

1 Answers1

0

Try this:

    $exists = '';
    if($result = $connect->query('SELECT username FROM users WHERE username = "'.$username.'" LIMIT 1')){
        if($result->num_rows == 1){
        $exists = 'u';
        }else{
            die;
        }
    }   
    if($result = $connect->query('SELECT email FROM users WHERE email = "'.$email.'" LIMIT 1')){
        if($result->num_rows == 1){
        $exists = 'e';
        }else{
           die;
        }
    }

Or depending on how you're checking if the query was successful you could do:

    $exists = '';
    if($result = $connect->query('SELECT username FROM users WHERE username = "'.$username.'" LIMIT 1')){
        if($result->num_rows == 1){
        $exists = 'u';
        }
        return true;
    }else{
        return false;
    }   
    if($result = $connect->query('SELECT email FROM users WHERE email = "'.$email.'" LIMIT 1')){
        if($result->num_rows == 1){
        $exists = 'e';
        }
        return true;
    }else{
        return false;
    }

Or even:

    $exists = '';
    if($result = $connect->query('SELECT username FROM users WHERE username = "'.$username.'" LIMIT 1')){
        if($result->num_rows == 1){
        $exists = 'u';
        }
        echo 'Success!';
    }else{
        echo 'Failure!';
    }   
    if($result = $connect->query('SELECT email FROM users WHERE email = "'.$email.'" LIMIT 1')){
        if($result->num_rows == 1){
        $exists = 'e';
        }
        echo 'Success!';
   }else{
        echo'Failure';
    }

I hope this helps!

Jason Bassett
  • 1,250
  • 7
  • 19
  • I edited my answer for proper error checking and some logic errors I had in the code as well. While you may not use any of the examples I provided other than the modifications I made to your original PHP, I want to make sure that anyone else who reads this post is able to benefit from it in various ways. :) – Jason Bassett Jan 17 '15 at 20:23