Twitter Taking Too Long To Load
#1
Reputation: 7
- Posts: 167
- Joined: 09-April 09
Script taking way too long to load.
Posted 25 September 2011 - 04:40 PM
Hey, i'm a novice programmer and i've encoutered a error.
The page takes 30 sec + to load and the server simply replyes with the following error:
Fatal error: Maximum execution time of 30 seconds exceeded in C:\ApacheWebServer\htdocs\phpLogin\member.php on line 20
Now the code:
Member.php
<html> <head> <STYLE TYPE="text/css"> <!-- .deepsea, .deepsea TD, .deepsea TH { background-image:url('background.jpg'); background-color:blue; color:white; font-weight:600; } --> </STYLE> </head> <?php //Start the session session_start(); require_once('getDatabase.information.php'); if(isset($_SESSION['username'])) { $username = $_SESSION['username']; $databaseHook = new SilkroadDB(); $userJID = $databaseHook->GetUserJID($username); $userSilk = $databaseHook->GetSilkCount($userJID); echo ' <a href="logout.php">Logout</a><br/>'; echo "<TABLE BORDER=1 CELLPADDING=8 CELLSPACING=0 CLASS='deepsea'> <td> Welcome, $username!</td><tr/> <td> Silks: $userSilk</td> </TABLE>"; }else die("You must be logged in to gain access to this page. <a href='index.php'>Return to Index</a>"); ?> </html>
getDatabase.information.php
<?php //require_once('_conf/conf.inc'); class SilkroadDB { $sql['srv'] = ".\SRO"; $sql['usr'] = "sa"; $sql['pwd'] = "pass"; $sql['acc_db'] = "SRO_VT_ACCOUNT"; $sql['shard_db'] = "SRO_VT_SHARD"; function GetUserJID($username) { $connetion = mssql_connect($sql['srv'], $sql['usr'], $sql['pwd']); mssql_select_db($sql['acc_db']); $query = mssql_query("select * from TB_User where StrUserID='$username'"); if(mssql_num_rows($query)) { while($row = mssql_fetch_assoc($query)) { $userJID = $row['JID']; } } mssql_close($connection); return $userJID; } function GetSilkCount($JID) { $connetion = mssql_connect($sql['srv'], $sql['usr'], $sql['pwd']); mssql_select_db($sql['acc_db']); $query = mssql_query("select * from SK_Silk where JID='$userJID'"); if(mssql_num_rows($query)) { while($row = mssql_fetch_assoc($query)) { $userSilk = $row['silk_own']; } } mssql_close($connection); return $userSilk; } } ?>
I hope someone could help me out. Thanks in advance.
This post has been edited by Cha0sBG: 25 September 2011 - 04:54 PM
Is This A Good Question/Topic? 0
#2 e_i_pi
Reputation: 879
- Posts: 1,893
- Joined: 30-January 09
Re: Script taking way too long to load.
Posted 25 September 2011 - 04:46 PM
Probably not a good idea to post your username and password. Could a mod edit the original post if the OP hasn't got to it yet?
This post has been edited by e_i_pi: 25 September 2011 - 04:47 PM
#3 Cha0sBG
Reputation: 7
- Posts: 167
- Joined: 09-April 09
Re: Script taking way too long to load.
Posted 25 September 2011 - 04:56 PM
I don't care if anyone sees the id and pw , because that server is run on a VM. Just to make you happy i cleared the pw. The issue here is the load time . Any idea what can cause that and make the script not working? It worked fine before i entroduced the class to my script. Before that those mssql commands ware in the member.php file and everything worked perfectly.
#4 JackOfAllTrades
Reputation: 6260
- Posts: 24,030
- Joined: 23-August 08
Re: Script taking way too long to load.
Posted 25 September 2011 - 05:17 PM
Nothing obvious that I can see in that except your use of SELECT * in your queries. In the first query the only field you care about id JID, therefore you should only select that field.
SELECT JID from TB_User where StrUserID='$username'
Same with the second query. You could likely do what you're attempting in a single query with a join:
SELECT silk_own from SK_Silk JOIN TB_User on SK_Silk.JID = TB_User.JID where TB_User.StrUserID='$username'
Your field names are all over the place. You should seek some consistency in your naming patterns.
#5 e_i_pi
Reputation: 879
- Posts: 1,893
- Joined: 30-January 09
Re: Script taking way too long to load.
Posted 25 September 2011 - 06:34 PM
Mmm strange behaviour. I can't see an obvious reason why it would be failing, but I've got these suggestions as general fixes:
- Line 32 of Member.php has a syntax error, in the form of a misplaced and syntactically wrong table row
- Line 7 of getDatabase.information.php has a backslash, which means the S would be escaped. Is this intentional? If you are after the string '.\SRO' you need to use '.\\SRO' to escape the backslash
- AS JackOfAllTrades said, you could optimise those queries, which will result in much quicker execution time (though I don't think it even gets to that stage before timing out)
#6 Cha0sBG
Reputation: 7
- Posts: 167
- Joined: 09-April 09
Re: Script taking way too long to load.
Posted 25 September 2011 - 07:17 PM
I don't need to make the 2 in 1. I will need the separate functions. I have a question, where must i put the require_once('_conf/conf.inc');
When i put it after the class it wants a function, when i put it above the class it doesn't recognize the dbs or anything.
#7 Atli
Reputation: 4241
- Posts: 7,216
- Joined: 08-June 10
Re: Script taking way too long to load.
Posted 25 September 2011 - 11:55 PM
There are a couple of issue there that would cause this to fail, although how it is causing it to reach max execution time I don't know. It should just fail with an error.
First of all, your class has a syntax error. As far as I can remember, no version of PHP allows you to define array elements on member variables outside of a method. At least, PHP 5 does not. And there should be a visibility keyword on all of them. (Or the PHP 4 var keyword, if you are stuck in the dark ages.)
You probably want to do something more like this:
class SilkroadDB { private $srvr = ".\SRO"; private $usr = "sa"; private $pwd = "pass"; private $acc_db = "SRO_VT_ACCOUNT"; private $shard_db = "SRO_VT_SHARD";
And then:
mssql_connect($this->srvr, $this->usr, $this->pwd);
And second, you are calling session_start() after printing out a bunch of HTML, which, unless you have output buffering on, would show a warning and cause the isset($_SESSION['username']) to return false ever time. You should call the function at the very top of the document, before the HTML is printed.
Also, a side from all that: you are opening and closing your database connection unnecessarily. You typically just open it once when the class is created, or when the first query is executed, and then use that same connection for all the queries. Even if it is only used once, keeping the connection open until the script execution ends is not an issue.
class SilkroadDB { private $connection; public function __construct() { $this->conection = mssql_connect($this->srvr, $this->usr, $this->pwd); } }
There is no need to explicitly close the connection. That happens automatically when the script execution ends.
You may even want to go further with this, to prevent multiple instances of the class from creating multiple database connections. For instance, you could do something like this:
class SilkroadDB { // Static attributes, reusable by all instances // of the SolkroadDB. protected static $connection; protected static $srvr = ".\SRO"; protected static $usr = "sa"; protected static $pwd = "pass"; protected static $acc_db = "SRO_VT_ACCOUNT"; /** * Calling this method in the __construct of an * instance will make sure a connection exists, * but won't allow mutliple connections to exist. * The same connection can be used by all instances. */ protected static function InitConnection() { if (!self::$connection) { self::$connection = mssql_connect(self::$srvr, self::$usr, self::$pwd); mssql_select_db(self::$acc_db); } } /** * And then this makes sure the connection * exists before an instance of the class * is created. */ public function __construct() { self::InitConnection(); } }
This can also be extended fairly easily to allow multiple connections, like if you need to open two different connections to two different database servers. Just pass an identifier to the init method and then use a static getter method to get the connection object need.
Another popular way to prevent multiple connections is to create a database singleton class. That's usually what I do. Worth looking into!
#8 JackOfAllTrades
Reputation: 6260
- Posts: 24,030
- Joined: 23-August 08
Re: Script taking way too long to load.
Posted 26 September 2011 - 05:30 AM
How are we supposed to know where to put an include about which we have no knowledge?
#9 Cha0sBG
Reputation: 7
- Posts: 167
- Joined: 09-April 09
Re: Script taking way too long to load.
Posted 26 September 2011 - 08:23 AM
@atli: i'm using the code u suggested but now it wines about undeclared variables more spesificly:
self::srv and $this->$srv does not work aswell. dunno what's going on
#10 Atli
Reputation: 4241
- Posts: 7,216
- Joined: 08-June 10
Re: Script taking way too long to load.
Posted 26 September 2011 - 08:30 AM
You seem to be mixing up the position of the dollar signs. It should be self::$srv and/or $this->srv.
#11 Cha0sBG
Reputation: 7
- Posts: 167
- Joined: 09-April 09
Re: Script taking way too long to load.
Posted 26 September 2011 - 09:28 AM
I forgot to clean out old sql connect requests etc. Now everything is working fine. Thank u very much with your assistance and to all that pitched in with your suggestions and fixes.
This post has been edited by Cha0sBG: 26 September 2011 - 09:29 AM
Twitter Taking Too Long To Load
Source: https://www.dreamincode.net/forums/topic/248549-script-taking-way-too-long-to-load/
Posted by: hershbergerollare.blogspot.com
0 Response to "Twitter Taking Too Long To Load"
Post a Comment