所以我有这个质朴和基本的PHP将检查我的数据库中的值,如果它的更正它将拉出该行的其余部分我猜但我的问题是如何回应certaine HTML如果结果是好的,如何如果否则回复其他一些html。
btw atm else无效,换句话说,如果您输入的代码不在我的数据库中,则不会显示任何内容
<?php
$db_hostname = 'localhost';
$db_database = 'codedb';
$db_username = 'root';
$db_password = '';
$table = 'users';
$field = 'code';
$test = 'first_name';
// Connect to server.
$connection = mysql_connect($db_hostname, $db_username, $db_password) OR DIE ("Unable to
connect to database! Please try again later.");
// Select the database.
mysql_select_db($db_database)
or die("Unable to select database: " . mysql_error());
$query = "SELECT * FROM $table WHERE $field = '{$_GET["qcode"]}'";
$result = mysql_query($query);
if ($result) {
while($row = mysql_fetch_array($result)) {
$name = $row["$field"];
$test = $row["$test"];
echo "Hello: $name $test";
}
}
else {
echo "Im sorry you buddy, you are not a winner this time! $test";
}
mysql_close($connection);
?>
答案 0 :(得分:1)
您可以使用mysql_num_rows(),这是最好的方式。
if( mysql_num_rows($result) > 0 ){
/* Anything you want here on success */
} else {
/* Anything you want here on failure */
}
答案 1 :(得分:0)
对查询使用mysql_num_rows。
if(mysql_num_rows($result)){ } else { }
答案 2 :(得分:0)
您可以简单地将html包含在if语句
中,而不是回显每个html值像:
<?php
$query = "SELECT * FROM $table WHERE $field = '{$_GET["qcode"]}'";
$result = mysql_query($query);
if ($result) {
while($row = mysql_fetch_array($result)) {
$name = $row["$field"];
$test = $row["$test"];
?>
<b>Hello: <?php echo $name $test"; ?></b>
<?php
}
}
else {
?>
<b> Im sorry you buddy, you are not a winner this time! </b> <?php echo $test";
}
mysql_close($connection);
?>
答案 3 :(得分:0)
if(mysql_num_rows($result) > 0) { echo "output"; } else { echo "error msg"; }
答案 4 :(得分:0)
首先,您必须更正这样的数据库选择语法
mysql_select_db($db_database,$connection);
Insted of
mysql_select_db($db_database);
检查Manual