Array in function to replace with mysql data [closed]
I have hardcoded array called users
in a PHP function:
protected static $users = array(
'us1' => array('password'=>"pass1", 'fullname'=>"name1", 'email'=>"name1@mail.com"),
'us2' => array('password'=>"pass2", 'fullname'=>"name2", 'email'=>"name2@mail.com"),
'us3' => array('password'=开发者_C百科>"pass3", 'fullname'=>"name3", 'email'=>"name3@mail.com"),
);
I need to connect this part of function to my users
table in MySql so I dont have to type new users here in function.
I tried the following, but it gives me an error:
$sql = mysql_query('select username, password, fullname, email FROM s_users');
$users = array()
while ($row_user = mysql_fetch_assoc($sql))
$users[] = $row_user;
How can this be fixed?
Try something like this:
while ($row_user = mysql_fetch_assoc($sql)) {
$users[$row_user['username']] = $row_user;
}
And make sure to first check that $sql
is valid (if (!$sql) { // error processing }
), and that you print out the error if $sql
is invalid (using mysql_error
).
精彩评论