SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot
    Join Date
    Jul 2003
    Location
    London
    Posts
    189
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    i can connect to data but cant print it!

    hiya guys and girls

    ive managed to connect to my db but i cant figure out the code to actually print data from it into browser.

    can someone please look at the code and tell me what i'm missing? i would like to print the contents of the cell newsId = 00001 in the headline column.

    any idea?

    Thanks guys


    $host = "localhost";
    $user = "";
    $password = "";
    $database = "test";

    $connection = mysql_connect($host, $user, $password);
    $db = mysql_select_db($database, $connection);


    $query = "select HEADLINE from news where newsId='00001'";
    $result = mysql_query($query) or die ("Couldn't execute query.");

  2. #2
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  3. #3
    SitePoint Zealot
    Join Date
    Jul 2003
    Location
    London
    Posts
    189
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks!

    i got some stuff to print out but while loops dont work. any idea what im doign wrong here?

    $query = "select headline from news";
    $result = mysql_query($query) or die ("Couldn't execute query.");

    while ($row = mysql_fetch_array($result));
    {
    echo '<p>' . $row['headline'] . '</p>';
    }

    thanks people, im almost getting somewhere feels good!!

    J

  4. #4
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You don't want the semi colen after the while loop.

    PHP Code:
      while ($row mysql_fetch_array($result))
      {
      echo 
    '<p>' $row['headline'] . '</p>';
      } 
    Also, could you please wrap code in [php] tags? It just makes things easier to read. (Oh, and this post should really be in the PHP forum not MySQL, but that's not a huge issue, a mod will move it.)

  5. #5
    SitePoint Zealot
    Join Date
    Jul 2003
    Location
    London
    Posts
    189
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks a lot Nathan! That fixed it sorry i neve wrote back

    J

  6. #6
    SitePoint Addict
    Join Date
    Feb 2005
    Location
    Maine
    Posts
    244
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can extract the row first, too.

    PHP Code:
    while ($row mysql_fetch_array($result))
       {
      
    extract($row);
      echo 
    '<p>$headline</p>';
      } 
    It makes it fairly esaier. You don't have to. Of course, it probably would start interfering in big applications, so it may not be a good idea.



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
  •