简体   繁体   中英

What's wrong with this php foreaach code?

$db = new PDO("mysql:host=$hostname;dbname=$database",$username,$password);

$menus = 'SELECT menus FROM menus';

foreach($db->query($menus) as $row ){
  echo "<li class=\"main\"> $row['mainLink'] $row['menus'] </a> </li>";
}

$row['mainLink'] is equal to <a href='yada yada.php'> .

$row['menus'] is equals to yada yada .

The code give me this error:

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\xampp\htdocs\yada-yada\index.php on line 34

You can't do $row['mainlink'] inside a string "" .

Try either " . $row['mainLink'] . " or {$row['mainLink']}

我想下面的代码应该可行

printf('<li class="main">%s %s</a></li>', $row['mainLink'], $row['menus']);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM