SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    Level 8 Chinese guy Archbob's Avatar
    Join Date
    Sep 2001
    Location
    Somewhere in this vast universe
    Posts
    3,741
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Whats wrong with this code?

    PHP Code:
    <?php //iframe for chat module
    include "connect.php";
    include 
    "admin/var.php";
    while(!
    connection_aborted())
    {
    $getmessage="Select * from ch_online";
    $getmessage2=mysql_query($getmessage) or die("Could not get messages");
      while(
    $getmessage3=mysql_fetch_array($getmessage2))
      {
          print 
    "$getmessage3[sessionname]<br>";
      }
     
      
      
    flush();
      
    sleep(2); 
     

    }

    ?>
    I even put something in ch_online but nothing shows up when I go to the page, I waited for a long time and still nothing. Whats wrong?

    Help!!

    Thanks in advance.

  2. #2
    ********* Genius Mike's Avatar
    Join Date
    Apr 2001
    Location
    Canada
    Posts
    5,458
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    sessionname should be in quotes, no?
    Mike
    It's not who I am underneath, but what I do that defines me.

  3. #3
    SitePoint Enthusiast rev909's Avatar
    Join Date
    Mar 2002
    Location
    Romania
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    To be able to use $getmessage3['sessionname'] you have to use mysql_fetch_array($getmessage2, MYSQL_ASSOC) or use mysql_fetch_assoc($getmessage2).

  4. #4
    ********* Genius Mike's Avatar
    Join Date
    Apr 2001
    Location
    Canada
    Posts
    5,458
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    mysql_fetch_array() should automatically cast the columns into a textual array as well as a numerical array.
    Mike
    It's not who I am underneath, but what I do that defines me.

  5. #5
    SitePoint Enthusiast rev909's Avatar
    Join Date
    Mar 2002
    Location
    Romania
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Whooops. Sorry. I see now that MYSQL_BOTH is default.

  6. #6
    Level 8 Chinese guy Archbob's Avatar
    Join Date
    Sep 2001
    Location
    Somewhere in this vast universe
    Posts
    3,741
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Putting sessioname in quotes didn't really help, still nothing shows up.

  7. #7
    Employed Again Viflux's Avatar
    Join Date
    May 2003
    Location
    London, On.
    Posts
    1,127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Run your query at the command line?


    I like to do that to make sure my query is actually returning something, before spending hours trying to figure out a problem in displaying it.

  8. #8
    SitePoint Addict Viral's Avatar
    Join Date
    Nov 2001
    Location
    Washington DC
    Posts
    294
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Also, try making it print out a static character as well as the variable to see if your loop is getting executed at all:

    print "test - $getmessage3['sessionname']<br>";

    btw, why the flush() and sleep() functions?
    --Viral
    A computer without Windows is like a chocolate cake without mustard.

  9. #9
    SitePoint Enthusiast rev909's Avatar
    Join Date
    Mar 2002
    Location
    Romania
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?
        $getmessage3 
    = Array('sessionname'=>'Test');
        print 
    "$getmessage3[sessionname]<br>";
    ?>
    works. Prints Test. But:
    PHP Code:
    <?
        $getmessage3 
    = Array('sessionname'=>'Test');
        print 
    "$getmessage3['sessionname']<br>";
    ?>
    gives an error: Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in d:\pages\test\test.php on line 3.
    If you want to use it with the ' then use it like this:
    PHP Code:
    <?
        $getmessage3 
    = Array('sessionname'=>'Test');
        print 
    $getmessage3['sessionname'].'<br>';
    ?>
    I know I was rather offtopic.

  10. #10
    SitePoint Addict Viral's Avatar
    Join Date
    Nov 2001
    Location
    Washington DC
    Posts
    294
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think there's an issue with printing an array value like that. Try encapsulating the entire array in curly brackets...

    print "{$getmessage3[sessionname]}<br>";

    That should work. (I remember reading this, but can't remember where)
    --Viral
    A computer without Windows is like a chocolate cake without mustard.

  11. #11
    Non-Member Icheb's Avatar
    Join Date
    Mar 2003
    Location
    Germany
    Posts
    1,474
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You shouldn't use an array in quotes at all.
    As rev909 already said, you should use
    $getmessage3['sessionname']
    instead of
    "$getmessage3[sessionname]" .
    Both works, but the latter is improper code.

    And you don't need that connection_aborted() stuff. If the client goes to a different page or simply closes the browser, the script's execution will be stopped. You would have to force PHP to continue by using ignore_user_abort().

  12. #12
    Level 8 Chinese guy Archbob's Avatar
    Join Date
    Sep 2001
    Location
    Somewhere in this vast universe
    Posts
    3,741
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks guys, I'll try those suggestions

  13. #13
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Argh, here we go again:
    PHP Code:
    //Correct:
    echo "blah blah $var[a]. blah blah";
    echo 
    "blah blah {$var['a']}. blah blah";
    echo 
    'blah blah' $var['a'] . '. blah blah';

    //Incorrect:
    echo 'blah blah $var[a]. blah blah'//Single Quotes 
    Array keys should NOT be quoted within double-quoted strings, as PHP does not search for constants within strings anyways, unless however, you encapsulate the variable with curly braces, in which case PHP does look for constants, and you once again need to quote the array key.

    Also: try using both ob_flush() and flush() together.
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.


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
  •