Jump to content

load takes time


robert_gsfame

Recommended Posts

back to previous loop problem, i have this code...but i am not sure why it takes time to load the page

 

<?php

require_once('config.php');

$username="James";

$checkfirstuser=mysql_query(sprintf("SELECT * FROM test WHERE username='%s'",

mysql_real_escape_string($username)));

$i=1;

$tot=0;

while($i==1){

$rand=rand(0,9);

while($checkfirstuserarray=mysql_fetch_array($checkfirstuser)){

$number=$checkfirstuserarray['id'];

if($rand==$number){

$tot++;}

}

if($tot==0){

$i++;}else{}

}

echo $rand;

?>

 

Link to comment
https://forums.phpfreaks.com/topic/201871-load-takes-time/
Share on other sites

You're loop is similar to the one I rewrote in your other post.

 

Instead of

<?php
$i=1;
$tot=0;
while($i==1){
$rand=rand(0,9);
while($checkfirstuserarray=mysql_fetch_array($checkfirstuser)){
$number=$checkfirstuserarray['id'];
if($rand==$number){
$tot++;}
}
if($tot==0){
$i++;}else{}
} 
?>

Try

<?php
$checkfirstuserarray=mysql_fetch_assoc($checkfirstuser);
$number=$checkfirstuserarray['id'];
$rand = rand(0,9);
while ($rand == $number) {
    $rand = rand(0,9);
}
echo $rand;
?>

 

BTW, get into the habit of properly indenting your code, it makes reading & debugging code much easier. Also you don't need an "else" clause if there is nothing in it.

 

Ken

Link to comment
https://forums.phpfreaks.com/topic/201871-load-takes-time/#findComment-1058791
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.