3

I have problem about putting mysql into a function showMsg(). The mysql is working fine if it is not wrapped by function showMsg(), but when I wrap it with function showMsg(), it gives error message "Warning: mysql_query(): supplied argument is not a valid". How to put mysql inside a php function? Below is my codes :

<?php    
function showMsg(){   
        $query2 = "SELECT id, message, username, datetime FROM messageslive ORDER BY id DESC LIMIT 20";
        $result2 = mysql_query($query2,$connection) or die (mysql_error());
        confirm_query($result2);
        $num = mysql_num_rows($result2); 
        while($msginfo = mysql_fetch_array($result2)){
            echo $msginfo['message'];
            echo $msginfo['username'];
        }
}

<div>
   <?php showMsg(); ?>
</div>
?>

6 Answers 6

9
  1. Never use global.
    Pass $connection into your function as an argument.

  2. Logic and representation should be separated.
    Read about MVC: here or here.

  3. Global variables are evil, never use it. If someone suggests it - ignore all their answers.

Sign up to request clarification or add additional context in comments.

1 Comment

never knew i can do that. I have follow your suggestion, i am using function parameter instead of global :)
6

You probably need:

global $connection;

(Inside the function, that is.)

See Variable Scope

1 Comment

I voted down for recommending globals. globals very often lead to sloppy code with unexpected results.
4

As everyone mentioned, the issue has to do with variable scoping. Instead of add global $connection; you could consider a more OOP approach and consider:

A: passing the $connection variable into the function.

B: placing related functions in a class and pass the DB connection into the Class constructor. for example:

class YourClass  {

   private $connection;

   public function __construct($connection) {
       $this->connection = $connection;
   }

   public function showMsg(){   
        $query2 = "SELECT id, message, username, datetime FROM messageslive ORDER BY id DESC LIMIT 20";
        $result2 = mysql_query($query2,$this->connection) or die (mysql_error());
        confirm_query($result2);
        $num = mysql_num_rows($result2); 
        while($msginfo = mysql_fetch_array($result2)){
            echo $msginfo['message'];
            echo $msginfo['username'];
        }
   }

}

I don't have enough rep to comment. But I also like OZ_'s answer :)

2 Comments

OMG, I have very less knowledge about Class and Object things. May I know any benefits if I use class instead of passing $connection to function parameters?
@Zac1987 It can take a bit of time to learn OOP techniques, but a quick google search on OOP + PHP should bring back something interesting. Trust me learning OOP will benefit you so much in the long run as well as make your code more manageable and clean.
2

$connection variable has no value assigned. The following code should solve your problem (add it at the beginning of the function):

global $connection;

But you should be aware of the fact, that using globals is not a good idea and you may want to:

  • (preferably) pass $connection variable within the parameter of the function, or
  • move $connection declaration from outside the function just into the function (if it does not cause additional problems), or
  • redeclare $connection variable within the function (again: if it will not cause additional problems),

2 Comments

I was wondering where should i put global $connection; Thanks for pointing me :)
No problem :) It should be placed before you use $connection variable - if you put it at the beginning of the function, this should meet this requirement ;)
0

Because variables are local to functions. You need to add this inside your function:

global $connection;

Comments

-2

Simply put, functions ignore outside variables due to variable scope. You must let the declare the variable as being from the outside by using global or you can send $connection through a parameter.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.