How can I reduce lines of code per file?

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
User avatar
waradmin
Forum Contributor
Posts: 240
Joined: Fri Nov 04, 2005 2:57 pm

How can I reduce lines of code per file?

Post by waradmin »

All over one of my files (profile.php) I have database query's in the form of:

Code: Select all

$result = mysql_query("SELECT * FROM loginphp WHERE Uname='{$_SESSION[Uname]}'") or die(mysql_error());
$row = mysql_fetch_array( $result );
and

Code: Select all

$result = mysql_query("SELECT * FROM friends WHERE global_id='$_GET[id]' AND friend_global_id='$global_id'") or die(mysql_error());
$num_rows = mysql_num_rows($result);
Is there a way to just throw each one of these into a file and call that file each time I need the specific query run?

I mean there are all kinds of querys on tables such as friends, loginphp, profile_photo, wall, education, general and having to constantly add in the query is annoying, I would like to have a master file of my database query's and be able to call the one I want when I want it to reduce lines of code in my files, and eliminate the need for me to type in the query's over and over.

Thanks
-Steve
danharibo
Forum Commoner
Posts: 76
Joined: Thu Aug 17, 2006 8:56 am

Post by danharibo »

make a php file containg all you querys, then call like this:

Code: Select all

DoUserQuery("Bob321");
Would return A mysql Resource containg the user Bob321 :)
User avatar
waradmin
Forum Contributor
Posts: 240
Joined: Fri Nov 04, 2005 2:57 pm

Post by waradmin »

Im not following, how does it know which one to call when each is in the form of $result?
danharibo
Forum Commoner
Posts: 76
Joined: Thu Aug 17, 2006 8:56 am

Post by danharibo »

Put them all in diffrent Functons like
DoUserQuery()
DoNewsQuery()
Etc..
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

You don't want to actually DO the queries before you need them, but you could make a function like this...

Code: Select all

function do_user_query($what)
{
    switch($what)
    {
        case 'bob321':
        return mysql_query("SELECT * FROM `users` WHERE `username` = 'bob321'");
        break;
    }
}
Although, I don't really see that being practical.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
waradmin
Forum Contributor
Posts: 240
Joined: Fri Nov 04, 2005 2:57 pm

Post by waradmin »

Seems more practical than what I am doing now.
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

It's time to read about OOP. :D
User avatar
waradmin
Forum Contributor
Posts: 240
Joined: Fri Nov 04, 2005 2:57 pm

Post by waradmin »

Daedalus- wrote:It's time to read about OOP. :D
That is one of the next things I am learning about in my Computer Science: C++ lecture series, so I may wait until I learn about C++ OOP to apply it to PHP, because much of C++ is similar to PHP as far as syntax and concepts go.
Post Reply