spode Posted May 21, 2007 Share Posted May 21, 2007 Hey guys, first just wanted to say, great site and I'm already learning a ton. However, in the book I bought (PHP for the WWW by Larry Ullman) it looks like he assigns the connection as a variable. Let me explain by example. <?php $host = "localhost"; $username = "imcool"; $password = "123456"; $database = "acoolone"; if ($dbc = mysql_connect ($host, $username, $password)) { echo "Connected"; if (!mysql_select_db ($database)) { die ('<p>Could not select the database because: <b>' . mysql_error() . '</b></p>'); } } else { die ('<p>Could not connect to MySQL because: <b>' . mysql_error() . '</b></p>'); } ?> To me (as a n00bie) it looks like he's assigning the variable $dbc inside the if statement arguments. Am I right? If so is it neccessary? However, I notice in many scripts you guys do it like so: <?php $host = 'localhost'; $user = 'username'; $password = 'password'; $dbName = 'databasename'; $conn = mysql_connect($host, $user, $password) or die(mysql_error()); $db = mysql_select_db($dbName, $conn) or die(mysql_error()); Could someone explain the difference, and what is going on? Thanks for the help! Quote Link to comment https://forums.phpfreaks.com/topic/52403-assign-variables-to-connect-to-mysql/ Share on other sites More sharing options...
spode Posted May 21, 2007 Author Share Posted May 21, 2007 7 views no help Quote Link to comment https://forums.phpfreaks.com/topic/52403-assign-variables-to-connect-to-mysql/#findComment-258613 Share on other sites More sharing options...
john010117 Posted May 21, 2007 Share Posted May 21, 2007 Please wait at least an hour before double-posting. We all have lives, you know. Just to simplify things, use the second code. It's guaranteed to work. Quote Link to comment https://forums.phpfreaks.com/topic/52403-assign-variables-to-connect-to-mysql/#findComment-258618 Share on other sites More sharing options...
spode Posted May 21, 2007 Author Share Posted May 21, 2007 Ok, sorry about the double post. But why are you using things like $dbc = .....I still don't understand why it's being assigned as a variable ??? Quote Link to comment https://forums.phpfreaks.com/topic/52403-assign-variables-to-connect-to-mysql/#findComment-258621 Share on other sites More sharing options...
trq Posted May 21, 2007 Share Posted May 21, 2007 The first example gives you more opportunity to customize your error output rather than just calling die() and showing the default mysql_error(). But why are you using things like $dbc = .....I still don't understand why it's being assigned as a variable mysql_connect returns a connection resource which can later optionally be passed to mysql_query. Assigning your connection resource to a variable isn't really required, though it can come in handy when dealing with multiple databases. Quote Link to comment https://forums.phpfreaks.com/topic/52403-assign-variables-to-connect-to-mysql/#findComment-258625 Share on other sites More sharing options...
spode Posted May 21, 2007 Author Share Posted May 21, 2007 So will it go ahead and connect when you do it like that? Or do you have to call the variable? Thanks for all the help BTW. Quote Link to comment https://forums.phpfreaks.com/topic/52403-assign-variables-to-connect-to-mysql/#findComment-258638 Share on other sites More sharing options...
trq Posted May 21, 2007 Share Posted May 21, 2007 No, you do not need to call the variable. Both methods make a valid connection. Quote Link to comment https://forums.phpfreaks.com/topic/52403-assign-variables-to-connect-to-mysql/#findComment-258640 Share on other sites More sharing options...
Barand Posted May 21, 2007 Share Posted May 21, 2007 if ($dbc = mysql_connect ($host, $username, $password)) is equivalent to $dbc = mysql_connect ($host, $username, $password); if ($dbc) As you observed, the assigment is done in the if(). The value of an assignment is the value assigned to the left-hand variable. The alternative method relies on evaluation using an "or" condition. Consider if (a or b) if a is true, there is no need to evaluate b as the condition is already satisfied. So with $conn = mysql_connect($host, $user, $password) or die(mysql_error()); if connection ok it's value is assigned to $conn if the connect fails and returns false then the "die()" is evaluated. Quote Link to comment https://forums.phpfreaks.com/topic/52403-assign-variables-to-connect-to-mysql/#findComment-258643 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.