mr cracker Posted March 13, 2012 Share Posted March 13, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/258797-help-me-understand-this-really-basic-function-please/ Share on other sites More sharing options...
scootstah Posted March 13, 2012 Share Posted March 13, 2012 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); Quote Link to comment https://forums.phpfreaks.com/topic/258797-help-me-understand-this-really-basic-function-please/#findComment-1326682 Share on other sites More sharing options...
mr cracker Posted March 13, 2012 Author Share Posted March 13, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/258797-help-me-understand-this-really-basic-function-please/#findComment-1326728 Share on other sites More sharing options...
Proletarian Posted March 13, 2012 Share Posted March 13, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/258797-help-me-understand-this-really-basic-function-please/#findComment-1326734 Share on other sites More sharing options...
cpd Posted March 13, 2012 Share Posted March 13, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/258797-help-me-understand-this-really-basic-function-please/#findComment-1326746 Share on other sites More sharing options...
trq Posted March 13, 2012 Share Posted March 13, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/258797-help-me-understand-this-really-basic-function-please/#findComment-1326748 Share on other sites More sharing options...
cpd Posted March 13, 2012 Share Posted March 13, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/258797-help-me-understand-this-really-basic-function-please/#findComment-1326750 Share on other sites More sharing options...
scootstah Posted March 13, 2012 Share Posted March 13, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/258797-help-me-understand-this-really-basic-function-please/#findComment-1326775 Share on other sites More sharing options...
Mahngiel Posted March 13, 2012 Share Posted March 13, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/258797-help-me-understand-this-really-basic-function-please/#findComment-1326803 Share on other sites More sharing options...
trq Posted March 13, 2012 Share Posted March 13, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/258797-help-me-understand-this-really-basic-function-please/#findComment-1326809 Share on other sites More sharing options...
Mahngiel Posted March 13, 2012 Share Posted March 13, 2012 Indeed. And in my example, if $con did not exist, then the connection class would be run. Quote Link to comment https://forums.phpfreaks.com/topic/258797-help-me-understand-this-really-basic-function-please/#findComment-1326811 Share on other sites More sharing options...
scootstah Posted March 13, 2012 Share Posted March 13, 2012 Indeed. And in my example, if $con did not exist, then the connection class would be run. In your example $con would never exist, even if the function was run. Quote Link to comment https://forums.phpfreaks.com/topic/258797-help-me-understand-this-really-basic-function-please/#findComment-1326813 Share on other sites More sharing options...
Mahngiel Posted March 13, 2012 Share Posted March 13, 2012 I wasn't intending to judge the functionality of the code, just describing the purpose of the return. but if OP did return $con = TRUE, then he could eval $con as true/false and run accordingly. Quote Link to comment https://forums.phpfreaks.com/topic/258797-help-me-understand-this-really-basic-function-please/#findComment-1326823 Share on other sites More sharing options...
scootstah Posted March 13, 2012 Share Posted March 13, 2012 Huh? The purpose of the return is to supply mysql_query() with an active connection, nothing more. Quote Link to comment https://forums.phpfreaks.com/topic/258797-help-me-understand-this-really-basic-function-please/#findComment-1326826 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.