SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Dec 2007
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Fetch the full list from the table of the database

    <?
    $data = "SELECT * FROM vituopen WHERE lighting";
    $data = @mysql_query('SELECT * FROM lighting');
    if (!$data);
    $info = mysql_fetch_array( $data );
    Print "<b>Name/b> ".$info['Cast'] . " ";
    Print "<b>Characters/b> ".$info['Characters'] . " <br>";
    ?>

    This script pulls only the data from the first row in the table of the database and not from all the row in the table. So how do I change this script to fetch data from the the entire table in the database?

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,198
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    When you want to grab all the inserted values you either need to use a while or foreach loop to go through the array. Below i have listed some things which is just friendly feedback.

    1. When using MySQL queries even if a query works its best practice to write an error statement like i have in the example below as it helps for debugging if something changes.
    2. When using PHP reserved names such as "print" and "echo" its best to use the lowercase name as that is what 99% of all IDE's are programmed to see and personally its just easier to read.

    Here is an example of how you could use your code
    PHP Code:
    //$data = "SELECT * FROM vituopen WHERE lighting";

    if (!$data mysql_query('SELECT * FROM lighting')) {
        die(
    'MySQL Error: ' mysql_error());
    }

    while (
    $info mysql_fetch_array($data)) {
        echo 
    '<b>Name:</b> ' $info['Cast'] . ' ';
        echo 
    '<b>Characters:</b> ' $info['Characters'] . '<br /><br />';


  3. #3
    SitePoint Member
    Join Date
    Dec 2007
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank You


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
  •