Trying to understand use of ->

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
maxd
Forum Commoner
Posts: 41
Joined: Sun Dec 04, 2005 12:12 am
Location: Denver

Trying to understand use of ->

Post by maxd »

OK, this will certainly be the newbie award-winner of the week. :(

I have done some PHP development, but for some reason never encountered the use of -> in PHP before. I am trying to figure out just what it does? As in:

$sql = 'INSERT INTO user (username, passwordHash) VALUES (?, SHA1(?))';
$result = $db->query($sql, array($_POST['username'], $_POST['password']));

What exactly does the $db->query part of that code do? I searched and searched, went all through PHP.net documentation, and found no official explanation of "->" anywhere, although it is used extensively in example code, both in the documentation and in people's examples. I worked around my ignorance by just rewriting the sql insert to not use this array setup, but it's driving me crazy not knowing what such a fundamental syntax element does.

Any help is greatly appreciated.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

in that sample:

$db is the instantiated object of some database class
query() is a method within that class.

so when you use $db->query(), you're calling the query() method for your class instance ($db).
AGISB
Forum Contributor
Posts: 422
Joined: Fri Jul 09, 2004 1:23 am

Post by AGISB »

Just look again under OOP or Objects. You will find it there
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

here....this will get you started:

http://www.php.net/classobj
maxd
Forum Commoner
Posts: 41
Joined: Sun Dec 04, 2005 12:12 am
Location: Denver

thanks...

Post by maxd »

Jcart | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


Thank you for your prompt reply. 

So, is that what [b][color=red]->[/color][/b] always does? "Calls" a method or a function?

I figured the problem when I was trying to use this code was the $db, since I certainly hadn't instantiated it anywhere. I tried a variety of ways of getting around it.

Here's what I ended up with (which works fine, as far as I can tell):

*****code snippet*****

Code: Select all

<?php 

if(isset($_POST['username']) || isset($_POST['password'])) {

// Database Connection

$server = "myserver"; // 
$username = "myuserid"; // 
$password = "mypass"; // 

/* Connects to the MySQL server */

$link = @mysql_connect ($server, $username, $password)
or die (mysql_error());

/* Defines the Active Database for the Connection */

if (!@mysql_select_db("mydatabase", $link)) {   
     echo "<p>There has been an error. This is the error message:</p>";
     echo "<p><strong>" . mysql_error() . "</strong></p>";
     echo "Please Contact Your Systems Administrator with the details";
}

/* Store user details */

$myusername = ($_POST['username']);
$passwordHash = sha1($_POST['password']);

$sql = "INSERT INTO mydatabasetable (username,password) VALUES ('$myusername','$passwordHash')";
$result = mysql_query($sql);

if (!$result) {
   echo "DB Error, could not query the database\n";
   echo 'MySQL Error: ' . mysql_error();
   exit;
}
?>
*****end code*****

How would I go about utilizing the array method described in the sample code I provided? And what are the advantages of one way over the other?

Thanks very much for your insight into this.

Max


Jcart | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
Post Reply