显示MySQL数据库作为一个数组 [英] Display MySQL Database as an array
本文介绍了显示MySQL数据库作为一个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个MySQL数据库充分的用户信息,像他们的用户名,密码,电子邮件等。
I have a MySQL database full of user information, like their username, password, email, etc.
我想一个PHP脚本,让我拉只是他们的用户名和像这样显示的:
I want a PHP script that allows me to pull JUST their username and display it like so:
用户名1,USERNAME2,USERNAME3
"username1","username2","username3"
从字面上完全一样,报价和所有。
Literally exactly like that, the quotes and all.
编辑:对不起,不提供足够的信息。
Sorry for not supplying enough information.
表被命名为用户的方面,我想拉断是用户名我可以把它拉并显示,我唯一的问题是它爆的所有信息。
The table is named "users" the field I want to pull off it is "username" I can get it to pull and display all the information, my only problem is imploding it.
推荐答案
OK纨绔子弟,阅读注释
OK dude, read the comments
<?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
?>
这篇关于显示MySQL数据库作为一个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文