unl*_*ver 1 php mysql database arrays
I have a MySQL database full of user information, like their username, password, email, etc.
我想要一个PHP脚本,该脚本允许我仅提取其用户名并显示为:
“ username1”,“ username2”,“ username3”
从字面上看完全一样,引号和所有。
编辑:很抱歉没有提供足够的信息。
该表名为“用户”,我要提取的字段是“用户名”,我可以将其提取并显示所有信息,我唯一的问题是内爆。
好家伙,请阅读评论
<?php // open a php tag
$dbc = mysql_connect("host", "username", "password"); // connect to database
mysql_select_db("db_name", $dbc) // select the database
$sql = "SELECT `username` FROM `users_table`"; // select only the username field from the table "users_table"
$result = mysql_query($sql); // process the query
$username_array = array(); // start an array
while($row = mysql_fetch_array($result)){ // cycle through each record returned
$username_array[] = "\"".$row['username']."\""; // get the username field and add to the array above with surrounding quotes
}
$username_string = implode(",", $username_array); // implode the array to "stick together" all the usernames with a comma inbetween each
echo $username_string; // output the string to the display
?>
Run Code Online (Sandbox Code Playgroud)