TapeGun007 Posted August 27, 2015 Share Posted August 27, 2015 I have a calendar function. In the function I declared $con and global so it would pass into the function. I have several other pages using this basic format of code that work fine that are not functions... Here is the code that is giving me issues within the CalendarFunction: $Code = $_SESSION['SalesReferralCode']; $sql = "SELECT * FROM Prospects WHERE ProspectCode = ?"; $stmt = $con->prepare($sql); var_dump($stmt); if($stmt === false){ trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $con->error, E_USER_ERROR); } // Bind parameters. Types: s = string, i = integer, d = double, b = blob $stmt->bind_param("s",$Code); $stmt->execute(); Quote Link to comment https://forums.phpfreaks.com/topic/297947-function-calls-to-db-not-working/ Share on other sites More sharing options...
TapeGun007 Posted August 27, 2015 Author Share Posted August 27, 2015 Here is what I get as an error: object(mysqli_stmt)#2 (0) { } bool(false) Fatal error: Wrong SQL: SELECT * FROM Prospects WHERE ProspectCode = ? Error: in functions/Calendar.php on line 56 Quote Link to comment https://forums.phpfreaks.com/topic/297947-function-calls-to-db-not-working/#findComment-1519705 Share on other sites More sharing options...
mac_gyver Posted August 27, 2015 Share Posted August 27, 2015 (edited) 1) if you had php's error_reporting set to E_ALL, you would likely be getting php errors that would help pin down the problem with $con. 2) don't use global to bring values into functions. pass them in as call-time parameters. 3) we cannot help you with what is wrong with your code unless you post enough of your code (less things like database credentials) that reproduces the problem. there could be at least a half-dozen different things that could be causing your current symptom. Edited August 27, 2015 by mac_gyver Quote Link to comment https://forums.phpfreaks.com/topic/297947-function-calls-to-db-not-working/#findComment-1519711 Share on other sites More sharing options...
TapeGun007 Posted August 27, 2015 Author Share Posted August 27, 2015 I was able to finally figure it out. But I do have a question: Why not use the global variable? I believe you, but if I understand the "why" it will stick better in memory. Quote Link to comment https://forums.phpfreaks.com/topic/297947-function-calls-to-db-not-working/#findComment-1519719 Share on other sites More sharing options...
gizmola Posted August 27, 2015 Share Posted August 27, 2015 Global variables introduce side-effect bugs, and go against the grain of good procedural and object oriented coding practices. A function or method should be discreet and provide unit testable reproducible behavior. Specific input(s) should produce specific results and return values. When you use globals, the function stops being discreet, and becomes dependent on the state of the global variables used. Worse yet, the globals are available and shared by any code that desires to make use of them. This leads to situations where you will have a hard time tracing down or reproducing problems because some path of execution has lead to a global variable being changed, and you won't be able to know easily what lead to the problem. They also contribute in general to spaghetti code where it becomes difficult to trace or understand what is happening. Specifically, imagine you are looking at some source code and a function is called, that then makes use of a global variable. Given the alternative of using a parameter, you can see where the parameter got it's value in the source code, because to call it you have to pass the parameter specifically. Contrast this with a global, where you may have no idea what has happened to that variable previously, where it came from, or what created its value. It is simply referenced in the source code at some point. Quote Link to comment https://forums.phpfreaks.com/topic/297947-function-calls-to-db-not-working/#findComment-1519723 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.