1 character only foreach loop result in PHP

Hello, I have this foreach loop that gives a weird result, it only displays the first character form of the db entry

<?php
$result2 = mysql_query("SELECT id,fp_thumb,title FROM media") or die(mysql_error());
$data2 = mysql_fetch_array($result2) or die(Mysql_error());

foreach ($data2 as $val) {

  echo '<li><a href="media.php?id='.$val['id'].'"><img src="'.$val['fp_thumb'].'" alt="'.$val['title'].'" /></a></li>';
}
?>

and this is my db structure

SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for media
-- ----------------------------
CREATE TABLE `media` (
  `id` int(11) NOT NULL auto_increment,
  `thumb` varchar(500) NOT NULL,
  `url` varchar(500) NOT NULL,
  `fp_thumb` varchar(500) NOT NULL,
  `title` varchar(500) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records 
-- ----------------------------
INSERT INTO `media` VALUES ('1', '22', 'http://goeshere.com', '/images/slideshow/ctmbs.jpg', 'Test 1');
INSERT INTO `media` VALUES ('2', '2', 'http://goeshere1.com', '/images/slideshow/hitlex.jpg', 'test 2');
INSERT INTO `media` VALUES ('3', '3 ', 'http://goeshere2.com', '/images/slideshow/tsord.jpg', 'test 3');

Any information would be helpful. Thanks in advance and welcome ^^

+3
source share
4 answers

From the code, I would say that it $data2represents data from 1 row from the database.

So, it should be $data2that contains what you want to display.

Your foreach loop is useless: if you want to display data from 1 row, you can simply use $data2directly.


, $data2, , mysql_fetch_array .

- , :

$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>';
}

, , , , - HTML/javascript- - , src title, ; htmlspecialchars

, , :

echo '<li><a href="media.php?id='
    . $data2['id']
    . '"><img src="'
    . htmlspecialchars($data2['fp_thumb'])
    . '" alt="'
    . htmlspecialchars($data2['title'])
    . '" /></a></li>';

( , id , - , )


, :

$data2 - , :

array
  'id' => int 152
  'title' => string 'this is the title' (length=17)
  'fp_thumb' => string 'http://...../' (length=13)

, foreach , $val :

int 152
string 'this is the title' (length=17)
string 'http://...../' (length=13)

( )

.. $val , .


:

$a = 152;
var_dump($a['id']);

null

$a = 'this is the title';
var_dump($a['id']);

:

string 't' (length=1)

.. , .


, :

$a = 'this is the title';
var_dump($a[3]);

:

string 's' (length=1)


. , , :

, , $str [42]. .

:

.

"title" 0; .


, , ""; -)

+4

, :

$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>';
}
?>

. , ?

, mysql_fetch_array , , . FALSE, .

+2

$results mysql_fetch_array, :

while ($val = mysql_fetch_array($result2, MYSQL_ASSOC)) {
  echo '<li><a href="media.php?id='.$val['id'].'"><img src="'.$val['fp_thumb'].'" alt="'.$val['title'].'" /></a></li>';
}
0

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


All Articles