SitePoint Sponsor

User Tag List

Results 1 to 16 of 16
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2008
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Fill PHP array from mySQL table

    I am trying to use a mysql query to fill a PHP array but it doesn't seem to work correctly. Here is the code I am using now:

    PHP Code:
    $a = array();
    $rt mysql_query("SELECT * FROM users WHERE upline='{$ir['username']}' AND uplinetype=2");
    while(
    $r=mysql_fetch_array($rt))
    {
        for(
    $count=0$count $ir['referrals']; $count++)
        {
            
    $a[$count] = $r['username'];
        }
    }
        
    foreach(
    $a as $value) {
       echo 
    "$value\n";

    the $ir[] array is defined elsewhere and I know this isn't the problem. Am I filling the array wrong or is something wrong with the code? Any help is appreciated. Thanks.

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Your request is a little strange, but this should work...

    PHP Code:
    <?php
    $aArray 
    = array();
    $sSQL sprintf("SELECT username FROM users WHERE upline='%s' AND uplinetype=2",
        
    mysql_real_escape_string($ir['username'])
    );
    $rResult mysql_query($sSQL);
    while (
    $aRow mysql_fetch_assoc($rResult))
    {
        
    $aArray array_fill(count($aArray), count($ir['referrals']), $aRow['username']);
    }
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    SitePoint Enthusiast
    Join Date
    Jul 2008
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm this seems to print only one value?

    I am using this:

    PHP Code:
    $aArray = array();
    $sSQL sprintf("SELECT username FROM users WHERE upline='%s' AND uplinetype=2",
        
    mysql_real_escape_string($ir['username'])
    );
    $rResult mysql_query($sSQL);
    while (
    $aRow mysql_fetch_assoc($rResult))
    {
        
    $aArray array_fill(count($aArray), count($ir['referrals']), $aRow['username']);
    }
        
    foreach(
    $aArray as $value) {
       echo 
    "$value\n";

    It only prints one result when it should print 3

  4. #4
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    That's all your original script would have done, what are looking to achieve?
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  5. #5
    SitePoint Enthusiast
    Join Date
    Jul 2008
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I want to print everything that the mysql query returns and store those values in an array. There should be multiple values where upline="$ir['username'] and uplinetype='2'.

  6. #6
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    OK, give this a whirl...
    PHP Code:
    <?php
    $aArray 
    = array();
    $sSQL sprintf("SELECT * FROM users WHERE upline='%s' AND uplinetype=2",
        
    mysql_real_escape_string($ir['username'])
    );
    $rResult mysql_query($sSQL);
    while (
    $aRow mysql_fetch_assoc($rResult))
    {
        
    $aArray[] = $aRow;
    }
    print_r($aArray);
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  7. #7
    SitePoint Enthusiast
    Join Date
    Jul 2008
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you so much!!!! I really appreciate it!

  8. #8
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    No problem, glad we finally got to the bottom of it for you.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  9. #9
    SitePoint Enthusiast
    Join Date
    Jul 2008
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay, one more question. I am not sure if you're able to do this but: Is there a way I can have a WHERE statement in mysql query where I can compare a table field to the array I just created?

  10. #10
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Do you have an example?
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  11. #11
    SitePoint Enthusiast
    Join Date
    Jul 2008
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Something like:

    PHP Code:
    mysql_query("UPDATE users SET upline='{$mainuser['username']}', uplinetype='2',
    uplineremains='
    {$data['uplineremains']}' WHERE username!='{$data['username']}' AND
    username!='
    {$mainuser['username']}' AND upline='' AND 
    totalclicks>"
    .round($set['refs_minclicks'])." AND lastaction>{$tsp} AND 
    username!=
    $aArray LIMIT 1;"); 
    Where $aArray is what I(well, you ) just created.

  12. #12
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    So you only want to select 1 username from the array that was created? If so, how would you determine which one?

    Or do you want to exclude all users that were found in the previous query, which are now contained in the array?
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  13. #13
    SitePoint Enthusiast
    Join Date
    Jul 2008
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I want to select a single user that is NOT from the array that I just created.

  14. #14
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    This query should do what you ask without using the array we created.
    PHP Code:
    <?php
    $sSQL 
    sprintf("UPDATE users SET upline = '%s', uplinetype = 2, uplineremains = '%s' WHERE username != '%s' AND username != '%s' AND upline = '' AND totalclicks > %s AND lastaction > %s AND username NOT IN (SELECT username FROM users WHERE upline='%s' AND uplinetype = 2) LIMIT 1;",
        
    $mainuser['username'],
        
    $data['uplineremains'],
        
    $data['username'],
        
    $mainuser['username'],
        
    round($set['refs_minclicks']),
        
    $tsp,
        
    mysql_real_escape_string($ir['username'])
    );
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  15. #15
    SitePoint Enthusiast
    Join Date
    Jul 2008
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well the thing that I am doing is placing new users in a users "upline" field and taking the old ones out. Basically recycling referrals. I don't want the users recycled to be the same users that were in the downline before. This query is executed 3 times using jquery and I want to make sure that they are not the same referrals. Also, when I used your code...it didn't work.

  16. #16
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    It seems like you maybe best of heading on over to the SQL forums, I'm a little lacking in this area and I'm sure any query I put together wouldn't be the best or most suitable.

    Good luck, and I'm sure the guys over there will sort you out.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •