gvp16 Posted April 30, 2008 Share Posted April 30, 2008 Hi all, im developing a website that assigns a random number to a user when they go on to the site and stores it in a session( this is later used as part or a shopping cart), well every so often the site completely locks up and will not load for a good deal of time. after a long time of waiting an error is some times produced saying " fatal error maximum execution time of 30 seconds exceeded in (page name) on line (lineno)" when checking the line of page it refers to its blank, but the closet thing to it is an include for the random number script. this is why i thing its causeing my problem. if anyone has some clue how to fix this it would be great. this is the code i am using to get the random number : <? while ($_SESSION["randno1"] == "") { $randno=rand(1, 1000000); $randquery="SELECT * FROM basket WHERE randid like '$randno'"; $result=mysql_query($randquery) or die ("couldnt do it!"); $numrows=mysql_num_rows($result); If ($numrows==0) { $_SESSION["randno1"]=$randno; } } ?> Link to comment https://forums.phpfreaks.com/topic/103562-website-locking-up/ Share on other sites More sharing options...
947740 Posted April 30, 2008 Share Posted April 30, 2008 There could be a problem with the search of the database for the random number. If the database is humongous, it might not be able to search the entire thing in 30 seconds. Link to comment https://forums.phpfreaks.com/topic/103562-website-locking-up/#findComment-530285 Share on other sites More sharing options...
gvp16 Posted April 30, 2008 Author Share Posted April 30, 2008 the table where the number is stored in isnt very large, its only used while the user adds things to the basket. there are only records in the table when a user has added something to the basket. Link to comment https://forums.phpfreaks.com/topic/103562-website-locking-up/#findComment-530289 Share on other sites More sharing options...
947740 Posted April 30, 2008 Share Posted April 30, 2008 Try: <? while (!isset($_SESSION["randno1"])) { $randno=rand(1, 1000000); $randquery="SELECT * FROM basket WHERE randid like '$randno'"; $result=mysql_query($randquery) or die ("couldnt do it!"); $numrows=mysql_num_rows($result); If ($numrows==0) { $_SESSION["randno1"]=$randno; } } ?> I have a question about your code. You query the database for results comparable to the random number, but don't you have to make an entry into the database with that random number before you query the database? Link to comment https://forums.phpfreaks.com/topic/103562-website-locking-up/#findComment-530291 Share on other sites More sharing options...
The Little Guy Posted April 30, 2008 Share Posted April 30, 2008 Try something like this: <?php while ($_SESSION["randno1"] == "") { $randquery="SELECT * FROM basket ORDER BY RAND() LIMIT 1"; $result=mysql_query($randquery) or die ("couldnt do it!"); If (mysql_num_rows($result)==1) { $row = mysql_fetch_array($result); $_SESSION["randno1"]=$row['randid']; break; } } ?> Link to comment https://forums.phpfreaks.com/topic/103562-website-locking-up/#findComment-530294 Share on other sites More sharing options...
gvp16 Posted April 30, 2008 Author Share Posted April 30, 2008 the idea behind it is that, it generates a number, then checks the database to see if it already exsists, if does it makes another, if it does it stores the number generated in a session. if i used this : <?php while ($_SESSION["randno1"] == "") { $randquery="SELECT * FROM basket ORDER BY RAND() LIMIT 1"; $result=mysql_query($randquery) or die ("couldnt do it!"); If (mysql_num_rows($result)==1) { $row = mysql_fetch_array($result); $_SESSION["randno1"]=$row['randid']; break; } } ?> it wouldnt generate a number for me would it? Thanks for you replys guys. Link to comment https://forums.phpfreaks.com/topic/103562-website-locking-up/#findComment-530327 Share on other sites More sharing options...
The Little Guy Posted April 30, 2008 Share Posted April 30, 2008 This line selects the random value from the database: $randquery="SELECT * FROM basket ORDER BY RAND() LIMIT 1"; So now you have your id number, which would be the same value as your "$randno", so I was able to completely remove that line. Link to comment https://forums.phpfreaks.com/topic/103562-website-locking-up/#findComment-530339 Share on other sites More sharing options...
gvp16 Posted April 30, 2008 Author Share Posted April 30, 2008 ah rite ok, ill give it a go and let you know how it works out. Thanks Link to comment https://forums.phpfreaks.com/topic/103562-website-locking-up/#findComment-530343 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.