Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

PHP/MySQL Help: Explain "as" and "->"

8:00 pm on Aug 19, 2007 (gmt 0)

New User

10+ Year Member

joined:Oct 17, 2006
votes: 0

Can someone please help me to understand the code below, in particular lines 2, 4 (the sql query) and lines 5 + 6. I thought I understood the use of the -> symbol, but apparently I dont, so some explanation of its use, particulary within this example would be fantastic. Could somone also please explain the use of the word "as" within the mysql query, and how it ties together the use of the word howmany and the -> symbol.

Sorry its a lot to ask in one go! Ive basically been asked to modify somone elses code and ive not really seen PHP/MySQL laid out like this before. The code itself doesnt seem to be OO which is why im not understanding the use of -> as ive only ever seen it used in that context.

1. function count_mail() {
2. $recipient = $_SESSION['user_data']->user_id;
4. $sql = mysql_fetch_object(mysql_query("SELECT COUNT(message_id) as howmany FROM mailbox WHERE recipient=$recipient"));
5. if ($sql->howmany >= 1) {
6. $unread = $sql->howmany;
7. return $unread;
8. }
9. else {
10. $unread = '0';
11. return $unread;
12. }
13. }

[edited by: Sonnenblume at 8:02 pm (utc) on Aug. 19, 2007]

8:51 pm on Aug 19, 2007 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Jan 16, 2007
votes: 0

Line 2 is written that way because apparently the session data is saved as an object; it's getting the user_id property from the user_data object stored in the session. If you were to change how that's done, you'd have to go ferret it out everywhere to change it.

Line 4 is getting the query row as an object (as opposed to mysql_fetch_array, assoc, or row). Lines 4, 5, and 6 could be rewritten as:

$sql = mysql_fetch_assoc(mysql_query("SELECT COUNT(message_id) as howmany FROM mailbox WHERE recipient=$recipient"));
if ($sql['howmany'] >= 1) {
$unread = $sql['howmany'];

The AS in the query just tells mysql what name you want to use for the field; it's pretty handy sometimes.

8:55 pm on Aug 19, 2007 (gmt 0)

Junior Member

10+ Year Member

joined:May 16, 2005
votes: 0

Do you know what Object Oriented Programming (OOP) is? As a quality programmer, it's a good thing to know.

Here's a very very simple analogy of what you're looking at:

Think about a car. Now think about how many tires it has.
For simplicity sake, let's say the car has four tires. In OOP this could be depicted with variables as:

$car->numTires = 4

The command in your sql query: "mysql_fetch_object" is taking the results returned from the query, and turning them into an object. Similar to how "mysql_fetch_array", would turn the results into an array.

So "$sql->howmany" is the number of db entries in the table "mailbox" where the recipient field equals the variable $recipient.

You should invest in a beginning computer science book, and read up about OOP. The "->" terminology is used in most all C based languages (php is one of them), and the OOP methodology is a driving force behind the Java language.

If your programming career progresses, this is not the last time you will see OOP in PHP.



Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members