From the code, I would say that $data2
represents the data of 1 line from the DB.
So, it should be $data2
that contains what you want to display.
Your foreach loop is useless : if you want to display the data from 1 line, you can just use $data2
directly.
If you want to go line by line, you must not iterate over $data2, but loop while mysql_fetch_array returns data.
Something like this, I'd say :
$result2 = mysql_query("SELECT id,fp_thumb,title FROM media") or die(mysql_error());
while ($data2 = mysql_fetch_array($result2)) {
echo '<li><a href="media.php?id='.$data2['id'].'"><img src="'.$data2['fp_thumb'].'" alt="'.$data2['title'].'" /></a></li>';
}
Also, you might want to escape the data your are outputing, to make sure you don't have any kind of HTML/javascript injection -- at least in the src and title, I'd say ; for that, you can use htmlspecialchars
Your echo would, then, probably look like this :
echo '<li><a href="media.php?id='
. $data2['id']
. '"><img src="'
. htmlspecialchars($data2['fp_thumb'])
. '" alt="'
. htmlspecialchars($data2['title'])
. '" /></a></li>';
(I considered id is an integer, auto-incremented or something like that, so not much of a risk)
Edit after seeing the comment on another answer
With what you first used :
$data2 is an associative array, that looks like this :
array
'id' => int 152
'title' => string 'this is the title' (length=17)
'fp_thumb' => string 'http://...../' (length=13)
Which means the foreach will loop three times, and $val will be :
int 152
string 'this is the title' (length=17)
string 'http://...../' (length=13)
(One value for each iteration)
ie, $val is not an array is a scalar value.
When you do that :
$a = 152;
var_dump($a['id']);
you get
null
When doing
$a = 'this is the title';
var_dump($a['id']);
you get :
string 't' (length=1)
ie, you only get the first character when using array-access on a string, with a key that is a string.
Note that array-access on a string, with a numeric key, is used to access one character of the array :
$a = 'this is the title';
var_dump($a[3]);
gets you :
string 's' (length=1)
For more informations, see the manual page about string, especially String access and modification by character, which states :
Characters within strings may be
accessed and modified by specifying
the zero-based offset of the desired
character after the string using
square array brackets, as in $str[42].
Think of a string as an array of
characters for this purpose.
And :
Non-integer types are converted to
integer.
And converting 'title' to an integer gives 0 ; so, you get the first character of the string.
Hope this helps, and you understood "why" ;-)