SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Member
    Join Date
    Jun 2013
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Mysqli shows word "array" when echo

    Hi there again. When the echo's in this php post is just says the word "Array", and not the username of the person who has the points.
    What might be missing from here? Or coded wrong.

    Thanks
    Chad
    PHP Code:
    <?php 

            $earnings 
    mysqli_query($con"SELECT * FROM earnings ORDER BY date DESC LIMIT 5");

            
    $number mysqli_num_rows($earnings);

            

    $i 1;



            while(
    $row mysqli_fetch_array($earnings))

      {

          if(
    $i <= $number)

       {

        echo 
    " <div class=\"row\">

              <ul>

                <li class=\"name\">"
    ;

                
    $tempId $row['userId'];

                
    $tempScore $row['score'];

                
    $tempTitle $row['title'];

                
    $userName mysqli_query($con"SELECT userName FROM users WHERE userId = '$tempId'");

                
    $uN mysqli_fetch_assoc($userName);

                echo 
    "$uN";

                echo 
    "</li>

                <li class=\"point\"><img src=\"images/point.png\" width=\"16\" height=\"16\" /></li>

                <li class=\"reward\">
    $tempScore</li>

              </ul>

              <div class=\"clear\"></div><font style='font-size:10px; margin-top:-4px;'>
    $tempTitle</font>

            </div>"
    ;

       } else {

           echo 
    " <div class=\"row last\">

              <ul>

                <li class=\"name\">"
    ;

                
    $tempId $row['userId'];

                
    $tempScore $row['score'];

                
    $userName mysqli_query($con"SELECT userName FROM users WHERE userId = '$tempId'");
                
                
    $uN mysqli_fetch_assoc($userName);

                echo 
    "$uN";

                echo 
    "</li>

                <li class=\"point\"><img src=\"images/point.png\" width=\"16\" height=\"16\" /></li>

                <li class=\"reward\">
    $tempScore</li>

              </ul>

              <div class=\"clear\"></div>

            </div>"
    ;

       }

          

          
    $i++;

      }

            

            
    ?>

  2. #2
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,401
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    Hi Chad,

    The problem is here:
    PHP Code:
    $uN mysqli_fetch_assoc($userName); 
    echo 
    "$uN"
    You're fetching the result of the query as an associative array, so even if the query returns a single row with a single column, you still have to access the value using the column name as an array key, like this:
    PHP Code:
    echo $uN['userName']; 

  3. #3
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    5,014
    Mentioned
    103 Post(s)
    Tagged
    0 Thread(s)
    Are you intending to display a list of the last 5 users earnings? If not what is it you're intending to display? I'm wondering if it may be possible for you to do it in one hit with a join query and just loop through the result set of a join query
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  4. #4
    SitePoint Member
    Join Date
    Jun 2013
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by fretburner View Post
    Hi Chad,

    The problem is here:
    PHP Code:
    $uN mysqli_fetch_assoc($userName); 
    echo 
    "$uN"
    You're fetching the result of the query as an associative array, so even if the query returns a single row with a single column, you still have to access the value using the column name as an array key, like this:
    PHP Code:
    echo $uN['userName']; 
    That did it thanks, still getting used to the mysqli.

    I tried to do what you said on this one as well, but it doesn't show anything at all when echoed.

    PHP Code:
    $history mysqli_query($con"SELECT * FROM earnings WHERE userId = '$userId' ORDER BY date DESC");
              while(
    $row mysqli_fetch_array($history)){
                  
    $tempScore $row['points'];
                  
    $tempTitle $row['rare'];
    echo 
    $tempTitle

  5. #5
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,401
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    Quote Originally Posted by clfarmer21 View Post
    I tried to do what you said on this one as well, but it doesn't show anything at all when echoed.

    PHP Code:
    $history mysqli_query($con"SELECT * FROM earnings WHERE userId = '$userId' ORDER BY date DESC");
              while(
    $row mysqli_fetch_array($history)){
                  
    $tempScore $row['points'];
                  
    $tempTitle $row['rare'];
    echo 
    $tempTitle
    The while loop is missing the closing brace (}). You don't actually need the while loop unless your query is going to return more than one row. You could rewrite
    the code like this:
    PHP Code:
    $history mysqli_query($con"SELECT * FROM earnings WHERE userId = '$userId' ORDER BY date DESC");
    $row mysqli_fetch_array($history);
    $tempScore $row['points'];
    $tempTitle $row['rare']; 

  6. #6
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,401
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    Quote Originally Posted by SpacePhoenix View Post
    Are you intending to display a list of the last 5 users earnings? If not what is it you're intending to display? I'm wondering if it may be possible for you to do it in one hit with a join query and just loop through the result set of a join query
    Good point, the code could be simplified by doing everything with one query:
    Code MySQL:
    SELECT users.username, earnings.* FROM users JOIN earnings ON users.userId = earnings.userId ORDER BY date DESC LIMIT 5

    The PHP could then be rewritten to something like this:
    PHP Code:
    $sql "SELECT users.username, earnings.* FROM users JOIN earnings ON users.userId = earnings.userId ORDER BY date DESC LIMIT 5";
    $user_earnings =  mysqli_query($con$sql); 
    $number mysqli_num_rows($user_earnings); 

    $i 1
    while(
    $row mysqli_fetch_array($user_earnings)) 

        if(
    $i <= $number
        { 
            echo 
    " <div class=\"row\"> 
                     <ul> 
                     <li class=\"name\">"


            echo 
    $userName

            echo 
    "</li> 
                <li class=\"point\"><img src=\"images/point.png\" width=\"16\" height=\"16\" /></li> 
                <li class=\"reward\">
    {$row['score']}</li> 
              </ul> 
              <div class=\"clear\"></div><font style='font-size:10px; margin-top:-4px;'>
    {$row['title']}</font> 
            </div>"

            
        } else {
        
            echo 
    " <div class=\"row last\"> 
              <ul> 
                <li class=\"name\">"
    ;
                
                echo 
    $row['userName']; 
                
                echo 
    "</li> 
                <li class=\"point\"><img src=\"images/point.png\" width=\"16\" height=\"16\" /></li> 
                <li class=\"reward\">
    {$row['score']}</li> 
              </ul> 
              <div class=\"clear\"></div> 
            </div>"

        } 
        
    $i++; 


  7. #7
    SitePoint Member
    Join Date
    Jun 2013
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh there is a bracket, it just cut it off for some reason why I copied, here is the code again:
    PHP Code:
            <?php 
              
              $history 
    mysqli_query($con"SELECT * FROM earnings WHERE userId = '$userId' ORDER BY date DESC");
              while(
    $row mysqli_fetch_array($history)){
                  echo 
    " <div class=\"rowinnerhis\">     
              <ul>
                <li class=\"his1\">
    <p> " 
    $row['rare'] ." </p>
                </li>
                <li class=\"his2\">" 
    $row['date'] ." </li>
                <li class=\"his3\"><img src=\"images/point.png\" width=\"16\" height=\"16\" /><span class=\"value\">" 
    $row['score'] ." </span> </li>
            </ul>
              <div class=\"clear\"></div>
          </div>"
    ;
              }
              
    ?>
         
           <?php } else {
               echo 
    "<h2>No History!</h2><p> You need to be loged in to view history.</p>";
           }
                
    ?>
    Nothing shows up its just blank.
    Its trying to add a list to show all the history earnings.

    Ill try adding everything in one que, thanks.

  8. #8
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,401
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    Quote Originally Posted by clfarmer21 View Post
    Nothing shows up its just blank.
    Its trying to add a list to show all the history earnings.
    Should this query be returning more than one row from the database? If you're not getting any output at this point, it could be that your query is not returning any records.

    Also:
    PHP Code:
    <?php } else {
               echo 
    "<h2>No History!</h2><p> You need to be loged in to view history.</p>";
           }
                
    ?>
    is there an IF statement that you've not shown, or are you trying to add an else clause to a while loop (which isn't valid in PHP)?

  9. #9
    SitePoint Member
    Join Date
    Jun 2013
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by fretburner View Post
    Should this query be returning more than one row from the database? If you're not getting any output at this point, it could be that your query is not returning any records.

    Also:
    PHP Code:
    <?php } else {
               echo 
    "<h2>No History!</h2><p> You need to be loged in to view history.</p>";
           }
                
    ?>
    is there an IF statement that you've not shown, or are you trying to add an else clause to a while loop (which isn't valid in PHP)?
    Sorry not at home atm. But yes there is a
    If isset $session `user`
    There is more then one row of each user that has different points from the rare. Im trying to show the history of everything they have goten so far.

  10. #10
    SitePoint Member
    Join Date
    Jun 2013
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is how it is in my data base:
    This is from earnings table


    There could be hundreds if someone plays more, so I want to show them all.

    I tried using the :
    PHP Code:
    $tempScore $row['points']; 
    but still doesn't show.

    Here is the hole file:
    PHP Code:
    <?php include("function.php"); ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

    <title>History &bull; <?php echo $siteName?></title>
    <?php include("headscript.php"); ?>
    <?php 
    include("logscr.php"); ?>
        
        
        
    </head>
    <body>
    <div id="wrapper">
      <div id="header">
        <div class="header_top">
          <div class="top">
          </div>
          <div class="clear"></div>
        </div>
        <div class="header_bottom">
        <?php include("buttons.php"); ?>
          <?php include("menu.php"); ?>
        </div>
      </div>
      <div id="body">
        <div class="box">
        <?php 
        
    if(isset($_SESSION["user"])){
            
    $userId $_SESSION["user"];
                
    ?>
           <h2>Your Stats</h2>
           <?php 
           
    //$myPoints = mysqli_query($con, "SELECT points FROM balances WHERE userId = '$userId'");
           
    $myOffers mysqli_query($con"SELECT offers FROM balances WHERE userId = '$userId'");
           
    $myRefs mysqli_query($con"SELECT refered FROM users WHERE userId = '$userId'");
           
    $mySpent mysqli_query($con"SELECT spent FROM balances WHERE userId = '$userId'");
           
    $myPoints mysqli_query($con"SELECT * FROM balances WHERE userId = '$userId'");
           
    $Mpoints mysqli_fetch_assoc($myPoints);
           
    $mOffers mysqli_fetch_assoc($myOffers);
           
    $mRef mysqli_fetch_assoc($myRefs);
           
    $mSpent mysqli_fetch_assoc($mySpent);
           
           
    ?>
           <div style="padding:10px; border:1px solid #557453; background:url(../images/boxbg.png);">
           <table align="center" width="800px" cellspacing="5px"><tr><td><h4 style="display:inline;">Your Points:</h4> <?php echo $Mpoints  ?> <img src="images/point.png" /></td><td><h4 style="display:inline;">Points Spent:</h4> <?php echo $MS?> <img src="images/point.png" /></td><td><h4 style="display:inline;">Complete: </h4> <?php echo $MO?></td><td><h4 style="display:inline;">Referals: </h4> <?php echo $MR?></td></tr></table>
          </div>
          <h2>View History</h2>
          <div class="rowinnerhistop">
              <ul>
                <li class="his1">
              
    Title
                </li>
                <li class="his2">Date/Time</li>
                <li class="his3">Value</li>
            </ul>
              <div class="clear"></div>
            </div>
            <?php 
              
              $history 
    mysqli_query($con"SELECT * FROM earnings WHERE userId = '$userId' ORDER BY date DESC");
              while(
    $row mysqli_fetch_array($history)){
                  
    $tempScore $row['points'];
                  echo 
    " <div class=\"rowinnerhis\">     
              <ul>
                <li class=\"his1\">
    <p> " 
    $row['offerTitle'] ." </p>
                </li>
                <li class=\"his2\">" 
    $row['date'] ." </li>
                <li class=\"his3\"><img src=\"images/point.png\" width=\"16\" height=\"16\" /><span class=\"value\">" 
    $tempScore ." </span> </li>
            </ul>
              <div class=\"clear\"></div>
          </div>"
    ;
              }
              
    ?>
         
           <?php } else {
               echo 
    "<h2>No History!</h2><p> You need to be loged in to view history.</p>";
           }
                
    ?> 
          
          
          
         
        </div>
        <div class="clear"></div>
      </div>
      <div id="footer">
                <ul>
            <?php include("footlinks.php"); ?>
           
        </ul>
      </div>
    </div>
    <?php include("footer.php"); ?>
    </body>
    </html>
    Nothing is being called in this file.
    Thanks
    Chad

  11. #11
    SitePoint Guru bronze trophy
    Join Date
    Feb 2013
    Posts
    725
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Can we assume you have session_start(); in your function.php file?
    I would echo $history after it's defined to make sure your $userId is set.


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
  •