简体   繁体   English

PHP代码未执行

[英]PHP code not executing

$sql1=mysql_query("SELECT * FROM Persons", $con);

echo "<table border="3">
        <tr>
        <th>Name</th>
        <th>Age</th>
        </tr>";
while($info=mysql_fetch_array($sql1))
 {
        echo "<tr>";
        echo "<td>" . $info['fname'] . "</td>";
        echo "<td>" . $info['age'] . "</td>";
        echo "</tr>";
 }
echo "</table>";

this code is a part of a code which is trying to retrieve data from the table"Persons", there is some error in this part of the code.. 此代码是试图从表“ Persons”中检索数据的代码的一部分,此部分代码中有一些错误。

You have double quotes in the quoted html. 您在带引号的html中使用双引号。 Try using single quotes in stead, ie 尝试代替使用单引号,即

echo "<table border='3'> <--- here
        <tr>
        <th>Name</th>
        <th>Age</th>
        </tr>";

Your code looks ok, except for the unescaped double quotes: 您的代码看起来不错,除了未转义的双引号:

It should be: 它应该是:

echo "<table border=\"3\"> ... ";

or 要么

echo '<table border="3"> ... ';

Make sure it is enclosed in <?php and ?> . 确保将其包含在<?php?>

Also make sure your db columns names of fname and age really exist.... 还要确保您的数据库列名称fnameage确实存在。

Make sure you're getting what you think back from the DB, by using print_r($info) or var_dump($info) . 通过使用print_r($info)var_dump($info)确保从数据库中得到您的回想。

Finally, your connection $con could be broken / not working. 最后,您的连接$con可能损坏/无法正常工作。 You can check that by using: 您可以使用以下方法进行检查:

if ( ! $con ) {
    die('Could not connect: ' . mysql_error());
}

$sql1 = mysql_query("SELECT * FROM Persons", $con);
...

Beside double quotes in second line. 在第二行的双引号旁边。
mysql_fetch_array return array indexed by integer if you want asoc array indexed by fields use mysql_fetch_assoc 如果要按字段索引的asoc数组,则mysql_fetch_array返回以整数索引的数组,请使用mysql_fetch_assoc

while ($info=mysql_fetch_assoc($sql1)) {
   ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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