iseriouslyneedhelp Posted July 10, 2012 Share Posted July 10, 2012 Okay, so I have a page 'details.php' which is mostly html (meaning not much php). Then I have another page which is lead-results.php. In lead-results.php, is my php and I have 2 functions that are called into details.php by using require_once. The problem that I'm having is that I get an error when 2 of the functions are in the same results.php page. I separated the functions into their own files lead-results.php and lead-results2.php and they work fine. I think it has something to do possibly with closing MYSQLI then opening again, but I'm not sure any other way to write these functions so they work together. I've tried several ways with no success. My question is, why am I getting the following errors and how do I fix my functions so that they work without error? ERRORS: Warning: mysqli::prepare() [mysqli.prepare]: Couldn't fetch mysqli in D:\Hosting\4879265\html\cms\scripts\lead-results.php on line 181 Fatal error: Call to a member function bind_result() on a non-object in D:\Hosting\4879265\html\cms\scripts\lead-results.php on line 183 DETAILS.PHP - HTML PAGE <?php session_start(); require_once("../scripts/login-check.php"); ?> <?php require_once("../scripts/lead-results.php"); ?> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <title><?php include("../style/inc/logo.php"); ?> :: View Details</title> <meta name="description" content=""/> <meta name="keywords" content=""/> <script type="text/javascript" src="/cms/style/js/jquery-1.7.2.js"></script> <link href="/cms/style/css/page.css" rel="stylesheet" type="text/css"/> </head> <body> <div id="layout"> <div id="header"> <div id="logo"><a href="/cms" id="logo"> <?php include("../style/inc/logo.php"); ?> </a></div> <div id="topnav"> <?php require("../menus/main-nav.php"); ?> </div> </div> <div id="content"> <div id="secondary-nav"> <?php leaddetailspgid(); ?> </div> <div id="sub-nav"> <ul id="sub"> <?php require("../scripts/contacts-results.php"); $stmt->fetch(); echo'<li><a href="/cms/edit/?ID='.$ID.'">Edit</a></li>'; $stmt->close(); $mysqli->close(); ?> <li><a href="javascript: history.go(-1)">Cancel</a></li> </ul> </div> <div id="view-detail"> <?php leaddeatails(); ?> </div> </div> </div> <div id="footer" class="clear"> <?php include("../menus/footer.php"); ?> </div> </body> </html> LEAD RESULTS - PHP PAGE // DATABASE CONNECTION require("/config.php"); // LEAD DETAILS PAGE - PAGE ID function leaddetailspgid() { global $mysqli; $stmt = $mysqli->prepare("SELECT ID,date,firstname,lastname,spousefirst,spouselast,primarybday,spousebday,phonecell,phonehome,phoneoffice,spousecell,phoneother,email,emailspouse,emailother,emailspouseother,address,suite,city,state,zipcode,addressother,suiteother,cityother,stateother,zipcodeother,agentassigned,contacttype,status,contactsource,timing,password,subscribesearches,subscribedrips FROM contacts"); $stmt->bind_result($ID,$date,$firstname,$lastname,$spousefirst,$spouselast,$primarybday,$spousebday,$phonecell,$phonehome,$phoneoffice,$spousecell,$phoneother,$email,$emailspouse,$emailother,$emailspouseother,$address,$suite,$city,$state,$zipcode,$addressother,$suiteother,$cityother,$stateother,$zipcodeother,$agentassigned,$contacttype,$status,$contactsource,$timing,$password,$subscribesearches,$subscribedrips); $stmt->execute(); $stmt->fetch(); $space = (!empty($firstname) && !empty($lastname)) ? ' ' : ''; $name = $firstname .$space. $lastname; $name .= (!empty($spousefirst)) ? " & {$spousefirst}" : ''; $name .= (!empty($spouselast)) ? " {$spouselast}" : ''; $cell = (!empty($phonecell)) ? " {$phonecell} (cell)" : ''; $home = (!empty($phonehome)) ? " {$phonehome} (home)" : ''; $email = (!empty($email)) ? " {$email} (email)" : ''; $pbday = date("F jS", strtotime($primarybday)); $sbday = date("F jS", strtotime($spousebday)); echo' <ul id="secondary"> <li class="pageid">DETAILS:</a></li> <li class="pageid">'.$name.'</li> <li class="pageid">'.$home.'</li> <li class="pageid">'.$cell.'</li> <li class="pageid">'.$email.'</li> </ul>'; $stmt->close(); $mysqli->close(); } // LEAD DETAILS PAGE - DETAILS function leaddeatails() { global $mysqli; // THIS IS LINE 181 $stmt = $mysqli->prepare("SELECT ID,date,firstname,lastname,spousefirst,spouselast,primarybday,spousebday,phonecell,phonehome,phoneoffice,spousecell,phoneother,email,emailspouse,emailother,emailspouseother,address,suite,city,state,zipcode,addressother,suiteother,cityother,stateother,zipcodeother,agentassigned,contacttype,status,contactsource,timing,password,subscribesearches,subscribedrips FROM contacts"); // THIS IS LINE 183 $stmt->bind_result($ID,$date,$firstname,$lastname,$spousefirst,$spouselast,$primarybday,$spousebday,$phonecell,$phonehome,$phoneoffice,$spousecell,$phoneother,$email,$emailspouse,$emailother,$emailspouseother,$address,$suite,$city,$state,$zipcode,$addressother,$suiteother,$cityother,$stateother,$zipcodeother,$agentassigned,$contacttype,$status,$contactsource,$timing,$password,$subscribesearches,$subscribedrips); $stmt->execute(); $stmt->fetch(); echo' <table class="view-detail" id="view-detail"> <tbody> <tr> <td colspan="2"><h2>Details</h2></td> </tr> <tr> <td>Partner: </td><td>'.$spousefirst.' '.$spouselast.'</td> </tr> <tr> <td>Cell: </td><td>'.$phonecell.'</td> </tr> <tr> <td>Partner Cell: </td><td>'.$spousecell.'</td> </tr> <tr> <td>Home: </td><td>'.$phonehome.'</td> </tr> <tr> <td>Office: </td><td>'.$phoneoffice.'</td> </tr> <tr> <td>Email: </td><td>'.$email.'</td> </tr> <tr> <td>Partner Email: </td><td>'.$emailspouse.'</td> </tr> <tr> <td>Password: </td><td>'.$password.'</td> </tr> <tr> <td>Other Email: </td><td>'.$emailother.'</td> </tr> <tr> <td colspan="2"><h2>Mailing Address</h2></td> </tr> <tr> <td>Address: </td><td>'.$address.' Suite: '.$suite.'</td> </tr> <tr> <td>City: </td><td>'.$city.'</td> </tr> <tr> <td>State: </td><td>'.$state.'</td> </tr> <tr> <td>Zip Code: </td><td>'.$zipcode.'</td> </tr> <tr> <td colspan="2"><h2>Investment Property</h2></td> </tr> <tr> <td>Address: </td><td>'.$addressother.' Suite: '.$suiteother.'</td> </tr> <tr> <td>City: </td><td>'.$cityother.'</td> </tr> <tr> <td>State: </td><td>'.$stateother.'</td> </tr> <tr> <td>Zip Code: </td><td>'.$zipcodeother.'</td> </tr> <tr> <td colspan="2"><h2>Birthdays</h2></td> </tr> <tr> <td>Birthday: </td><td>'.$pbday.'</td> </tr> <tr> <td>Partner Birthday: </td><td>'.$sbday.'</td> </tr> <tr> <td colspan="2"><h2>Other Details</h2></td> </tr> <tr> <td>Agent: </td><td>'.$agentassigned.'</td> </tr> <tr> <td>Type: </td><td>'.$contacttype.'</td> </tr> <tr> <td>Status: </td><td>'.$status.'</td> </tr> <tr> <td>Groups: </td><td>Groups Checkboxes Go Here!</td> </tr> <tr> <td colspan="2"><h2>Subscriptions</h2></td> <tr> <td>Searches: </td><td>'.$subscribesearches.'</td> </tr> <tr> <td>Email Drips: </td><td>'.$subscribedrips.'</td> </tr> </tbody> </table>'; $stmt->close(); $mysqli->close(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/265463-functions-php/ Share on other sites More sharing options...
kicken Posted July 10, 2012 Share Posted July 10, 2012 $mysqli->close(); Don't bother even closing the connection to mysql. Just delete that line from all your functions. PHP will close it automatically when the script ends. Constantly closing and re-opening a connection to the DB over the duration of a script is a waste of time and resources, not to mention a pain to manage. Quote Link to comment https://forums.phpfreaks.com/topic/265463-functions-php/#findComment-1360471 Share on other sites More sharing options...
hakimserwa Posted July 10, 2012 Share Posted July 10, 2012 make different functions to care for the opening and closing of database and insted call them in the detailss.php and remove them from your functions that works for me. Quote Link to comment https://forums.phpfreaks.com/topic/265463-functions-php/#findComment-1360473 Share on other sites More sharing options...
ignace Posted July 10, 2012 Share Posted July 10, 2012 This should tell you what the error is: <?php // DATABASE CONNECTION require("/config.php"); // LEAD DETAILS PAGE - PAGE ID function leaddetailspgid() { global $mysqli; $stmt = $mysqli->prepare("SELECT ID,date,firstname,lastname,spousefirst,spouselast,primarybday,spousebday,phonecell,phonehome,phoneoffice,spousecell,phoneother,email,emailspouse,emailother,emailspouseother,address,suite,city,state,zipcode,addressother,suiteother,cityother,stateother,zipcodeother,agentassigned,contacttype,status,contactsource,timing,password,subscribesearches,subscribedrips FROM contacts"); $stmt->bind_result($ID, $date, $firstname, $lastname, $spousefirst, $spouselast, $primarybday, $spousebday, $phonecell, $phonehome, $phoneoffice, $spousecell, $phoneother, $email, $emailspouse, $emailother, $emailspouseother, $address, $suite, $city, $state, $zipcode, $addressother, $suiteother, $cityother, $stateother, $zipcodeother, $agentassigned, $contacttype, $status, $contactsource, $timing, $password, $subscribesearches, $subscribedrips); $stmt->execute(); $stmt->fetch(); $space = (!empty($firstname) && !empty($lastname)) ? ' ' : ''; $name = $firstname . $space . $lastname; $name .= (!empty($spousefirst)) ? " & {$spousefirst}" : ''; $name .= (!empty($spouselast)) ? " {$spouselast}" : ''; $cell = (!empty($phonecell)) ? " {$phonecell} (cell)" : ''; $home = (!empty($phonehome)) ? " {$phonehome} (home)" : ''; $email = (!empty($email)) ? " {$email} (email)" : ''; $pbday = date("F jS", strtotime($primarybday)); $sbday = date("F jS", strtotime($spousebday)); echo ' <ul id="secondary"> <li class="pageid">DETAILS:</a></li> <li class="pageid">' . $name . '</li> <li class="pageid">' . $home . '</li> <li class="pageid">' . $cell . '</li> <li class="pageid">' . $email . '</li> </ul>'; $stmt->close(); $mysqli->close(); } // LEAD DETAILS PAGE - DETAILS function leaddeatails() { global $mysqli; $stmt = $mysqli->prepare("SELECT ID,date,firstname,lastname,spousefirst,spouselast,primarybday,spousebday,phonecell,phonehome,phoneoffice,spousecell,phoneother,email,emailspouse,emailother,emailspouseother,address,suite,city,state,zipcode,addressother,suiteother,cityother,stateother,zipcodeother,agentassigned,contacttype,status,contactsource,timing,password,subscribesearches,subscribedrips FROM contacts"); if ($stmt === false) { echo 'SQL ERROR: ', $mysqli->error; exit; } $stmt->bind_result($ID, $date, $firstname, $lastname, $spousefirst, $spouselast, $primarybday, $spousebday, $phonecell, $phonehome, $phoneoffice, $spousecell, $phoneother, $email, $emailspouse, $emailother, $emailspouseother, $address, $suite, $city, $state, $zipcode, $addressother, $suiteother, $cityother, $stateother, $zipcodeother, $agentassigned, $contacttype, $status, $contactsource, $timing, $password, $subscribesearches, $subscribedrips); $stmt->execute(); $stmt->fetch(); echo ' <table class="view-detail" id="view-detail"> <tbody> <tr> <td colspan="2"><h2>Details</h2></td> </tr> <tr> <td>Partner: </td><td>' . $spousefirst . ' ' . $spouselast . '</td> </tr> <tr> <td>Cell: </td><td>' . $phonecell . '</td> </tr> <tr> <td>Partner Cell: </td><td>' . $spousecell . '</td> </tr> <tr> <td>Home: </td><td>' . $phonehome . '</td> </tr> <tr> <td>Office: </td><td>' . $phoneoffice . '</td> </tr> <tr> <td>Email: </td><td>' . $email . '</td> </tr> <tr> <td>Partner Email: </td><td>' . $emailspouse . '</td> </tr> <tr> <td>Password: </td><td>' . $password . '</td> </tr> <tr> <td>Other Email: </td><td>' . $emailother . '</td> </tr> <tr> <td colspan="2"><h2>Mailing Address</h2></td> </tr> <tr> <td>Address: </td><td>' . $address . ' Suite: ' . $suite . '</td> </tr> <tr> <td>City: </td><td>' . $city . '</td> </tr> <tr> <td>State: </td><td>' . $state . '</td> </tr> <tr> <td>Zip Code: </td><td>' . $zipcode . '</td> </tr> <tr> <td colspan="2"><h2>Investment Property</h2></td> </tr> <tr> <td>Address: </td><td>' . $addressother . ' Suite: ' . $suiteother . '</td> </tr> <tr> <td>City: </td><td>' . $cityother . '</td> </tr> <tr> <td>State: </td><td>' . $stateother . '</td> </tr> <tr> <td>Zip Code: </td><td>' . $zipcodeother . '</td> </tr> <tr> <td colspan="2"><h2>Birthdays</h2></td> </tr> <tr> <td>Birthday: </td><td>' . $pbday . '</td> </tr> <tr> <td>Partner Birthday: </td><td>' . $sbday . '</td> </tr> <tr> <td colspan="2"><h2>Other Details</h2></td> </tr> <tr> <td>Agent: </td><td>' . $agentassigned . '</td> </tr> <tr> <td>Type: </td><td>' . $contacttype . '</td> </tr> <tr> <td>Status: </td><td>' . $status . '</td> </tr> <tr> <td>Groups: </td><td>Groups Checkboxes Go Here!</td> </tr> <tr> <td colspan="2"><h2>Subscriptions</h2></td> <tr> <td>Searches: </td><td>' . $subscribesearches . '</td> </tr> <tr> <td>Email Drips: </td><td>' . $subscribedrips . '</td> </tr> </tbody> </table>'; $stmt->close(); $mysqli->close(); } Quote Link to comment https://forums.phpfreaks.com/topic/265463-functions-php/#findComment-1360485 Share on other sites More sharing options...
iseriouslyneedhelp Posted July 10, 2012 Author Share Posted July 10, 2012 $mysqli->close(); Don't bother even closing the connection to mysql. Just delete that line from all your functions. PHP will close it automatically when the script ends. Constantly closing and re-opening a connection to the DB over the duration of a script is a waste of time and resources, not to mention a pain to manage. By doing that, I get an error that it can't fetch the first function. Quote Link to comment https://forums.phpfreaks.com/topic/265463-functions-php/#findComment-1360533 Share on other sites More sharing options...
iseriouslyneedhelp Posted July 10, 2012 Author Share Posted July 10, 2012 This should tell you what the error is: <?php // DATABASE CONNECTION require("/config.php"); // LEAD DETAILS PAGE - PAGE ID function leaddetailspgid() { global $mysqli; $stmt = $mysqli->prepare("SELECT ID,date,firstname,lastname,spousefirst,spouselast,primarybday,spousebday,phonecell,phonehome,phoneoffice,spousecell,phoneother,email,emailspouse,emailother,emailspouseother,address,suite,city,state,zipcode,addressother,suiteother,cityother,stateother,zipcodeother,agentassigned,contacttype,status,contactsource,timing,password,subscribesearches,subscribedrips FROM contacts"); $stmt->bind_result($ID, $date, $firstname, $lastname, $spousefirst, $spouselast, $primarybday, $spousebday, $phonecell, $phonehome, $phoneoffice, $spousecell, $phoneother, $email, $emailspouse, $emailother, $emailspouseother, $address, $suite, $city, $state, $zipcode, $addressother, $suiteother, $cityother, $stateother, $zipcodeother, $agentassigned, $contacttype, $status, $contactsource, $timing, $password, $subscribesearches, $subscribedrips); $stmt->execute(); $stmt->fetch(); $space = (!empty($firstname) && !empty($lastname)) ? ' ' : ''; $name = $firstname . $space . $lastname; $name .= (!empty($spousefirst)) ? " & {$spousefirst}" : ''; $name .= (!empty($spouselast)) ? " {$spouselast}" : ''; $cell = (!empty($phonecell)) ? " {$phonecell} (cell)" : ''; $home = (!empty($phonehome)) ? " {$phonehome} (home)" : ''; $email = (!empty($email)) ? " {$email} (email)" : ''; $pbday = date("F jS", strtotime($primarybday)); $sbday = date("F jS", strtotime($spousebday)); echo ' <ul id="secondary"> <li class="pageid">DETAILS:</a></li> <li class="pageid">' . $name . '</li> <li class="pageid">' . $home . '</li> <li class="pageid">' . $cell . '</li> <li class="pageid">' . $email . '</li> </ul>'; $stmt->close(); $mysqli->close(); } // LEAD DETAILS PAGE - DETAILS function leaddeatails() { global $mysqli; $stmt = $mysqli->prepare("SELECT ID,date,firstname,lastname,spousefirst,spouselast,primarybday,spousebday,phonecell,phonehome,phoneoffice,spousecell,phoneother,email,emailspouse,emailother,emailspouseother,address,suite,city,state,zipcode,addressother,suiteother,cityother,stateother,zipcodeother,agentassigned,contacttype,status,contactsource,timing,password,subscribesearches,subscribedrips FROM contacts"); if ($stmt === false) { echo 'SQL ERROR: ', $mysqli->error; exit; } [/quote] I don't get an error, just the same message: Warning: mysqli::prepare() [mysqli.prepare]: Couldn't fetch mysqli Fatal error: Call to a member function bind_result() on a non-object Quote Link to comment https://forums.phpfreaks.com/topic/265463-functions-php/#findComment-1360536 Share on other sites More sharing options...
boompa Posted July 10, 2012 Share Posted July 10, 2012 Globals are bad, and you don't show us where you're creating the $mysqli global object you're attempting to use. Quote Link to comment https://forums.phpfreaks.com/topic/265463-functions-php/#findComment-1360542 Share on other sites More sharing options...
iseriouslyneedhelp Posted July 10, 2012 Author Share Posted July 10, 2012 Globals are bad, and you don't show us where you're creating the $mysqli global object you're attempting to use. I'm still learning php so you have to be more clear, do you want to see my config file? It's simple, see below. Also, if globals are so bad, what do you suggest? <?php /* connect to mysqli */ $host = 'host.com'; $uname = 'user'; $pword = 'pass'; $db = 'mydb'; $mysqli = new mysqli($host, $uname, $pword, $db); /* check mysqli connection */ if ($mysqli->connect_errno) { echo "Failed to connect to MySQL: (" .$mysqli->connect_errno . ")" . " " . $mysqli->connect_error; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/265463-functions-php/#findComment-1360550 Share on other sites More sharing options...
xyph Posted July 10, 2012 Share Posted July 10, 2012 Rather than use globals, pass the object as an argument in the function. <?php $db = new mysqli('localhost','root','','db'); $bad_var = 'lalala'; var_dump( this_uses_sql($db) ); // array // 'col1' => string 'foo' (length=3) // 'col2' => string 'bar' (length=3) var_dump( this_uses_sql($bad_var) ); // Fatal error: Call to a member function query() on a non-object on line 25 var_dump( this_needs_mysqli($db) ); // array // 'col1' => string 'foo' (length=3) // 'col2' => string 'bar' (length=3) var_dump( this_needs_mysqli($bad_var) ); // Catchable fatal error: Argument 1 passed to this_needs_mysqli() must be an // instance of mysqli, string given, called on line 19 and defined on line 35 function this_uses_sql($sql) { $result = $sql->query('SELECT * FROM sometable LIMIT 1'); if( $result == FALSE ) { return FALSE; } else { return $result->fetch_assoc(); } } // you can require the argument be an instance of the mysqli class function this_needs_mysqli(mysqli $sql) { $result = $sql->query('SELECT * FROM sometable LIMIT 1'); if( $result == FALSE ) { return FALSE; } else { return $result->fetch_assoc(); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/265463-functions-php/#findComment-1360581 Share on other sites More sharing options...
KevinM1 Posted July 10, 2012 Share Posted July 10, 2012 As to why globals are bad, they create an implicit connection between a function and the environment it's being invoked in. A connection that's easy to change/erase/not know about. This kind of connection is known as 'coupling' and the tighter that connection is between the function and its external environment, the harder your code will be to maintain, debug, and even understand. What's more, the entire point behind functions is to create reusable, loosely coupled bits of code that can be used in a multitude of situations. Using globals negates much of that. Functions have what's known as an argument list. It's a list of values that are supposed to be passed into the function to allow it to work, and is located in the () of the function definition like so: function example($arg1, $arg2) { echo "$arg1 - $arg2"; } How is that different than using 'global'? Functions are supposed to be black boxes. Material (variables) go in, some sort of result comes out. To the script at large, what happens within a function doesn't matter. So long as the right kind of result is returned, the script can go about its business. This kind of arrangement - a function's name, its dependencies, and what it returns - is known as the function's signature. Since functions are supposed to be black boxes, all raw material that it requires needs to be present up front and explicitly passed in. Globals blow the black box analogy apart by creating a hidden requirement. There's nothing in a function signature that gives evidence to the required global(s). Your functions now have knowledge about the outside environment, yet the script itself is essentially blind to the fact that the requirement exists at all. This might not seem like a big deal, especially if you never have others use your code, but it's really a bad way to do things. Given how easily globals can be overwritten, and how deep those kinds of dependencies can be nested, using them creates very fragile, very confusing code. In short, there's never a good reason to use globals, and it's generally a sign that the person who wrote that code didn't know what they were doing. If the resources you're using to teach yourself PHP uses globals, get better resources. Use the argument list instead. That's why it's there. Quote Link to comment https://forums.phpfreaks.com/topic/265463-functions-php/#findComment-1360588 Share on other sites More sharing options...
iseriouslyneedhelp Posted July 10, 2012 Author Share Posted July 10, 2012 In short, there's never a good reason to use globals, and it's generally a sign that the person who wrote that code didn't know what they were doing. If the resources you're using to teach yourself PHP uses globals, get better resources. Use the argument list instead. That's why it's there. Very good explaination, KevinM1. Thank you! So here's another question, would it be better to use __construct for my database connection and put it in a class? Quote Link to comment https://forums.phpfreaks.com/topic/265463-functions-php/#findComment-1360602 Share on other sites More sharing options...
iseriouslyneedhelp Posted July 10, 2012 Author Share Posted July 10, 2012 So I've learned that I need to use an argument list instead of $global, however, this still has not solved my original problem. I have 2 functions in the same lead-results.php file and an html page, index.php. When these functions are called in my index.php file, the first function works but the second doesn't. If I put them in their own separate files, they work fine. I'm sure there is a way for these to play nicely and I can't figure it out. ERRORS (COMMENTED LINES IN CODE BELOW): Warning: mysqli::prepare() [mysqli.prepare]: Couldn't fetch mysqli on line 173 Fatal error: Call to a member function bind_result() on a non-object on line 174 LEAD-RESULTS.php <?php // DATABASE CONNECTION require("/config.php"); // LEAD DETAILS PAGE - PAGE ID function leaddetailspgid($mysqli) { $stmt = $mysqli->prepare("SELECT ID,date,firstname,lastname FROM contacts"); $stmt->bind_result($ID,$date,$firstname,$lastname); $stmt->execute(); $stmt->fetch(); $space = (!empty($firstname) && !empty($lastname)) ? ' ' : ''; $name = $firstname .$space. $lastname; echo' <ul id="secondary"> <li class="pageid">DETAILS:</a></li> <li class="pageid">'.$name.'</li> </ul>'; $stmt->close(); } // LEAD DETAILS PAGE - DETAILS function leaddeatails ($mysqli) { $stmt = $mysqli->prepare("SELECT ID,date,firstname,lastname FROM contacts"); // line 173 $stmt->bind_result($ID,$date,$firstname,$lastname); // line 174 $stmt->execute(); $stmt->fetch(); echo' <table class="view-detail" id="view-detail"> <tbody> <tr> <td colspan="2"><h2>Details</h2></td> </tr> <tr> <td>Name: </td><td>'.$firstname.' '.$lastname.'</td> </tr> <tr> <td>Date Registered: </td><td>'.$date.'</td> </tr> </tbody> </table>'; $stmt->close(); $mysqli->close(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/265463-functions-php/#findComment-1360611 Share on other sites More sharing options...
xyph Posted July 10, 2012 Share Posted July 10, 2012 We've already told you not to close your database connection. Please attempt to apply all advice given. Objects are special, and are always passed by reference. This means changes to an object in a function will affect the way the object behaves outside of the function. Quote Link to comment https://forums.phpfreaks.com/topic/265463-functions-php/#findComment-1360614 Share on other sites More sharing options...
KevinM1 Posted July 10, 2012 Share Posted July 10, 2012 In short, there's never a good reason to use globals, and it's generally a sign that the person who wrote that code didn't know what they were doing. If the resources you're using to teach yourself PHP uses globals, get better resources. Use the argument list instead. That's why it's there. Very good explaination, KevinM1. Thank you! So here's another question, would it be better to use __construct for my database connection and put it in a class? Unless you're familiar with OOP as a methodology, I wouldn't. Your $mysqli is already an object. And there's a lot more to OOP than merely stuffing things related by theme in a class and calling it a day. So, unless you feel like you're going to gain something by doing that, just leave it procedural. Also, what xyph said. Quote Link to comment https://forums.phpfreaks.com/topic/265463-functions-php/#findComment-1360616 Share on other sites More sharing options...
iseriouslyneedhelp Posted July 10, 2012 Author Share Posted July 10, 2012 We've already told you not to close your database connection. Please attempt to apply all advice given. Objects are special, and are always passed by reference. This means changes to an object in a function will affect the way the object behaves outside of the function. Thanks, I've tried that as well...same errors! I also took out $stmt->close, still same errors as above. <?php // DATABASE CONNECTION require("/config.php"); // LEAD DETAILS PAGE - PAGE ID function leaddetailspgid($mysqli) { $stmt = $mysqli->prepare("SELECT ID,date,firstname,lastname FROM contacts"); $stmt->bind_result($ID,$date,$firstname,$lastname); $stmt->execute(); $stmt->fetch(); $space = (!empty($firstname) && !empty($lastname)) ? ' ' : ''; $name = $firstname .$space. $lastname; echo' <ul id="secondary"> <li class="pageid">DETAILS:</a></li> <li class="pageid">'.$name.'</li> </ul>'; } // LEAD DETAILS PAGE - DETAILS function leaddeatails ($mysqli) { $stmt = $mysqli->prepare("SELECT ID,date,firstname,lastname FROM contacts"); // line 173 $stmt->bind_result($ID,$date,$firstname,$lastname); // line 174 $stmt->execute(); $stmt->fetch(); echo' <table class="view-detail" id="view-detail"> <tbody> <tr> <td colspan="2"><h2>Details</h2></td> </tr> <tr> <td>Name: </td><td>'.$firstname.' '.$lastname.'</td> </tr> <tr> <td>Date Registered: </td><td>'.$date.'</td> </tr> </tbody> </table>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/265463-functions-php/#findComment-1360617 Share on other sites More sharing options...
xyph Posted July 10, 2012 Share Posted July 10, 2012 You're only going to get Couldn't fetch mysqli if - You're trying to use an instance of a mysqli object that's been close()'d You're trying to use an instance of a mysqli object in a procedural mysqli_ function You've somehow serialized the instance of the object You're using a custom session handler that closes the connection before writing Quote Link to comment https://forums.phpfreaks.com/topic/265463-functions-php/#findComment-1360625 Share on other sites More sharing options...
hakimserwa Posted August 6, 2012 Share Posted August 6, 2012 by the why do you jump advises? Quote Link to comment https://forums.phpfreaks.com/topic/265463-functions-php/#findComment-1367091 Share on other sites More sharing options...
redarrow Posted August 6, 2012 Share Posted August 6, 2012 KevinM1 <<< nice read on globs mate cheers. well written. Quote Link to comment https://forums.phpfreaks.com/topic/265463-functions-php/#findComment-1367142 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.