homepage Welcome to WebmasterWorld Guest from 184.73.104.82
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

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

5+ Year Member



 
Msg#: 3426265 posted 8:00 pm on Aug 19, 2007 (gmt 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;
3.
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]

 

cameraman

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 3426265 posted 8:51 pm on Aug 19, 2007 (gmt 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.

Judah_Ben

5+ Year Member



 
Msg#: 3426265 posted 8:55 pm on Aug 19, 2007 (gmt 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.

Regards,
-JB

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved