Web Design Forums

PHP

Have questions about PHP? Ask them here and our experts will assist you before you know it! You can also find help in the documentation at PHP.net.

Confused about connecting to MySQL with PHP



Site of the Month Voting - Now Open. CAST YOUR VOTE NOW!

Reply
 
LinkBack Thread Tools
Old April 5 '07, 06:44 PM (#1)
LearningNerd is offline
New Member!
 
LearningNerd's Avatar
 
Join Date: November 2006
Posts: 6
LearningNerd is an unknown quantity at this point
Send a message via Yahoo to LearningNerd
Confused about connecting to MySQL with PHP

The PHP manual has an example script. I'm confused by this first part:

PHP Code:
// Connecting, selecting database
$link mysql_connect('mysql_host''mysql_user''mysql_password')
    or die(
'Could not connect: ' mysql_error());
echo 
'Connected successfully';
mysql_select_db('my_database') or die('Could not select database'); 
It looks like they're just setting the $link variable equal to a function, not actually executing the function. So how/why does this code work to connect to the database?

I'm also confused about how they're using "or die(...)" -- you can set variables equal to "function() or function()"? I've never seen this before!

And then where it echos 'Connected successfully', why would that only happen if the connection works? I don't see any form of an if statement or anything.

In short, I'm just confused by this whole thing, lol. Why does this work? Is this how you connect to MySQL, or do you do something different? Thanks in advance!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old April 5 '07, 06:54 PM (#2)
bfsog is offline
Coder
 
bfsog's Avatar
 
Join Date: May 2003
Location: UK
Posts: 2,354
bfsog is a splendid one to beholdbfsog is a splendid one to beholdbfsog is a splendid one to beholdbfsog is a splendid one to beholdbfsog is a splendid one to beholdbfsog is a splendid one to beholdbfsog is a splendid one to behold
Send a message via MSN to bfsog
Quote:
It looks like they're just setting the $link variable equal to a function, not actually executing the function. So how/why does this code work to connect to the database?
- $link is the identifier to the connection. That line of code does call the function mysql_connect which creates a connection.

Quote:
I'm also confused about how they're using "or die(...)" -- you can set variables equal to "function() or function()"? I've never seen this before!
- What die() does is if theres a fatal PHP error, you can give PHP some relevant text to output, rather than the fatal error's message that occured.

Quote:
And then where it echos 'Connected successfully', why would that only happen if the connection works? I don't see any form of an if statement or anything.
- Furthermore to die, it stops the execution of the PHP code. So if the function call caused a fatal error, the die message will be displayed. Otherwise, the execution of the source code will continue. That is why there is no if statement.

Last edited by bfsog; April 5 '07 at 06:55 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old April 5 '07, 07:07 PM (#3)
LearningNerd is offline
New Member!
 
LearningNerd's Avatar
 
Join Date: November 2006
Posts: 6
LearningNerd is an unknown quantity at this point
Send a message via Yahoo to LearningNerd
Thanks, bfsog! Makes a lot more sense now.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old April 5 '07, 08:38 PM (#4)
raspberryh is offline
Retired Staff
 
raspberryh's Avatar
 
Join Date: August 2004
Location: Mountain View, CA
Posts: 859
raspberryh has a spectacular aura aboutraspberryh has a spectacular aura about
Quote:
Originally Posted by LearningNerd
It looks like they're just setting the $link variable equal to a function, not actually executing the function.
Hey just wanted to mention that they're not setting it equal to the function, they're setting it equal to the OUTPUT of the function. So it DOES execute the function, and then puts the output (the return variable) into $link.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old April 8 '07, 03:18 PM (#5)
leprechaun13 is offline
WDF Regular
 
leprechaun13's Avatar
 
Join Date: May 2005
Location: Northampton
Posts: 487
leprechaun13 will become famous soon enough
Send a message via AIM to leprechaun13 Send a message via MSN to leprechaun13
and answer to initial question the mysql functions are executed becasue they are use in a variable, not another function which would mean they wouldnt be excecuted untill the function was called, the mysql functions are put into variables to make it easier for using them later on in the script.

one thing i wouldnt put the connect string in every page but rather 1 page and include it where need like this.

PHP Code:
<?php

// Change all of the values on the right

define("DB_SERVER","localhost"); // Defines Constant Called DB_SERVER value localhost
define("DB_USER","root"); // Defines Constant Called DB_USER value root
define("DB_PASS","password");  // Defines Constant Called DB_PASS value password
define("DB_NAME","somedatabase");  // Defines Constant Called DB_NAME value somedatabase

$connection mysql_connect(DB_SERVERDB_USERDB_PASS); /// Connects to the data base with the constants define earlier

mysql_select_db(DB_NAME$connection); // Selects the database defined in the constant and with the database connection $connection.
?>
then just include that files where needed

hope this helps
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old April 8 '07, 03:40 PM (#6)
rosland is offline
Retired Staff
 
rosland's Avatar
 
Join Date: July 2003
Location: Norway
Posts: 1,939
rosland is a jewel in the roughrosland is a jewel in the roughrosland is a jewel in the roughrosland is a jewel in the rough
Another thing you should keep in mind, is that what appears to be a variable ($link in your question) is not actually containing anything readable. It is a resource identifier!

In many other PHP constructs, you'll run into error messages like "illegal resource identifier".
You will also run into problems accessing "variables" that are resultsets.
A resultset is a refence to a data set, that can be accessed by a variety of abstracted functions to iterate through and present tabular data, or load arrays.

They are basically internal pointers to the scripting engine. They are used to propagate actions in other bulit in functions.
You could of course ask why such detours are neccesary, but the reason is that a lot of functionality depends on internal reference to other data objects. Instead of writing a thousand functions that covers all bases, the language gives you the option of using a resultset in various forms to instantiate different results, depending on what you want to achieve.

A connection link can be used for a variety of purposes, if you want to use it to execute a query, then that is one of the options you have with that resultset.

Last edited by rosland; April 12 '07 at 01:29 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old April 8 '07, 07:18 PM (#7)
Shadowfiend is offline
Code beautifully and honorably
 
Shadowfiend's Avatar
 
Join Date: June 2005
Location: Atlanta, GA
Posts: 4,143
Shadowfiend has a reputation beyond reputeShadowfiend has a reputation beyond reputeShadowfiend has a reputation beyond reputeShadowfiend has a reputation beyond reputeShadowfiend has a reputation beyond reputeShadowfiend has a reputation beyond reputeShadowfiend has a reputation beyond reputeShadowfiend has a reputation beyond reputeShadowfiend has a reputation beyond reputeShadowfiend has a reputation beyond reputeShadowfiend has a reputation beyond repute
And as a final note, to address your original question of:
Quote:
Originally Posted by LearningNerd
I'm also confused about how they're using "or die(...)" -- you can set variables equal to "function() or function()"? I've never seen this before!
Or is an operator, and it's what we call `short-circuiting' operator. Basically, saying `true or false' will return true, but it will return true without even *looking* at false, because you don't need to -- the result of an or is true if *any* of its parts are true.

Now, in PHP, `null' is equivalent to `false'. So, if I say `true or null', we get the same result (again, because we only look at true), and if I say `null or true', we also get `true'. Typically, anything that *isn't* null or false or 0 (I believe those are all of the false values in PHP) evaluates as `true' for the purposes of ors or conditionals. Moreover, what `or' does isn't return `true' or `false'. It returns the first value that doesn't evaluate to false. So `5 or false' would return 5, `6 or 7 or 8 or 9' would return `6', and so on.

Progressing to the next step, mysql_connect will return a null pointer, or a null value, when it fails to connect. We just mentioned that `or' won't process anything past the first value that evaluates to not-false. So, when we do this:
PHP Code:
mysql_connect/* ... */ ) or die( "Got an error, stopping script..." 
We know that if mysql_connect returns something that isn't null (i.e., it succesfully connects), then die() won't even run, and the value of that entire expression will be the value returned by mysql_connect.

Similarly, when mysql_connect returns null (i.e., it fails to connect), then die() will run, and it will kill the output with the error message you pass it, and execution of the PHP will stop there.

Hope that made sense

Last edited by Shadowfiend; April 8 '07 at 07:19 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

  Web Design Forums » Programming Help » PHP

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Top 21 PHP Programming mistakes thexchord PHP 19 June 18 '10 02:20 PM
PHP and MySQL issues: PHP not loading extensions!! darknailblue PHP 5 January 2 '07 09:02 PM
Prepared statements (PHP 5 / MySQL 4.1.x) rosland Coding Articles & Tutorials 0 February 25 '05 09:18 PM
Connecting to mysql Eddy Bones Database Systems Help 4 December 17 '04 01:37 PM
need PHP and mySQL help jbagley PHP 4 November 4 '04 07:24 AM

 
User Infomation
Your Avatar

Site Of The Month
Nominate Your Site Now!

Advertisement
WolfCMS.org

Latest Articles
- by RickM
- by bfsog

Advertisement

Partner Links



All times are GMT -4. The time now is 02:10 PM.


WebDesignForums.net is Copyright © 2010 RikeMedia.

SEO by vBSEO

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164