Jump to content

Recommended Posts

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();
Link to comment
https://forums.phpfreaks.com/topic/297947-function-calls-to-db-not-working/
Share on other sites

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 by mac_gyver

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.