views:

57

answers:

2

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!!

+3  A: 

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>
Peter Ajtai
A: 

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.

conartist6