I want to be able to store (not echo) some data that has been selected from a mysql database in a php array. So far, I have only been able to echo the information, I just want to be able to store it in an array for later use. Here is my code:
我希望能够存储(不回显)从php数组中的mysql数据库中选择的一些数据。到目前为止,我只能回应信息,我只是希望能够将它存储在一个数组*以后使用。这是我的代码:
$query = "SELECT interests FROM signup WHERE username = '$username'";
$result = mysql_query($query) or die ("no query");
while($row = mysql_fetch_array($result))
{
echo $row['interests'];
echo "<br />";
}
2 个解决方案
#1
19
You could use
你可以用
$query = "SELECT interests FROM signup WHERE username = '".mysql_real_escape_string($username)."'";
$result = mysql_query($query) or die ("no query");
$result_array = array();
while($row = mysql_fetch_assoc($result))
{
$result_array[] = $row;
}
This will basically store all of the data to the $result_array
array.
这基本上将所有数据存储到$ result_array数组中。
I've used mysql_fetch_assoc
rather than mysql_fetch_array
so that the values are mapped to their keys.
我使用了mysql_fetch_assoc而不是mysql_fetch_array,以便将值映射到它们的键。
I've also included mysql_real_escape_string
for protection.
我还包括mysql_real_escape_string以进行保护。
#2
2
You can "store" it by not accessing it from the result set until you need it, but if you really want to just take it and put it in a variable…
您可以通过不在结果集中访问它来“存储”它,直到您需要它为止,但是如果您真的想要把它放在一个变量中......
$query = "SELECT interests FROM signup WHERE username = '$username'";
$result = mysql_query($query) or die ("no query");
$interests = array();
while(false !== ($row = mysql_fetch_assoc($result))) {
$interests[] = $row;
}
#1
19
You could use
你可以用
$query = "SELECT interests FROM signup WHERE username = '".mysql_real_escape_string($username)."'";
$result = mysql_query($query) or die ("no query");
$result_array = array();
while($row = mysql_fetch_assoc($result))
{
$result_array[] = $row;
}
This will basically store all of the data to the $result_array
array.
这基本上将所有数据存储到$ result_array数组中。
I've used mysql_fetch_assoc
rather than mysql_fetch_array
so that the values are mapped to their keys.
我使用了mysql_fetch_assoc而不是mysql_fetch_array,以便将值映射到它们的键。
I've also included mysql_real_escape_string
for protection.
我还包括mysql_real_escape_string以进行保护。
#2
2
You can "store" it by not accessing it from the result set until you need it, but if you really want to just take it and put it in a variable…
您可以通过不在结果集中访问它来“存储”它,直到您需要它为止,但是如果您真的想要把它放在一个变量中......
$query = "SELECT interests FROM signup WHERE username = '$username'";
$result = mysql_query($query) or die ("no query");
$interests = array();
while(false !== ($row = mysql_fetch_assoc($result))) {
$interests[] = $row;
}