Array_rand problem

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
koncling
Forum Newbie
Posts: 2
Joined: Wed Oct 15, 2008 3:03 am

Array_rand problem

Post by koncling »

hello i'm new in PHP code
my friends got some error when running php script and I need your help to fix this error

"Warning: array_rand() [function.array-rand]: First argument has to be an array in /home/.backbones/onnix/www.domain-kuh.co.cc/check.php on line 68"

this is the check.php script

Code: Select all

<?
// 
// 
// 
// 
// 
 
require( "configsis.php" );
session_start();
if( isset( $id ) )
{
dbConnect();
$result = mysql_query( "SELECT * FROM memberarisan WHERE username='$id'" ) or error( mysql_error() );
$member = mysql_fetch_array( $result );
if( mysql_num_rows( $result ) != 1 ) error( "Maaf, Username ini Tidak ada dalam database kami, Jika mau mengakses halaman ini memerlukan sponsor <a href=\"index.php?id=$username_1\">KLIK DISINI</a>" );
$status="nonaktif";
if ( $member[25] == $status ) error( "Maaf Member anda belum kami aktifkan, atau sedang kami blokir karena belum menyelesaikan transaksi dengan kami " );
else
 
 {
    mysql_query("UPDATE memberarisan SET hits=hits+1 WHERE username='$member[1]'") or error( mysql_error() );
    session_register("session_sponsor"); 
    session_register("session_nama"); 
    session_register("session_email"); 
    session_register("session_kota"); 
    session_register("session_bank"); 
    session_register("session_telp"); 
    session_register("session_rekening"); 
    session_register("session_sponsor2"); 
    session_register("session_sponsor3"); 
    session_register("session_sponsor4"); 
    session_register("session_sponsor5");
        
    $session_sponsor=$member[1];
    $session_nama=$member[3];
    $session_email=$member[4];
    $session_kota=$member[6];
    $session_bank=$member[13];
    $session_telp=$member[7];
    $session_rekening=$member[15];
    $session_sponsor2=$member[18];
    $session_sponsor3=$member[19];
    $session_sponsor4=$member[20]; 
    $session_sponsor5=$member[21]; 
    
    return true;
    //exit;
  }
}
else
{
  if( $session_sponsor != "" ) 
  {
     return true;
    //exit;
  }
  else
  {
     dbConnect();
     $status="aktif";
     $resul = mysql_query( "SELECT * FROM memberarisan WHERE stat='$status' " ) or error( mysql_error() );
     $i2 = 0;
     while( $row = mysql_fetch_array( $resul ) )
          {
           $bb2[$i2]=$row[username];
           $i2++;
          }
     $bb12 = array_rand($bb2,1) ;
     $id=$bb2[$bb12];
 
          
     $result = mysql_query( "SELECT * FROM memberarisan WHERE username='$id'" ) or error( mysql_error() );
     $member = mysql_fetch_array( $result );
   
    mysql_query("UPDATE memberarisan SET hits=hits+1 WHERE username='$member[1]'") or error( mysql_error() );
    session_register("session_sponsor"); 
    session_register("session_nama"); 
    session_register("session_email"); 
    session_register("session_kota"); 
    session_register("session_bank"); 
    session_register("session_telp"); 
    session_register("session_rekening"); 
    session_register("session_sponsor2"); 
    session_register("session_sponsor3"); 
    session_register("session_sponsor4");
    session_register("session_sponsor5"); 
        
    $session_sponsor=$member[1];
    $session_nama=$member[3];
    $session_email=$member[4];
    $session_kota=$member[6];
    $session_bank=$member[13];
    $session_telp=$member[7];
    $session_rekening=$member[15];
    $session_sponsor2=$member[18];
    $session_sponsor3=$member[19];
    $session_sponsor4=$member[20]; 
    $session_sponsor5=$member[21]; 
      
        
    return true;
    //exit;
   }            
}
?>
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Array_rand problem

Post by requinix »

Are you sure that the query is returning any results?
koncling
Forum Newbie
Posts: 2
Joined: Wed Oct 15, 2008 3:03 am

Re: Array_rand problem

Post by koncling »

yes...
ok this is the hole script....
Last edited by koncling on Wed Oct 15, 2008 3:50 am, edited 1 time in total.
User avatar
pcoder
Forum Contributor
Posts: 230
Joined: Fri Nov 03, 2006 5:19 am

Re: Array_rand problem

Post by pcoder »

I think the query is not returning any results.
And the first argument in array_rand() should be an array input.
To remove this warning and to let the other code run properly, just declare the $bb2 an array.
User avatar
pcoder
Forum Contributor
Posts: 230
Joined: Fri Nov 03, 2006 5:19 am

Re: Array_rand problem

Post by pcoder »

Check your line number 65:
And place username within single quote.

Code: Select all

 
 $bb2[$i2]=$row['username'];
 
I think it works now.
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: Array_rand problem

Post by Stryks »

Well ... your error message pretty much sums it up.
koncling wrote:array_rand() [function.array-rand]: First argument has to be an array
array_rand() works on arrays, so the variable you are passing to it ($bb2) must not be an array.

A few lines up shows how $bb2 is created, and how it would fail to be created if the query were to return no results.

Code: Select all

    $resul = mysql_query( "SELECT * FROM memberarisan WHERE stat='$status' " ) or error( mysql_error() );
     $i2 = 0;
     while( $row = mysql_fetch_array( $resul ) )
          {
           $bb2[$i2]=$row[username];
           $i2++;
          }
If no results are returned, while doesn't perform an iteration, and $bb2 is not created. So you would then be passing an undefined variable (NULL) to array-parse(), hence the error.

Try ...

Code: Select all

$resul = mysql_query( "SELECT * FROM memberarisan WHERE stat='$status' " ) or error( mysql_error() );
 
if(mysql_num_rows($result) > 0) {
   while($row = mysql_fetch_array( $resul )) $bb2[]=$row['username'];
} else $bb2 = array();
 
The main change is that you're checking how many results were returned. If nothing is returned, you can at least make sure an array is the result instead of leaving the variable unset (or you could check for an unset variable before going to array_rand(), your choice). You'll notice a few other changes in there also. There is no need to use an array index counter when you want to generate an array that uses an incrementing number as a key starting with 0. This behavior with arrays is default, so you can just knock up an array on the fly with $new_array[] = 'hiya'; That's the same thing as saying $new_array[0] = 'hiya'; ... with the obvious benefit of automatically incrementing each time you use it.

Also, when you're using a textual array index (such as $row[username];) the text should always be quoted ($row['username']);

As a side note, you can happily discard those session_register() calls. They really aren't necessary, and can cause issues. See the manual for more info HERE.

But the bottom line ... check your query. It's not returning what you might be hoping for.

Cheers

EDIT: Damn pcoder ... beat me to the punch :lol:
User avatar
pcoder
Forum Contributor
Posts: 230
Joined: Fri Nov 03, 2006 5:19 am

Re: Array_rand problem

Post by pcoder »

Damn pcoder ... beat me to the punch????
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: Array_rand problem

Post by Stryks »

Sorry ... it doesn't read well ... I was referring to the fact that you guys all beat me to the reply. I got distracted part way through.

It wasn't a "Damn that pcoder" ... it was more a "Damn man .. beat me. DOH!!" kinda thing.

No offense meant, I assure you. 8)
User avatar
pcoder
Forum Contributor
Posts: 230
Joined: Fri Nov 03, 2006 5:19 am

Re: Array_rand problem

Post by pcoder »

haha, any way that's really nice and sorry too.. :D
Post Reply