用html回应php变量


Echoing php variables with html

我有以下代码,但在第7行一直遇到语法错误,说"分析错误:语法错误,意外的‘htmlentities’(T_STRING)。"这不是同时回显php和html的正确方法吗?感谢您的帮助!

<?php
$stmt = $pdo->query("SELECT first_name, last_name, department, website filename FROM Profile");
while ( $row = $stmt->fetch(PDO::FETCH_ASSOC) ) {
  echo('<div class="col-sm-3">
      <img class="img-rounded" src="'.$row['filename'].'"/>
    <p class="caption">');
  echo('<a href="'htmlentities($row['website'])'">'.($row['first_name']).' '.htmlentities($row['last_name']).'</a>')
  echo(', '.htmlentities($row['department']));
  echo('</p> </div>');
}
?>

您的echo应该没有括号,并且您丢失了。在你的一些回声命令上。

要连接回显字符串(连接),需要添加带有"."的字符串

请使用:

<?php
$stmt = $pdo->query("SELECT first_name, last_name, department, website filename FROM Profile");
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
   echo '<div class="col-sm-3">
   <img class="img-rounded" src="' . $row['filename'] . '"/>
   <p class="caption">';
   echo '<a href="' . htmlentities($row['website']) . '">' . ($row['first_name']) . ' ' . htmlentities($row['last_name']) . '</a>';
   echo ', ' . htmlentities($row['department']);
   echo '</p> </div>';
}
?>

当你像一样中断字符串时,你应该连接你的字符串

echo('<a href="' . htmlentities($row['website']) . '">'.($row['first_name']).' '

在htmlentities部分,您缺少字符串认证的点

您以错误的方式连接了字符串。尝试以下

<?php
    $stmt = $pdo->query("SELECT first_name, last_name, department, website, filename 
                    FROM Profile");
    while ( $row = $stmt->fetch(PDO::FETCH_ASSOC) ) 
    {
        echo '<div class="col-sm-3">
                <img class="img-rounded" src="'.$row['filename'].'"/>
                <p class="caption">';
        echo '<a href="'.htmlentities($row['website']).'">'.$row['first_name'].' '.htmlentities($row['last_name']).'</a>';
        echo ', '.htmlentities($row['department']);
        echo '</p> </div>';
    }
?> 

您也可以尝试以下替代方法。。

<?php
    $stmt = $pdo->query("SELECT first_name, last_name, department, website, filename 
                    FROM Profile");
    while ( $row = $stmt->fetch(PDO::FETCH_ASSOC) ) 
    {
        ?>
        <div class="col-sm-3">
            <img class="img-rounded" src="<?php echo $row['filename'] ?>"/>
            <p class="caption">
                <a href="<?php echo htmlentities($row['website']) ?>">
                    <?php echo $row['first_name'].' '.$row['last_name'] ?> 
                </a>
                <?php echo ', '.htmlentities($row['department']); ?>
            </p> 
        </div>
        <?php
    }
?> 

我编辑了你尝试的代码。你错过了。和

echo '<div class="col-sm-3">
      <img class="img-rounded" src="'.$row['filename'].'"/>
       <p class="caption">';
  echo '<a href="'.htmlentities($row['website']).'">'.($row['first_name']).' '.htmlentities($row['last_name']).'</a>';
  echo ', '.htmlentities($row['department']);
  echo '</p> </div>';