-1

I have been trying to create a sign up form for a website and I keep struggling with trying to check if a user's username had been used by someone else who signed up for the website in the past. I am using a Mysql database to store information and am trying to access past usernames through it using php. This specific part of my code keeps returning an error.

$query = "SELECT username FROM users";
$records = mysql_query("SELECT username FROM users");
$result = mysql_query($records);

$result_array = array();
while($row = mysql_fetch_assoc($result))
{
    $result_array[] = $row['username'];
}

The error messages I am receiving are:

  1. Warning: mysql_query() expects parameter 1 to be string, resource given in ...
  2. Warning: mysql_fetch_assoc() expects parameter 1 to be resource, null given in... thank you so much!
Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85
  • 1
    What's the error message you're receiving? – Aaron W. Jan 06 '16 at 03:51
  • what is the error message? – The Beast Jan 06 '16 at 03:51
  • Warning: mysql_query() expects parameter 1 to be string, resource given in ... and Warning: mysql_fetch_assoc() expects parameter 1 to be resource, null given in... – Walnut_Slayer Jan 06 '16 at 03:55
  • Show more code - that usually means you are either not connected to a database server, didn't select a database, or you have an error in your SQL – Aaron W. Jan 06 '16 at 03:57
  • The mysql_* extension is deprecated and doesn't ship anymore with the current php 7 release. Better [pick another API](http://docs.php.net/manual/en/mysqlinfo.api.choosing.php) to connect to your MySQL server. – VolkerK Jan 06 '16 at 03:57
  • Possible duplicate of [mysql\_fetch\_array() expects parameter 1 to be resource (or mysqli\_result), boolean given](http://stackoverflow.com/questions/2973202/mysql-fetch-array-expects-parameter-1-to-be-resource-or-mysqli-result-boole) – Abdulla Nilam Jan 06 '16 at 03:58
  • Better create a [unique index](http://dev.mysql.com/doc/refman/5.7/en/constraint-primary-key.html) for username and [check](http://docs.php.net/manual/en/mysqli.errno.php) for the [specific error code](https://dev.mysql.com/doc/refman/5.5/en/error-messages-server.html#error_er_dup_key) when a doublet is to be inserted. You avoid race conditions that way. – VolkerK Jan 06 '16 at 04:01

2 Answers2

3

The problem is you are doing mysql_query twice one on the $records variable and then on resultlike so:

$records = mysql_query("SELECT username FROM users");
$result = mysql_query($records);

What you need to do is this:

$records = "SELECT username FROM users";
$result = mysql_query($records);
The Beast
  • 1
  • 1
  • 2
  • 24
1

I hope the answer by Jack Smith was helpful. I'll just add that try to use mysqli_query as mysql is deprecated and may be removed in a newer version of php. You would also need to use mysqli_connect('host','usrname','password','database') instead of mysql_connect.

Abdul Basit
  • 394
  • 3
  • 12