/*mysql table admin
id=1, user_name = mike
id=2, user_name = sam*/
$user_name_query = mysql_query("select id, user_name from admin");
$user_name_array = array();
while ($row = mysql_fetch_array($user_name_query)) {
$user_name_array[] = array('user' => $row['user_name']);
}
foreach($user_name_array as $key => $record){
$record_values .= "{$record['user']},";
}
$record_values = substr($record_values, 0, -1);
// $record_values is: mike,sam
$userThatHaveThePermission = array($record_values);
if (in_array(mike, $userThatHaveThePermission)){
echo "do something"
}
我的问题是关于变量 $ record_values 这个变量不起作用的值:$ userThatHaveThePermission = array($ record_values); 如果我将用户名(mike,sam)添加到$ userThatHaveThePermission = array( mike,sam );一切正常,为什么?我错过了什么。
答案 0 :(得分:0)
我认为你是php-mysql的初学者。我会为你重构你的代码。
$user_name_query = mysql_query("select id, user_name from admin");
$user_name_array = [];
while ($row = mysql_fetch_array($user_name_query)) {
$user_name_array[] = $row['user_name'];
}
$userThatHaveThePermission = $user_name_array;
if (in_array('mike', $userThatHaveThePermission)){
echo "do something";
}
但请听取评论你身份的人的意见。不要使用mysql_query
。使用一些框架。