我是初学者学习php和mysql并且已经获得了Head First PHP& amp; MySQL和我正在尝试一个自制项目,我在其中创建了一个数据库和一个index.php页面,在那里我可以看到打印出的结果。当我转到我的index.php页面时,我看到了HTML标题但是php代码没有打印出我提交的内容。我必须假设我的代码语法是正确的,否则我最终会得到一个空白页面。有人可以告诉我,我编码错误的结果没有输出吗?感谢。
<?php
//Establish connection with database
$dbc = mysqli_connect(localhost,root,root,itmyfamily);
//Order the data to be retrieved
$query = "SELECT * FROM itsmyfamily ORDER BY last_name ASC, first_name `enter code here`DESC, date ASC";
//Execute the connect command and the query
$data = mysqli_query($dbc,$query);
//Loop through the array of family submissions, formatting it as html
$i = 0;
while ($row = mysqli_fetch_array($data)) {
//Display family submissions
if ($i == 0) {
echo '<strong>First Name:</strong> ' .$row['first_name']. ' <br/>';
echo '<strong>Last Name:</strong> ' .$row['last_name']. ' <br />';
echo '<strong>Spouse Name:</strong> ' .$row['spouse_name']. ' <br/>';
echo '<strong>Email:</strong> ' .$row['email']. ' <br />';
}
else {
echo 'There is no info in the database';
}
$i++;
}
mysqli_close($dbc);
?>
答案 0 :(得分:4)
将此设置在源代码之上以显示错误。
<?php error_reporting( E_ALL ); ?>
或者在php.ini中设置display_erros,如下所示:
display_errors = On
error_reporting = E_ALL | E_STRICT
还尝试使用以下内容替换源代码,
<?php
//Establish connection with database
$dbc = mysqli_connect(localhost,root,root,itmyfamily);
//Order the data to be retrieved
$query = "SELECT * FROM itsmyfamily ORDER BY last_name ASC, first_name DESC, date ASC";
//Execute the connect command and the query
$data = mysqli_query($dbc,$query);
while ($row = mysqli_fetch_array($data)) {
//Loop through the array of family submissions, formatting it as html
$i = 0;
//Display family submissions
if ($i == 0) {
echo '<strong>First Name:</strong> ' .$row['first_name']. ' <br />';
echo '<strong>Last Name:</strong> ' .$row['last_name']. ' <br />';
echo '<strong>Spouse Name:</strong> ' .$row['spouse_name']. ' <br />';
echo '<strong>Email:</strong> ' .$row['email']. ' <br />';
}
else{
echo 'There is no info in the database';
}
$i++;
}
mysqli_close($dbc);
?>
<强>更新强>
If you can write it this way, i think you dont even need the $i.
$result = mysql_query("SELECT id, first_name FROM mytable");
if($result){
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
printf("ID: %s Name: %s", $row["id"], $row["first_name"]);
}
}
else
echo 'There is no info in the database';
在此处阅读有关php mysql_fetch_array http://www.php.net/mysql_fetch_array
的更多信息请阅读此处以获取有关迭代的更多信息。 http://webcheatsheet.com/php/loops.php
请注意,您使用的方法已从PHP 5.5.0中弃用。所以我建议你考虑 mysqli 或 PDO 。示例可以在下面的php手册链接中找到