PHP Make my request array array identifier

So, I have this array of images, and the keys of the array are only 0,1,2,3,4,5 .... and what else ...

How can I make the value in the "id" column of this table a key and save the "link" as a 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" } 
+5
source share
2 answers

Just select the identifier and make it the key to the array. It is so 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]; } 

For your information, you should not use the mysql_* functions in the new code . They are no longer supported and are officially outdated . See the red box ? Instead, learn about ready-made statements and use PDO or MySQLi - this article will help you decide which ones. If you choose PDO, here is a good lesson .

You are also wide open for SQL injection

+3
source
  $myImages[$me['id']] = $row[0]; 
+1
source

Source: https://habr.com/ru/post/1480182/


All Articles