PHP - 让我查询的数组键ID [英] PHP - Make my query's array's key the ID
问题描述
所以我有这个阵列中拔出图片和数组的键只是0,1,2,3,4,5 ....和诸如此类的东西...
我怎样才能使该表中的关键的ID列中的值,并保持链接作为值。
关联数组,不是吗?
这是我的PHP:
$ myImageID = $我['imageid'];$ findImages =SELECT FROM WHERE图像= MODEL_ID'{$我['身份证']}链接;
$ imageResult =的mysql_query($ findImages)或死亡(mysql_error());$ myImages =阵列();
而($行= mysql_fetch_array($ imageResult)){
$ myImages [] = $行[0];
}
这是我有:
{
[0] - GT; http://website.com/link.jpg
[1] - > http://website.com/li123nk.jpg
[2] - > http://website.com/link3123.jpg
}
这就是我想要的:
{
[47] - GT; http://website.com/link.jpg
[122] - GT; http://website.com/li123nk.jpg
[4339] - GT; http://website.com/link3123.jpg
}
只需选择ID,并使其成为关键阵列。就这么简单。
$ findImages =SELECT ID,从图像中的WHERE MODEL_ID ='{$我['身份证']}链接;
$ imageResult =的mysql_query($ findImages)或死亡(mysql_error());$ myImages =阵列();
而($行= mysql_fetch_array($ imageResult)){
$ myImages [$行[0] = $行[1];
}
仅供参考,你不应该在新的$使用的mysql _ *
功能C $ç。他们不再维护并正式去precated 。请参阅红色框?了解的 prepared语句的,而是和使用的 PDO 或的MySQLi - 的这篇文章将帮助您决定哪。如果您选择PDO,是一个很好的教程。
您还敞开 SQL注入
So I have this array of images pulling and the array's keys are just 0,1,2,3,4,5.... and whatnot...
How can I make the value in the 'id' column of that table the key, and keep 'link' as the value.
Associative Array, no?
Here is my PHP:
$myImageID = $me['imageid'];
$findImages = "SELECT link FROM images WHERE model_id ='{$me['id']}'";
$imageResult = mysql_query($findImages) or die (mysql_error());
$myImages = array();
while($row = mysql_fetch_array($imageResult)) {
$myImages[] = $row[0];
}
Here is what I have:
{
[0] -> "http://website.com/link.jpg"
[1] -> "http://website.com/li123nk.jpg"
[2] -> "http://website.com/link3123.jpg"
}
Here is what I want:
{
[47] -> "http://website.com/link.jpg"
[122] -> "http://website.com/li123nk.jpg"
[4339] -> "http://website.com/link3123.jpg"
}
Just select the id and make it the key to the array. It's that simple.
$findImages = "SELECT id, link FROM images WHERE model_id ='{$me['id']}'";
$imageResult = mysql_query($findImages) or die (mysql_error());
$myImages = array();
while($row = mysql_fetch_array($imageResult)) {
$myImages[$row[0]] = $row[1];
}
FYI, you shouldn't use mysql_*
functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.
You also wide open to SQL injections
这篇关于PHP - 让我查询的数组键ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!