1

So I want to get all of the links of the images in my database:

$findMyImages = "SELECT link FROM images WHERE model_id ='{$me['id']}'";
$imageResult = mysql_query($findMyImages) or die (mysql_error());

$result_array = array();
while($row = mysql_fetch_array($imageResult))
{
    $result_array[] = $row;
}

print_r($result_array);

The print_r(); returns this:

Array ( 
    [0] => Array (
        [0] => http://scoutsamerica.com/uploads/529746_10200706796941357_1747291081_n.jpg 
        [link] => http://scoutsamerica.com/uploads/529746_10200706796941357_1747291081_n.jpg
    )
    [1] => Array (
        [0] => http://scoutsamerica.com/uploads/64311_10200924054292655_1770658989_n.jpg 
        [link] => http://scoutsamerica.com/uploads/64311_10200924054292655_1770658989_n.jpg
    )
)

I'm looking for something similar to:

Array ( 
    [0] => http://scoutsamerica.com/uploads/529746_10200706796941357_1747291081_n.jpg 
    [1] => http://scoutsamerica.com/uploads/64311_102n_image.jpg 
    [2] => http://scoutsamerica.com/uploads/face.jpg
    [3] => http://scoutsamerica.com/uploads/another_image.jpg 
)

How can I do that?

3
  • I know I am NOT looking for mysql_fetch_assoc(); I want the key's (they're called keys, no?) to be a integer index. Commented May 1, 2013 at 23:52
  • 1
    * PDO rant here * Commented May 1, 2013 at 23:54
  • Yes, I agree with @moonwave99, check out PDO, its much more secure and up to date. Development for mysql_ has seized and therefore its outdated and vulnerable to attacks. Commented May 2, 2013 at 0:05

3 Answers 3

3

Its because you are adding the result array to a new array. Simply select the information you want from the result array and place that in a new array.

For example:

while($row = mysql_fetch_array($imageResult))
{
    $result_array[] = $row[0];
}

OR:

while($row = mysql_fetch_array($imageResult))
{
    $result_array[] = $row['link'];
}
Sign up to request clarification or add additional context in comments.

1 Comment

Great! Both will work :) But in the future, the 0 will select the first returned value from the database, while the 'link' will return the value from the column called 'link'. So 'link' might be slightly more accurate if you're planning on making database changes later.
1

Attach element by element:

$result_array[] = $row[0];
// $result_array[] = $row[1]; this is the one you want to get rid of

Comments

1

Specify you just want numeric, not both:

$row = mysql_fetch_array($imageResult, MYSQL_NUM)[0];

or if you are on an older version of php:

$row = mysql_fetch_array($imageResult, MYSQL_NUM);
$row = $row[0];

The default is:

array mysql_fetch_array ( resource $result [, int $result_type = MYSQL_BOTH ] )

You see in brackets it says both, which tells it to give you both associative and numeric. You have to specify which one you want if you do not want that.

1 Comment

Nope, that still gives me an array of 2 arrays, theres is now only to elements within each array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.