A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: php mysql question

  1. #1
    Senior Member
    Join Date
    Mar 2000
    Posts
    584
    Here's the problem, I have two usernames, both of which are being called and stored in the variable $username, but for some reason, only one is being printed.

    Code:
    <?php
    
    //just get the all the user's names from the username column
    $sql="SELECT username FROM useradmins";
    
    //connect to db, etc.
    include("connect.php");
    
    //while there exists a username in the array, run this code
    while($row=mysql_fetch_array($sql_result))
         {
         //set the variable $username to hold the username value that was in the mysql column
         $username=$row["username"];
      
         //call the script that then takes the username variable and prints it out
         include("featured.php");
    
         print "<br>";
    
         }
    
    ?>
    While there are 2 usernames, only one gets printed. Any thoughts?

  2. #2
    Registered User
    Join Date
    Feb 2001
    Posts
    13,039
    Hi,

    I am sure you hve already tried to just print $username, so the problem might lie in featured.php. May I see it?

    Musicman

  3. #3
    Senior Member
    Join Date
    Mar 2000
    Posts
    584

    okay

    I may have oversimplified things in the previous post. Basically, I'm setting up a script that loads 'featured' editors data. You can see the output here:

    http://www.inflash.com/showplayer/testindex.php

    It is currently only displaying one editor when there are 3, all with publish=1

    [code]
    <?php

    $sql="SELECT username, website, picurl, websitetitle FROM useradmins WHERE publish=1";

    //connect db
    include("connect.php");

    while($row=mysql_fetch_array($sql_result))
    {
    $username=$row["username"];
    $website=$row["website"];
    $picurl=$row["picurl"];
    $websitetitle=$row["websitetitle"];

    include("testfeatured.php");

    print "<br>";

    }

    ?>

    in testfeatured.php is contained:

    Code:
    <?php
    
           print "<table>
                        <tr>
                            <td align=center colspan=4><font face=Verdana size=2><b>$username's Picks</b></a><br>";
    
    if(isset($website))
         {
         print "<font face=Verdana><a href=$website>$websitetitle</a></font>";
         }
    
         print "</td><td colspan=2>";
    
         if(isset($picurl))
         {
         print "<img src=$picurl border=1>";
         }
    
    etc.
    here's the data (all test data) contained in the useradmins table (sorry it's not in alignment):

    Code:
    id_num username email password rand validated ip_address website picurl websitetitle publish 
     10   Nastor   [email protected]   *****     1   24.30.117.121   BoneLand.com   http://members.spree.com/entertainme...astorthumb.jpg   BoneLand.com   1  Edit Delete 
     8   mbannonb   [email protected]   ****      1   24.30.117.121   http://www.inflash.com   http://members.spree.com/entertainme...ow-100-100.jpg   InFlash.com   1  Edit Delete 
     12   tester   [email protected]   1234      1      http://www.inflash.com         1  Edit Delete
    If you need the full scripts, let me know and I'll email them to you. It's got me stumped.

  4. #4
    Registered User
    Join Date
    Feb 2001
    Posts
    13,039
    Hi mbannonb,

    I changed script to fit my system (postgresql rather than mysql) and got three users rather than one.
    One thing looks somewhat different - the xx_fetch_array code. Probably check this one in ph/mysql manual
    Code:
    <?php 
    
    $sql="SELECT username, website, picurl, websitetitle FROM useradmins WHERE publish=1";
    
    $myDB = pg_connect("dbname=notenversand");
    $sql_result = pg_exec($myDB, $sql);
    $nrow = 0;
    while($row=@pg_fetch_array($sql_result, $nrow++))
    {
       $username=$row["username"];
       $website=$row["website"];
       $picurl=$row["picurl"];
       $websitetitle=$row["websitetitle"];         
    
       include("testfeatured.php");
    
       print "<br>";
    }
    
    ?>
    Musicman

  5. #5
    Senior Member
    Join Date
    Mar 2000
    Posts
    584

    okay

    First of all, thanks so much for your help.

    I put the selected code above up on my server (for mysql) and it was working fine as well, but it wasn't working when combined with the whole script.

    So, I figured it was something with the rest of the script. The problem was, I was fixated on the $username variable, thinking that it was the culprit. Why I stuck on that, I don't know.

    But it turns out it was the $sql_result variable that was causing the problem.

    The line: while($row=mysql_fetch_array($sql_result))

    occurs in index.php, and then index.php calls featured.php, which also has

    while($row=mysql_fetch_array($sql_result))

    Thus, '$sql_result' gets looped through until it's empty. When it returns to 'index.php', '$sql_result' is empty, so when it revisits 'while($row=mysql_fetch_array($sql_result))', it's considered empty and the loop is terminated, even though only one username's data has been processed.

    So there it is.

  6. #6
    Registered User
    Join Date
    Feb 2001
    Posts
    13,039
    Hi,

    you are right on that ... but this piece of code did not shine up on your post.
    Anyway, it is interesting to see that some of the more common functions look different for different databases, although it was probably a major concern in php development to make them as similar as possible

    Musicman

Posting Permissions

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




Click Here to Expand Forum to Full Width

HTML5 Development Center