致命错误:在非对象上调用成员函数Fetch_Assoc()

时间:2012-10-05 12:23:41

标签: php html mysql phpmyadmin

我正在尝试创建一个小表单,供人们访问我的网站,只需输入以下信息:

  • 名称
  • 电子邮件地址

它应该将此信息从我的数据库发回到我的表中。

现在这里的事情变得“复杂”(......好吧,对我来说,好吗?)

如果我们的表中确实存在电子邮件地址,那么它必须回显“已添加电子邮件地址。谢谢!”。否则,如果它不存在,那么它必须回显“电子邮件地址已经存在!”

出于某种原因,它没有这样做。它正在吐出这个错误:

Fatal error: Call to a member function fetch_assoc() on a non-object in C:\wamp\www\addemail.php on line 46

我附上了我的php代码(如下):

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html lang="en">

<head>

<script LANGUAGE="JavaScript">
<!--
function redirect () { setTimeout("go_now()",3000); }
function go_now ()   { window.location.href = "newsletter_frame.html"; }
//-->
</script>

<!-- This is the link to the external CSS file-->
<link rel="stylesheet" type="text/css" href="css/mystyle.css">

<!-- This is the link to the external Javascript file-->
<script type="text/javascript" src="js/myjs.js"></script>

<title>Page</title>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<meta name="verify-v1" content="gDSxHR1Tk8vWtty9GoRHABGFEH+Bh2VHYCHv0Cx1/Ek=">
<meta name="copyright" content="Burger man">
<meta name="keywords" content="afro, afrodeep, soulful, funky, deep, vocal house, broken beats, disco">
<meta name="description" content="Burgers and chips">
<meta name="page-type" content="Information">

</head>
    <body  onLoad="redirect()" id="iframe_newsl_spec">

        <?php
            $dbc = mysqli_connect('localhost', 'root', '', 'si_store') or die('Error connecting to MySQL server.');

                if ( isset($_POST['email_address']) && isset($_POST['firstname']) && isset($_POST['surname']) )
                    {
                        $email_address = $_POST['email_address'];
                        $firstname = $_POST['firstname'];
                        $surname = $_POST['surname'];

                        $query = mysqli_query("SELECT * FROM email_list WHERE email_address = '$email_address'");

                        if ( mysql_num_rows($query) > 0)
                        {
                            echo 'Email address already exists!';
                        }
                        else
                        {
                            mysqli_query("INSERT INTO email_list(email_address, firstname, surname) VALUES ('$email_address','$firstname','$surname')");

                            echo 'Email Address Added. Thank you!';
                        }   
                    }

            //mysqli_query($dbc, $query) or die ('Error connecting to database.');

            mysqli_close($dbc);
        ?>



    </body>
</html>

1 个答案:

答案 0 :(得分:0)

修复mysql_*个函数并将其更改为mysqli_*之后,您已更改了代码,以便使用预处理语句< / em>,您应该添加以下代码:

$stmt = mysqli_prepare('SELECT * FROM email_list WHERE email_address = ?');
//BTW, if all you want to check if a record exists, don't use SELECT *
//Just select one field, not the entire row
mysqli_stmt_bind_param($stmt,'s',$_POST['email_address']);
//bind param, to statement, as String, value ^^
mysqli_stmt_execute($stmt);
//execute the query
mysqli_stmt_bind_result($stmt,$col1,$col2,$col3);
//bind variables that will be assigned the value of the fields
while(mysqli_stmt_fetch($stmt))
{//get the values in question
    echo 'col1: '.$col1.'<br/>col2: '.$col2.'<br/>col3: '.$col3.'<br/>';
}

要获得结果集的关联数组,您将不得不read the docs有一些函数可能需要使用。