回声问题与while/if/else [英] Echo issue with while / if / else
问题描述
我是学习PHP和MySQL的初学者,并且已经进入 Head First PHP&的第5章. MySQL ,并且正在尝试一个自制项目,在该项目中创建了一个数据库和一个index.php
页面,从中可以看到打印出来的结果.当我进入index.php
页面时,我看到了HTML标题,但是PHP代码未打印出我的提交内容.我必须假设我的代码语法正确,否则我将得到空白页.有人可以告诉我我写错了什么,但没有输出结束吗?
I am a beginner learning PHP and MySQL and have gotten to chapter 5 of Head First PHP & MySQL and am attempting a self made project in which I created a database and a index.php
page where I can see the results printed out. When I go to my index.php
page I see the HTML title but the PHP code is not printing out my submissions. I have to assume my code syntax is correct or I would end up with a blank page. Can someone please tell me what I have coded wrong to wind up with no output?
<?php
$dbc = mysqli_connect(localhost, root, root, itmyfamily);
$query = "SELECT * FROM itsmyfamily ORDER BY last_name ASC, first_name DESC, date ASC";
$data = mysqli_query($dbc, $query);
$i = 0;
while ($row = mysqli_fetch_array($data))
{
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);
推荐答案
在源代码上方设置此项以显示错误.
Set this on top of the source code to display errors.
<?php error_reporting( E_ALL ); ?>
或按如下所示在php.ini中设置display_erros:
Or set display_erros in php.ini as follows:
display_errors = On
error_reporting = E_ALL | E_STRICT
也尝试将您的源代码替换为以下内容,
Also try to replace your source code with following,
<?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);
?>
如果您可以这样写,我想您甚至不需要$i
.
If you can write it this way, I think you don't 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
Read here for more information about PHP mysql_fetch_array http://www.php.net/mysql_fetch_array
在此处阅读有关迭代的更多信息. http://webcheatsheet.com/php/loops.php
Read here for more information about iterations. http://webcheatsheet.com/php/loops.php
请注意,PHP 5.5.0不推荐使用您使用的方法.因此,我建议您考虑使用 mysqli 或 PDO .可以在下面的PHP手册链接中找到示例
Please note that the method you have used is deprecated from PHP 5.5.0. So I suggest you consider mysqli or PDO. Examples can be found in below PHP manual links
http://www.php.net/manual/en/mysqli .query.php
http://www.php.net/manual/zh/pdo .query.php
这篇关于回声问题与while/if/else的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!