我如何从php中的查询创建数组

时间:2021-02-28 12:55:23

in my data base i have a table "options" which contains options from my site like title, language etc.

在我的数据库中,我有一个表“选项”,其中包含我的网站的选项,如标题,语言等。

i want to load all this options and store it in a array query the database.

我想加载所有这些选项并将其存储在数组查询数据库中。

this code works manual

此代码工作手册

$option=array("title" => "Site's title", "option2" => "option 2 value");
echo $option[title];

but when i make the query...

但是当我进行查询时......

$query_options=mysql_query("SELECT * FROM options");

while($data_options = mysql_fetch_row($query_options)){

    $option=array($data_options[1] => $data_options[2]);


}

echo $option[title];

it doesn't work.

它不起作用。

Hope you can help me.

希望您能够帮助我。

Thank you

2 个解决方案

#1


0  

mysql_fetch_row() only allows you to access columns in your query based on the column ordering : $options[0] or $options[1].

mysql_fetch_row()只允许您根据列顺序访问查询中的列:$ options [0]或$ options [1]。

Use mysql_fetch_array() instead. With it you can access data with $options["title"] for example. Using the column name.

请改用mysql_fetch_array()。有了它,您可以使用$ options [“title”]访问数据。使用列名称。

See the doc at http://www.php.net/manual/fr/function.mysql-fetch-array.php

请参阅http://www.php.net/manual/fr/function.mysql-fetch-array.php上的文档

PS - Don't forget to put title between quotes too!

PS - 别忘了把标题放在引号之间!

#2


0  

$query_options=mysql_query("SELECT * FROM options");
$array = array();

while($data_options = mysql_fetch_array($query_options)){

    $array[]["title"] = $data_options['title'];
    $array[]["value"] = $data_options['value'];

}

var_dump($array);

Should work. lemme know how it goes.

应该管用。 lemme知道它是怎么回事。

Also, you should know that you shouldn't use any mysql_* commands, they're becoming depreciated as they're in-secure, you should take a look here

另外,你应该知道你不应该使用任何mysql_ *命令,因为它们是安全的,它们会被折旧,你应该看看这里

http://www.php.net/manual/en/ref.mysql.php

and

http://www.php.net/manual/en/ref.mysqli.php

#1


0  

mysql_fetch_row() only allows you to access columns in your query based on the column ordering : $options[0] or $options[1].

mysql_fetch_row()只允许您根据列顺序访问查询中的列:$ options [0]或$ options [1]。

Use mysql_fetch_array() instead. With it you can access data with $options["title"] for example. Using the column name.

请改用mysql_fetch_array()。有了它,您可以使用$ options [“title”]访问数据。使用列名称。

See the doc at http://www.php.net/manual/fr/function.mysql-fetch-array.php

请参阅http://www.php.net/manual/fr/function.mysql-fetch-array.php上的文档

PS - Don't forget to put title between quotes too!

PS - 别忘了把标题放在引号之间!

#2


0  

$query_options=mysql_query("SELECT * FROM options");
$array = array();

while($data_options = mysql_fetch_array($query_options)){

    $array[]["title"] = $data_options['title'];
    $array[]["value"] = $data_options['value'];

}

var_dump($array);

Should work. lemme know how it goes.

应该管用。 lemme知道它是怎么回事。

Also, you should know that you shouldn't use any mysql_* commands, they're becoming depreciated as they're in-secure, you should take a look here

另外,你应该知道你不应该使用任何mysql_ *命令,因为它们是安全的,它们会被折旧,你应该看看这里

http://www.php.net/manual/en/ref.mysql.php

and

http://www.php.net/manual/en/ref.mysqli.php