A couple questions from a newbie

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
Katixa
Forum Newbie
Posts: 2
Joined: Sat Nov 16, 2002 6:50 am

A couple questions from a newbie

Post by Katixa »

Hi,

I'm converting my ASP site to PHP with MySQL, and I've been encountering some problems, as I have just started with PHP. Some of them have been solved after searching and reading lots of sites and tutorials. But others don't.

Basically, I have a page that has around 1.300 lines of code, with only functions. Functions for searching a nickname, the number of posts of an user, the forum tittle, the avatar, etc. Now I am converting that file to PHP, but I have a problem.

I made a "testphp.php" page referencing those functions I'm slowly converting, but each load of the page is *very* slow. I test it in a localnetwork with my ADSL connection. The "server" is a PII 233 with 64Mb RAM, running Win2000 with MySQL (with the FoxServ package). My main question is: Am I doing right including this "functions.php" file? Is it too big? Should I change my working style?

On another topic, with the ASP I used to open all the database connections in the global.asa file, when the whole application (website) is started, so in each database access I only needed to switch to the right connection, like "set active conection to xxxx". So only ONE conecction is opened always, regardless of the users accesing the site. But now with PHP I find nothing about Application object, or about a global connection.

I heard that MySQL closes connections but... Can I set a "global" connection instead of using mysql_connect statement in each file (even with an included file).

Or can I just add a function with something like this?
function sConnect($pDatabase)
{
mysql_connect("host","user","pass");
mysql_select_db($pDatabase);
}

So this way, I only have to use:

sConnect("forums");

Is this possible?


Oh... and another "small" doubt. That function file contains the connect and select_db statement on each function. Does the server try to run that to check if is right each time? Or does it only connect when I call the function?

Is just that is getting too strange to me to set a connection with each function.


And for ending this message, I would like to ask about "Active visitors". With ASP I use the global.asa file to add 1 to the Session variable that contains the value but... no global.asa in PHP... what is the equivalent file, if any?

Thanks for any answers.
User avatar
brianw84
Forum Newbie
Posts: 18
Joined: Fri Nov 15, 2002 9:01 pm
Location: Jax|Fla

mysql connection

Post by brianw84 »

im a newbie to and alot of the stuff your talking about is over my head but i think i can help u out in regards to your mysql connection. you can set up a persistent connection to a mysql d-base by useing this code:

Code: Select all

<?php
$linkID = mysql_pconnect("localhost,"","");
?>
hope that helps
MeOnTheW3
Forum Commoner
Posts: 48
Joined: Wed Nov 13, 2002 3:28 pm
Location: Calgary, AB

Post by MeOnTheW3 »

My main question is: Am I doing right including this "functions.php" file? Is it too big? Should I change my working style?
The first thing I would do is catagorize your functions into specific class files and separate and only include if/when needed.

...with the ASP I used to open all the database connections in the global.asa file, when the whole application (website) is started...
In the PHP main directory search for a file named "common_db*.*" (I can not remember if it is competed with .inc or .inc.php) This file is perfect for using as a global db connection manager. All you need to do is define the $host, $user, $default_db, etc... in the file, then include the file in any page you access the mySQL server and make connections like:

Code: Select all

<?php
$conn_1 = db_connect('db_1_name');
$conn_2 = db_connect('db_2_name');
?>
If you set multiple connections in PHP you need to specify which one you wish to use with each call:

Code: Select all

<?php
$sql_1 = 'SELECT * FROM some_where ORDER BY some_thing';
$result_1 = mysql_query($sql_1,$conn_2);
...

$sql_2 = 'SELECT A.*, B.id, C.name FROM some_table A, another_table B, and_one_more C WHERE ((A.id=B.name) && (B.something=C.something_else) && A.wild_card=''' . $some_var . ''') ORDER BY B.datetime LIMIT 0,30';
$result_2 = mysql_query($sql_2,$conn_1);
?>
about "Active visitors". With ASP I use the global.asa file to add 1 to the Session variable
Get the PHP manual from http://www.php.net and look up Session Handling functions built-in to PHP 4+.

Good luck, and I hope you find the jump over to PHP as great as I have.
Katixa
Forum Newbie
Posts: 2
Joined: Sat Nov 16, 2002 6:50 am

Ouch

Post by Katixa »

Thanks for the answers.

I think is going to be harder than I thouth. About categorizying the function file... a bit "difficult".

My site consists in 3 main parts. A top, a menu and a body. The menu and the top are both an included file in each page. The top acceses some of those functions to the the name of the user, number of posts, private message checking and some others, as does the menu to check other configurations. The top file is the one including that 32kb function file, in this way all the pages got that function file, so I can call any function in any page (like any library).

If I have to include only a few functions here and a few functions there, and then copy and pasting when making new features, I think that the main use of an include file goes away, and I return to my old style (very long ago)... that is having the functions on each page.

Well... I could check if I can have functions like "text manipulation", "user database access" etc. separated. Thanks for the sugestion.


Yesterday I spent some hours converting some functions (still 80% of file to do). Of course with the PHP.net manual opened and another one localy. I had to find some never-used-before basic functions like substr, str_replace and ord, instead of mid, replace and asc of VBScript. I find PHP interesting, but the change makes hard sometimes. I hope I can get my site working the same (for the user) after the conversion, even if I have to change all my working style.
Post Reply