Jump to content

Help me understand this really basic function please


mr cracker

Recommended Posts

Hello all.

 

I'm  learning Object oriented php and  found this simple piece of code online but i don't understand why the variable $con must be returned:

 

 


class Connect
{
   public static function con()
   {
      $con=mysql_connect("localhost","root","");// Connects to DB???
      mysql_query("SET NAMES 'utf8'"); // Sets charset to UTF8
      mysql_select_db("blog"); // Selects the DB
      return $con; // Don't know why $con must be returned, but it wont work without this.
   }
   
}


 

$con is later used in the script like this from another class to run a Query:

 

mysql_query($somequery,Connect::con());

 

 

Thanks for your help.

 

Link to comment
Share on other sites

That's actually not a very good way to do it. You are potentially creating a new connection with every query, which is needless overhead.

 

To answer your question, though, the second parameter of the mysql_query function is the link identifier. Basically, it is which active MySQL connection to use. If you only have one active connection it will automatically use that. The reason you must return $con is because you presumably don't have an active connection until Connect::con() is run. Based on that logic, then, this should also work:

Connect::con();
mysql_query($somequery);

Link to comment
Share on other sites

Thanks for your reply.

 

You are right,  i didn't have an active connection,  i tried your way and it works great and more effectively , i will use it from now on, but it's still not clear for me why do i have to return $con.  (I know i must do it because i don't have an active connection but i don't get why, i don't know what $con is equal to or why is it important)

 

So  the way i'm seeing this is that without returning $con like this:

 

class Connect
{
   public static function con()
   {
      $con=mysql_connect("localhost","root","");// Connects to DB???
      mysql_query("SET NAMES 'utf8'"); // Sets charset to UTF8
      mysql_select_db("blog"); // Selects the DB
    
   }
   
}

 

And when  running this:

 

mysql_query($somequery,Connect::con());// Entire method con() is run? or is it just passing the returned value if any? whatever that is 

 

Wouldn't php  run all 3 lines inside the method/function con() and start a new connection when the the first line is run? with no need to return anything.

 

Thanks for your time.

 

Link to comment
Share on other sites

I know i must do it because i don't have an active connection but i don't get why, i don't know what $con is equal to or why is it important

 

According to this website: http://www.php.net/manual/en/function.mysql-connect.php

 

Return Values: Returns a MySQL link identifier on success or FALSE on failure.

 

mysql_connect() returns a mysql link identifier which is used to identify the connection you have opened with the database. You use this link identifier to determine whether your connection was successful and which connection in particular you are working with.

Link to comment
Share on other sites

This particular piece of code, as already pointed out by scootstah, isn't very good. Your better of learning from another piece. See below for a slightly better way (althought not how I would do it) of doing it.

 

class Sql {
    public static function connect($connectionInfo){
        $connection = mysql_connect($connectionInfo['host'], $connectionInfo['user'], $connectionInfo['pass'];
        mysql_select_db($connectionInfo['database'];

        return $connection;
    }

    public static function query($sql, $connection){
        return mysql_query($sql, $connection);
    }
}

$dbInfo = array(DB INFO HERE);
$sql = new Sql;
$con = $sql->connect($dbInfo);

$query = $sql->query(SQL STRING HERE, $con) ;

 

Essentially you access an objects method, it executes the code wrapped in the methods squiggly brackets and may or may not return a value depending on how you program it.

 

Your particular piece of code adopts the object oriented style but is not a piece of object oriented code as it defeats the methodology of object oriented code due to having a single method in the object.

 

The examples using foobar on the php.net website explain classes far more clearly in my opinion so I would refer to them.

Link to comment
Share on other sites

Essentially you access an objects method, it executes the code wrapped in the methods squiggly brackets and may or may not return a value depending on how you program it.

 

That is all well and good except there is no object in your example.

Link to comment
Share on other sites

Essentially you access an objects method, it executes the code wrapped in the methods squiggly brackets and may or may not return a value depending on how you program it.

 

That is all well and good except there is no object in your example.

 

I do apollogise I wasn't thinking straight, accessing the methods statically as in his example. Adjusted the example accordingly.

Link to comment
Share on other sites

mysql_query($somequery,Connect::con());// Entire method con() is run? or is it just passing the returned value if any? whatever that is 

 

Wouldn't php  run all 3 lines inside the method/function con() and start a new connection when the the first line is run? with no need to return anything.

 

It would, but since there is no active connection at the time mysql_query is called, you have to pass it the connection that was created by Connect::con(). If you call Connect::con() before mysql_query, you don't have to return anything.

 

Link to comment
Share on other sites

I believe the OP is referring to the use of return in the function of his class.  OP asked why he was returning $con.

 

The reason for this is so that there exists a check tool.

 

if( !$con){       Connect::con();       }

 

http://php.net/manual/en/function.return.php

 

Sorry, but that is not the case. Data is returned from functions so it can be used outside of that function. Simple.

Link to comment
Share on other sites

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.