PHP warning help?

2019-01-20 20:09发布

I keep getting the following error listed below and I was wondering how do I correct this error.

Warning: mysqli_close() expects exactly 1 parameter, 0 given in

Here is the code listed below.

<?php
require_once ('./mysqli_connect.php'); // Connect to the db.

function make_list ($parent) {

    global $tasks;

    echo '<ol>';

    foreach ($parent as $task_id => $todo) {

        echo "<li>$todo";

        if (isset($tasks[$task_id])) { 

            make_list($tasks[$task_id]);

        }

        echo '</li>';

    } // End of FOREACH loop.

    // Close the ordered list:
    echo '</ol>';

} // End of make_list() function.

    $mysqli = new mysqli("localhost", "root", "", "sitename");
    $dbc = mysqli_query($mysqli,"SELECT task_id, parent_id, task FROM tasks WHERE date_completed='0000-00-00 00:00:00' ORDER BY parent_id, date_added ASC");
if (!$dbc) {
    // There was an error...do something about it here...
    print mysqli_error();
} 

$tasks = array();

while (list($task_id, $parent_id, $task) = mysqli_fetch_array($dbc, MYSQLI_NUM)) {

    // Add to the array:
    $tasks[$parent_id][$task_id] =  $task;

}

make_list($tasks[0]);


mysqli_close(); // close the connection

// Include the html footer
include('./includes/footer.html');
?>

标签: php mysqli
4条回答
Melony?
2楼-- · 2019-01-20 20:21

I am a newbie, but I was finally able to solve this problem by moving the location of the mysqli_close(). I had it right after the last } , but then I moved it right before it and it worked.

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Make Me Elvis - Send Email</title>
    <link rel="stylesheet" type="text/css" href="style.css" />
    </head>
    <body>
    <?php
    $from = 'elmer@makemeelvis.com';
    $subject = $_POST['subject'];
    $text = $_POST['elvismail'];

    if (empty($subject) && empty($text)){
    echo 'Both, the subject and the email field have been left empty';}

    if (empty($subject) && !empty($text)){
    echo 'The subject field is empty';}

    if (!empty($subject)&& empty($text)){
    echo 'The email field is empty'; }  

    if (!empty($subject) && !empty ($text)){

    $dbc = mysqli_connect('servername', 'username', 'password', 'dbname')
    or die('Error connecting to MySQL server.');

    $query = "SELECT * FROM email_list";
    $result = mysqli_query($dbc, $query)
    or die('Error querying database.');

    while ($row = mysqli_fetch_array($result)){
    $to = $row['email'];
    $first_name = $row['first_name'];
    $last_name = $row['last_name'];
    $msg = "Dear $first_name $last_name,\n $text";
    mail($to, $subject, $msg, 'From:' . $from);
    echo 'Email sent to: ' . $to . '<br />';
    }
    mysqli_close($dbc);
    } 

    ?>

``

查看更多
我命由我不由天
3楼-- · 2019-01-20 20:37

Call mysqli_close($mysqli);

查看更多
戒情不戒烟
4楼-- · 2019-01-20 20:39

The error message is clear : you called mysqli_close without any argument, while the function expects one.

According to mysqli_close documentation, you must provide the mysqli link as its argument.

查看更多
家丑人穷心不美
5楼-- · 2019-01-20 20:42

Use $mysqli->close(); or mysqli_close($mysqli);

查看更多
登录 后发表回答