Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Jun 2010
    Posts
    31
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Display from database

    I am wanting to fill parts of a page with values from a mysql database but Im not sure how to go about doing it. I used an example from w3 schools which displays things in a table fine, but I am wanting a different layout Please can somebody point me in the right direction. The layout works fine with
    PHP Code:
    <?php include("news.php"); ?>
    without my attempt to add database values.

    PHP Code:
    <?php
    $username 
    "user";
    $password "password";
    $hostname "localhost"

    //connection to the database
    $dbhandle mysql_connect($hostname$username$password
     or die(
    "Unable to connect to MySQL");
    echo 
    "Connected to MySQL<br>";

    //select a database to work with
    $selected mysql_select_db("username_news",$dbhandle
      or die(
    "Could not select username_news");
      echo 
    "Connected to Database<br>";

    //execute the SQL query and return records
    $result mysql_query("SELECT title, newstext, id, date FROM news");
      echo 
    "queried<br>";

    //fetch tha data from the database 
    while ($row mysql_fetch_array($result)) {
        
       echo 
    "<div id="main_column">"
             
    echo "<div class="main_column_section">"       
                
               
    echo "<h2><span></span>".$row{'title'}. "</h2>"
                    
                  
    echo"  <div class="main_column_section_content">"
                    
                     
    echo" <a href="#"><img class="image_wrapper fl_image" src="images/image_01.jpg" alt="image one" /></a>"
                   
    echo"   <p><br />"
                    
    echo    "<br />"
                       
    echo"".$row{'newstext'}. "<br />"
                  
    echo"".$row{'id'}. "<br />"
                  
    echo"".$row{'date'}. "<br />"


                       
                              
    echo"  </p>"
             
    echo"     </div>"
                 
    echo" <div class="cleaner"></div>"
                   
    echo" <div class="bottom"></div>"
            
    echo"</div>"
    }
    //close the connection
    mysql_close($dbhandle);
    ?>
    Last edited by Craigy; 06-17-2010 at 11:10 PM.

  • #2
    Regular Coder
    Join Date
    Jul 2009
    Posts
    187
    Thanks
    16
    Thanked 8 Times in 8 Posts
    Where you have your while replace that code with the following:
    PHP Code:
    <?php
    echo('<div id="main_column">');
        echo(
    '<div class="main_column_section">');
            echo(
    '<h2><span></span>'.$row['title'].'</h2>');
            echo(
    '<div class="main_column_section_content">');
                echo(
    '<a href="#"><img class="image_wrapper fl_image" src="images/image_01.jpg" alt="image one" /></a>');
                echo(
    '<p><br />');
                echo(
    '<br />');
                echo(
    $row['newstext']. "<br />");
                echo(
    $row['id']. "<br />");
                echo(
    $row['date']. "<br />");
                echo(
    '</p>');
            echo(
    '</div>');
        echo(
    '<div class="cleaner"></div>');
        echo(
    '<div class="bottom"></div>');
    echo(
    '</div>');
    ?>
    You left a lot of unclosed echo's (";") and to save the long method of escaping quotes with backslashes I replaced them with single quotes, take a look at the following example:

    PHP Code:
    <?php echo("<div id="main_column">"); ?>
    Then compare that with:
    PHP Code:
    <?php echo('<h2><span></span>'.$row['title'].'</h2>'); ?>
    If you notice in the second example you close the quote to allow for a php variable to be added into the echo, well this is exactly what you're doing with the first one except php thinks that "main_column" is meant to be php code because the first quote is being closed by the div ID quote.

    and if you wanted to keep the double quotes for personal preference:
    You have the following code:
    PHP Code:
    <?php echo("<div class="bottom"></div>"); ?>
    but php keeps closing the first quote and causing errors, simply escape any double quotes within the SAMe quotes with a backslash before the quote like so:
    PHP Code:
    <?php echo("<div class=\"bottom\"></div>"); ?>
    Now PHP would process this correctly.


    Edit:

    Also take a look at the syntax highlighting for a better example
    Last edited by Jazz914; 06-17-2010 at 02:33 AM.

  • Users who have thanked Jazz914 for this post:

    Craigy (06-17-2010)

  • #3
    New Coder
    Join Date
    Jun 2010
    Posts
    31
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Wow works very well indeed. I found

    echo $var; // Results in the value of $var being printed
    echo '$var'; // Results in the word '$var'
    echo "$var"; // Results in the value of $var being printed

    Which I will keep handy. Thank you for pointing me in the right direction to learn something new.

    The only problem now is it isn't populating the space with the database entry.
    PHP Code:
    echo('<a href="#"><img class="image_wrapper fl_image" src="images/image_01.jpg" alt="image one" /></a>'); 
    Displays the image. Perfectly and is great.

    But
    PHP Code:
    echo('<h2><span></span>'.$row['title'].'</h2>'); 
    Does not call in the title value to populate the top, it is just a blank space.

    I tried
    PHP Code:
    echo('<h2><span></span>' ".$row['title']." '</h2>'); 
    since
    echo "$var"; // Results in the value of $var being printed
    but that just breaks the whole thing again.

  • #4
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    Quote Originally Posted by Craigy View Post
    But
    PHP Code:
    echo('<h2><span></span>'.$row['title'].'</h2>'); 
    Does not call in the title value to populate the top, it is just a blank space.
    If you see the h2 and span tags when viewing the page source, that means there is no value for $row['title'] (or it may be zero, whitespace, or other equivalently empty() text).
    Are you a Help Vampire?

  • #5
    Regular Coder
    Join Date
    Jul 2009
    Posts
    187
    Thanks
    16
    Thanked 8 Times in 8 Posts
    What is the value of title in the table?

  • #6
    New Coder
    Join Date
    Jun 2010
    Posts
    31
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Sorry I'm still struggling

    database name : username_news
    table name : news



    Last edited by Craigy; 06-17-2010 at 10:05 PM.

  • #7
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    Show your current code. I suspect you indiscriminately copied/pasted jazz914's code and it's in the wrong place. Specifically, I'll bet you're missing your mysql_fetch_array line now.
    Are you a Help Vampire?

  • Users who have thanked tomws for this post:

    Craigy (06-17-2010)

  • #8
    New Coder
    Join Date
    Jun 2010
    Posts
    31
    Thanks
    10
    Thanked 0 Times in 0 Posts
    You were right that's what I was missing, after reading

    http://php.net/manual/en/function.mysql-fetch-array.php

    I have added
    PHP Code:
    while ($row mysql_fetch_array($resultMYSQL_ASSOC)) {

    jazz914s code


    and it now works perfectly.

    Thank you for pointing me in the right direction and all the help. I really do appreciate it.


  •  

    Posting Permissions

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