Results 1 to 5 of 5

Hybrid View

  1. #1

    how can i display news on outside page?

    Hi guys,

    i am currently write a few lines code to display all news from mysql by date.
    db is abc, and there is table called news.

    here is the code:

    PHP Code:
    <?php 
                    $username 
    "xxx"
                    
    $password "xxx"
                    
    $hostname "localhost";     
                    
    $db mysql_connect($hostname$username$password
                    or die(
    "Unable to connect to MySQL"); 
                    
    $selected mysql_select_db("abc",$db
                    or die(
    "Could not select abc"); 
                     
    $result mysql_query('SELECT * FROM news' 'ORDER BY date DESC'); 

                    
    $news = array(); 
                    while (
    $result $db->row_array()) { 
                    
    $news[] = $result
                    } 
                     
    ?>
    but this code seems doesn't work at all.

    i need help to show news on outside pages like anywhere in html or php pages.

    Thanks guys!!!

    sean

  2. #2
    I don't understand how $db->row_array() is supposed to work.

    But what example is the problem, "doesn't work" is a very vague description of it.

    If the result is derived directly from mysql_query() then why not use the standard mysql_fetch_[array|assoc|row]() functions.

  3. #3
    Originally posted by kuja
    I don't understand how $db->row_array() is supposed to work.

    But what example is the problem, "doesn't work" is a very vague description of it.

    If the result is derived directly from mysql_query() then why not use the standard mysql_fetch_[array|assoc|row]() functions.
    Hi Kuja,


    it shows this errors:

    Fatal error: Call to a member function on a non-object in /home/xxx/public_html/index.php on line 78


    so would you please tell me some detail info since i am beginner.


    Thanks

    sean

  4. #4
    Your problem is with the line: $db = mysql_connect(...)

    mysql_connect() does not return an object. It returns a resource that identifies a specific connection to the SQL server.

    All you have to do is change '$result = $db->row_array()' to '$row = mysql_fetch_assoc($result)'

    Within the loop, you may access fields through $row['fieldName']

    Example:

    PHP Code:
    <?php
      
    // other stuff here
      
    $db      mysql_connect(...);
                     
    mysql_select_db(..., $db);

      
    $result mysql_query(..., $db);

      while (
    $row mysql_fetch_assoc($result$db)) {
        echo 
    "{$row['my_field_name_here']}<br>\n"// output field data.
      
    }
    ?>

  5. #5
    Thank you so much. kuja


    sean

Posting Permissions

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