开发者

Array in function to replace with mysql data [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜