Note that there are some explanatory texts on larger screens.

plurals
  1. POVariable scope in PHP
    primarykey
    data
    text
    <p>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</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;?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."); } ?&gt; &lt;title&gt;Bucknell Programming Club&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;span id="posts"&gt; &lt;?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 "&lt;br /&gt;"; printf("Post title: %s\n", $post['title']); echo "&lt;br /&gt;"; printf("%s\n", $post['text']); echo "&lt;hr /&gt;"; //} } else printf("oh nooo!"); mysqli_free_result($result); mysqli_close($mysqli); ?&gt; &lt;/span&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>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. </p> <p>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!!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload