-2

This is my code:

$u_check = mysql_query("SELECT username FROM users WHERE usnername='$un' '");
$check = mysqli_num_rows ( $u_check );

I get the following error:

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\Socially\index.php on line 27

I can't fix this, i am using a tutorial to help me do something, and this is what he typed. I really want to fix it so i can continue what I want to do; I'm doing the database because I am making a login and register form. Please help. When I type $check = mysqli_num_rows ( $u_check ); into the sql tab in phpmyadmin, i get this message:

Static analysis:

3 errors were found during analysis.
Unexpected character. (near "$" at position 0)
Unexpected character. (near "$" at position 25)
Unexpected beginning of statement. (near "$" at position 0)

SQL query:
$check = mysqli_num_rows($u_check)

MySQL said: Documentation

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use

near '$check = mysqli_num_rows($u_check)' at line 1

Phil Hord
  • 12,780
  • 1
  • 26
  • 30
adam hope
  • 3
  • 6
  • 2
    Aren't you mixing mysql_* with mysqli_ *? – michaJlS May 25 '16 at 20:33
  • And also confusing PHP and SQL, it seems. – showdev May 25 '16 at 20:38
  • I see a typo in your first line: `username` vs `usnername`. That could throw and error and result in a boolean value. – showdev May 25 '16 at 20:40
  • I don't really know PHP, but it seems like that tutorial is encouraging a coding style that permits SQL injection (e.g., "username='$un'"): http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php – rrauenza May 25 '16 at 20:52

2 Answers2

0

You are mixing MySQL with MySQLi

If you are using MySQL:

$u_check = mysql_query("SELECT username FROM users WHERE usnername='$un'");
$check = mysql_num_rows ($u_check);

If you are using MySQLi:

$u_check = mysql_query($con, "SELECT username FROM users WHERE usnername='$un'");
$check = mysqli_num_rows ($u_check);

(Replace $con with your database parameter if you're using MySQL)

It's also possible that you meant username in your query rather than usnername.

The Codesee
  • 3,714
  • 5
  • 38
  • 78
  • Thanks! I appreciate it. – adam hope May 26 '16 at 14:53
  • I have been getting another error. When i type this: else { $pswd = md5($pswd); $pswd2 = md5($pswd2); $query = mysql_query("INSERT INTO users VALUES (' ', '$un', '$fn', '$ln', '$em','$pswd','$d','0')"); } I get an error message: Parse error: syntax error, unexpected 'else' (T_ELSE) in C:\xampp\htdocs\Socially\index.php on line 39 – adam hope May 26 '16 at 15:16
  • Can you post line 39? – The Codesee May 26 '16 at 15:17
  • Your error points to line 39 on index.php. Could you paste that line here? – The Codesee May 26 '16 at 16:07
  • So paste the else line somewhere else? I moved it down a bit, still got an error. Do you want the line that i'm getting an eror on pasted in here? I got you – adam hope May 26 '16 at 16:11
  • Have any way i could send you a screenshot? Here's the code that the tutorial showed me to use: else { if (strlen($pswd)>30||strlen($pswd)<5){} echo "Your password be between 5 and 30 characters long!"; } else { $pswd = md5($pswd); $pswd2 = md5($pswd2); $query = mysql_query("INSERT INTO users VALUES (' ', '$un', '$fn', '$ln', '$em','$pswd','$d','0')"); } Here's the error: Parse error: syntax error, unexpected 'else' (T_ELSE) in C:\xampp\htdocs\Socially\index.php on line 39 – adam hope May 26 '16 at 16:16
  • You can't start with an else statement - is that the complete code? – The Codesee May 26 '16 at 16:28
  • No. That's the two else statements – adam hope May 26 '16 at 16:45
  • I'm finding it hard to understand your code posted in a comment. Why not create a new question and link it to me? – The Codesee May 26 '16 at 16:45
  • I would send a screenshot if i could – adam hope May 26 '16 at 16:46
  • How would i do that? Its says i can't ask another question – adam hope May 26 '16 at 16:46
  • Do have some sort of gmail? – adam hope May 26 '16 at 18:04
-1

Replace:

$u_check = mysql_query("SELECT username FROM users WHERE usnername='$un' '");

With:

$u_check = mysqli_query("SELECT username FROM users WHERE usnername = $un");

You always put variables in double quotes, otherwise the script thinks what you are writing is a string, not a variable.

The Codesee
  • 3,714
  • 5
  • 38
  • 78
Mihail Ivanchev
  • 403
  • 6
  • 22
  • What about $check = mysql_num_rows ( $u_check ); When i just paste the correction in you gave me, i keep getting an error message: Warning: mysqli_query() expects at least 2 parameters, 1 given in C:\xampp\htdocs\Socially\index.php on line 26 Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in C:\xampp\htdocs\Socially\index.php on line 27 – adam hope May 26 '16 at 14:48
  • That SQL is invalid, the `$un` is a string so it needs to be quoted when it gets to the DB. `mysqli_query` requires a connection string. – chris85 May 28 '16 at 13:46