>编辑
所以我想出了一个解决方法。我用 if 替换了 while 语句,它工作得很好。感谢所有帮助过的人!
出现此错误时,我正在制作注册页面(我仍然对编程和 php 有点陌生,所以这可能会解释错误是什么,我只是不知道(
警告:mysql_fetch_array(( 期望参数 1 是资源,布尔值在第 22 行的/Users/Jacob/Sites/website/postregister.php 中给出
这是我的代码:
<!-- start sign up form -->
<?php
include("config.php");
$firstname = mysql_real_escape_string($_REQUEST['firstname']);
$lastname = mysql_real_escape_string($_REQUEST['lastname']);
$email = mysql_real_escape_string($_REQUEST['email']);
$birthyear = mysql_real_escape_string($_REQUEST['birthyear']);
$city = mysql_real_escape_string($_REQUEST['city']);
$address = mysql_real_escape_string($_REQUEST['address']);
$state = mysql_real_escape_string($_REQUEST['state']);
$phone = mysql_real_escape_string($_REQUEST['phone']);
$password = mysql_real_escape_string($_REQUEST['password']);
$zipcode= mysql_real_escape_string($_REQUEST['zipcode']);
$active = 0;
$hash = md5( rand(0,1000) ); // Generate random 32 character hash and assign it to a local variable.
$sql= mysql_query("INSERT INTO member (firstname, lastname, email, phone, city, state, address, zipcode, password, hash, active)
VALUES
('$firstname','$lastname','$email','$phone', '$city', '$state','$address', '$zipcode', '$password', '$hash', '$active')")or die(mysql_error());
while($row == mysql_fetch_array($sql)){
$to = $email; //Send email to our user
$subject = 'Signup | Verification'; //// Give the email a subject
$message = '
Thanks for signing up!<br/>
Your account has been created, you can login with the following credentials after you have activated your account by pressing the url below.<br/>
------------------------------------------------------------------------------<br/>
Email: '.$email.'<br/>
Password: '.$password.'<br/>
------------------------------------------------------------------------------
<a href="http://localhost/~jacob/pages/verify.php?email='.$email.'&hash='.$hash.'">Please Click Here to Activate Your Account</a>
'; // Our message above including the link
$headers = "From: customersupport@example.com'r'n";
$headers .= "Content-type: text/html'r'n";
mail($to, $subject, $message, $headers); // Send the email
}
?>
<form action="postregister.php" method="post">
<label for="firstname">First Name:</label><br/>
<input type="text" name="firstname" value="" size='90' style='height:20px;' /><br/><br/>
<label for="lastname">Last Name:</label><br/>
<input type="text" name="lastname" value="" size='90' style='height:20px;'/><br/><br/>
<label for="email">Email:</label><br/>
<input type="text" name="email" value="" size='90' style='height:20px;'/><br/><br/>
<label for="birthyear">Birthyear:</label><br/>
<input type="text" name="birthyear" value="" size='90' style='height:20px;'/><br/><br/>
<label for="city">City:</label><br/>
<input type="text" name="city" value="" size='90' style='height:20px;'/><br/><br/>
<label for="phone">Phone:</label><br/>
<input type="text" name="phone" value="" size='90' style='height:20px;'/><br/><br/>
<label for="address">Address:</label><br/>
<input type="text" name="address" value="" size='90' style='height:20px;'/><br/><br/>
<label for="zipcode">Zipcode:</label><br/>
<input type="text" name="zipcode" value="" size='90' style='height:20px;'/><br/><br/>
<label for="State">State:</label><br/>
<input type="text" name="state" value="" size='90' style='height:20px;'/><br/><br/>
<label for="password">Password:</label><br/>
<input type="password" name="password" value="" size='90' style='height:20px;'/><br/><br/>
<input type="submit" class="submit_button" value="Sign up" />
</form>
任何帮助将不胜感激。谢谢!
请阅读手册以了解mysql_query()
。在"返回值...
对于其他类型的 SQL 语句,INSERT、UPDATE、DELETE、DROP 等,mysql_query(( 在成功时返回 TRUE,在出错时返回 FALSE。
由于查询是INSERT
类型语句,因此mysql_query()
将返回布尔值。也没有要获取的行。
您可能可以将代码更改为以下内容
$result = mysql_query("All that SQL");
if ($result === false) {
// or die() is a terrible way to handle errors
throw new Exception(mysql_error());
}
$to = $email;
// and so on
大多数看到此消息的人只是忘记检查执行语句的错误。您已经用mysql_query
行的or die
部分覆盖了它。
您的问题是您正在尝试循环访问INSERT
生成的行,但INSERT
不会返回一组行。从 PHP 文档中:
对于其他类型的 SQL 语句,插入、更新、删除、删除等, mysql_query(( 在成功时返回 TRUE,在错误时返回 FALSE。
无论如何,您实际上并没有在循环中使用$row
。
代替循环和mysql_fetch_array
,您可以检查mysql_query
是否返回 true,在这种情况下,您的行已添加,您可以发送电子邮件。您已经编写查询的方式,虽然我个人不推荐它,但实际上使这变得非常简单:您只需要在查询行之后立即发送电子邮件,因为您已经在die
以防万一。
看起来您的查询返回 false。 我想您的数据库连接有错误。 尝试打印查询并在数据库上手动运行它,以确认其正常运行。 您也可以尝试一个简单的:
$result = mysql_query("SELECT * FROM member LIMIT 10");
$row = mysql_fetch_array($result);
print_r($row);
看看会发生什么。 我想这也会出错。 检查您的include("config.php");
,并确保数据库链接已设置且工作正常。 要确认您的$sql没有得到有效的结果,您可以尝试将其转储出来,看看它使用的是print_r或var_dump的类型。 祝你好运。