<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$firstname=$lastname = $email = $address = $hphone = $mphone = "";
$link = mysql_connect('shunurcom.fatcowmysql.com', 'sachin', 'sacjap');
mysql_select_db(cycle,$link);
if (!empty($_POST["firstname"]))
{
$firstname = $_POST["firstname"];
$sql= "SELECT * FROM users WHERE firstname = '$firstname' ";
$comments=mysql_query($sql,$link);
if(!$comments==false )
{
while($row = mysql_fetch_array($comments, MYSQL_ASSOC))
{
$firstname = $row['firstname'];
$lastname = $row['lastname'];
$email = $row['email'];
$address = $row['address'];
$hnum = $row['hphone'];
$mnum = $row['mphone'];
echo " <div style='margin:30px 0px;'>
First Name: $firstname<br />
Last Name: $lastname<br />
Email: $email<br />
Address: $address<br />
Home Number: $hnum<br />
Mobile Number: $mnum
</div>
";
}
}
else {
echo "invalid";
}
}
我尝试使用size of
,mysql_num_rows
但代码不会转到其他部分,当给出错误的输入时,该部分应该打印无效。
知道为什么吗?当输入无效时,它只显示一个空白屏幕。
答案 0 :(得分:0)
我在你的代码中看到很多混淆。
请试试这个:
<?php
if($_POST)
{
$link = mysql_connect('shunurcom.fatcowmysql.com', 'sachin', 'sacjap');
mysql_select_db(cycle,$link);
if (isset($_POST["firstname"]))
{
$firstname = $_POST["firstname"];
$sql= "SELECT * FROM users WHERE firstname = '$firstname' ";
$comments=mysql_query($sql,$link);
if($comments ) {
$number=mysql_num_rows($comments);
while($row = mysql_fetch_assoc($comments))
{
$firstname = $row['firstname'];
$lastname = $row['lastname'];
$email = $row['email'];
$address = $row['address'];
$hnum = $row['hphone'];
$mnum = $row['mphone'];
echo " <div style='margin:30px 0px;'>
First Name: $firstname<br />
Last Name: $lastname<br />
Email: $email<br />
Address: $address<br />
Home Number: $hnum<br />
Mobile Number: $mnum
</div> ";
}//end of while loop.
}
?>
答案 1 :(得分:0)
首先mysql_query
将在错误时返回FALSE
,而不是在结果集为空时,第二,您应该停止使用mysql_*
api,因为它已被弃用。
此扩展在PHP 5.5.0中已弃用,并已在PHP 7.0.0中删除
使用mysqli_*
或更好PDO
像这样更新你的代码:
// test here if you have a result set or not
$rows = mysql_num_rows($comments);
if ($rows == 0) {
print "invalid or no results"; // this way you won't execute the
// while block on an empty result set
}
else {
while($row = mysql_fetch_array($comments, MYSQL_ASSOC)) {
$firstname = $row['firstname'];
$lastname = $row['lastname'];
$email = $row['email'];
$address = $row['address'];
$hnum = $row['hphone'];
$mnum = $row['mphone'];
echo "<div style='margin:30px 0px;'>
First Name: $firstname<br />
Last Name: $lastname<br />
Email: $email<br />
Address: $address<br />
Home Number: $hnum<br />
Mobile Number: $mnum
</div>";
}
}
答案 2 :(得分:0)
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
$firstname = $lastname = $email = $address = $hphone = $mphone = "";
$link = mysql_connect('shunurcom.fatcowmysql.com', 'sachin', 'sacjap');
mysql_select_db(cycle, $link);
if (!empty($_POST["firstname"]))
{
$firstname = $_POST["firstname"];
$sql = "SELECT * FROM users WHERE firstname = '$firstname' ";
$comments = mysql_query($sql, $link);
if (!$comments == false)
{
$number = mysql_num_rows($comments);
if ($number >= 1)
{
while ($row = mysql_fetch_array($comments, MYSQL_ASSOC))
{
$firstname = $row['firstname'];
$lastname = $row['lastname'];
$email = $row['email'];
$address = $row['address'];
$hnum = $row['hphone'];
$mnum = $row['mphone'];
echo " <div style='margin:30px 0px;'>
First Name: $firstname<br />
Last Name: $lastname<br />
Email: $email<br />
Address: $address<br />
Home Number: $hnum<br />
Mobile Number: $mnum
</div>
";
}
}
else
{
echo "No record Found";
}
}
else
{
echo "invalid";
}
}