Variable scope in PHP

ironmonger1066

New Member
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\[code\]<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>\[/code\]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!!
 
Back
Top