1

I have an issue with php. The following code generates the error "PHP Warning: mysqli_close() expects parameter 1 to be mysqli, null given[...]" on the line containing the mysqli_query

<html>
<head>
<?php
    $table = "prjsuggestions";
    /$mysqli = NULL;
    if(!empty($_POST['posttext'])){
        $pnameempty = empty($_POST['postname']);
        $ynameempty = empty($_POST['name']);
        if($pnameempty || $ynameempty){
        }
        else{
            $mysqli = new mysqli("localhost", "progclub", "", "progclub");
            if(mysqli_connect_errno()){
                printf("Connect failed: %s\n", mysqli_connect_error());
                exit();
            }
            //successful query normally occurs here but code fails w/ or /wo it.
        }
    }
    else{
       printf("No information posted.");
    }
?>
<title>Bucknell Programming Club</title>
</head>
<body>
    <span id="posts">
<?php
    $offset = 0;
    $query = "SELECT * FROM {$table}";
    $result = mysqli_query($mysqli, $query);
    if($result !== FALSE){
        //while(($post = mysqli_fetch_assoc($result)) !== NULL){
            echo mysqli_num_rows($result);
            $post = mysqli_fetch_assoc($result);
            $author = $post['name'];
            printf("Author: %s\n", $author);
            echo "<br />";
            printf("Post title: %s\n", $post['title']);
            echo "<br />";
            printf("%s\n", $post['text']);
            echo "<hr />";
        //}
    }
    else printf("oh nooo!");
    mysqli_free_result($result);
    mysqli_close($mysqli);
?>
    </span>
</body>
</html>

Note that all queries have been checked out and are working correctly in phpmy, and that the original code contains an earlier query that adds data to the 'base, which also definitely works.

I have tried various combinations of static and global, and I have taken a thoroughish look at PHP's page on variable scope, alas I do not entirely understand it in this context (esp. given my inability to make my code work). Can somebody enlighten me as to the differing scopes here? I didn't think there should be any!!

10
  • 4 spaces before a line = code formatting. Select and tap ctr-k to do this. Commented Oct 1, 2010 at 5:16
  • Your scope looks okay, but it's impossible to tell, since you provide invalid and incomplete PHP. Show some code that at least works enough to produce the error. Commented Oct 1, 2010 at 5:17
  • 5
    The devil's in the details. Please show us the actual code that fails, don't summarize it with comments! *tsk tsk* Commented Oct 1, 2010 at 5:21
  • 1
    If $mysqli is NULL then that usually means the connection failed. Do you have the password and hostname correct? Commented Oct 1, 2010 at 5:28
  • 1
    And anyway, you should connect to the database and execute the query at the top of the file, and loop over the results where you want to print them. And put the PHP above the <html> tag to really sparate HTML and PHP. Commented Oct 1, 2010 at 6:05

2 Answers 2

3

Take a look at the documentation (specifically Example 1). There are a few example similar to yours.

The following should work, and it is similar to your code:

Note how the first argument of mysqli_query() is in fact your variable $mysqli. You were probably trying to put something else there.

Also, be sure to check your connection, like in the code below:

<html>
<head>    
<?php
    $mysqli = mysqli_connect("localhost", "my_user", "my_password", "world");
?>
</head>
<body>
<?php

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

/* Use $mysqli */
if (mysqli_query($mysqli, "/* ... MySQL goes here... */") === TRUE) {
    /* Success! */
}

mysqli_close($mysqli);
?>
</body>
</html>
Sign up to request clarification or add additional context in comments.

Comments

0

OOOOOOOOOOoooooooooooooooooohh. D'oh. Yeah. Scope. Perhaps I should have though more about the fact that the variable is only initialized when data is written. Oops. Thanks guys.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.