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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Dec 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post new to using php and need help

    hi i am new to using php and i'm wanting it so it will display a row of data i.e tittle, picture,description and then afterwards it will then display the next row of data could use please help me
    this is the code i have so far for it
    PHP Code:
    $query = 'SELECT title from image';
    $result = mysql_query($query);

    while($row = mysql_fetch_assoc($result)){
        foreach($row as $key => $value){
            echo $key.': '.$value.'<br>';
        }
        echo '<br>';
    }
    ?>
    <?php
    require 'mysql.php';
    if ( !(
    $link=mysql_connect($host$user$passwd)) ) {
       die(
    '<p>Error connecting to database</p></body></html>');
    } else if ( !(
    mysql_select_db("mdb_sn027")) ) {
       die(
    '<p>Error selecting database</p></body></html>');
    } else {
       
    $query "SELECT id,name,alt FROM image";
       if ( !(
    $result mysql_query($query,$link)) ) {
          die(
    '<p>Error reading database</p></body></html>');
       } else {
          for ( 
    $i $i mysql_num_rows($result) ; $i++ ) {
            
    $row mysql_fetch_assoc($result);
            echo 
    '<img src="getimage.php?id=' $row['id'] . '" alt="' $row['alt'] . '" title="' $row['name']  .'"/>  ' "\n";
          }
       }
    }
    ?>
    </p>
    <?php
    include_once('mysql.php'); 

    $query 'SELECT description from image';
    $result mysql_query($query);

    while(
    $row mysql_fetch_assoc($result)){
        foreach(
    $row as $key => $value){
            echo 
    $key.': '.$value.'<br>';
        }
        echo 
    '<br>';
    }
    ?>

  • #2
    Junsee
    Guest
    This will display all the rows depending on the query given to mysql

    PHP Code:
        include_once('mysql.php');  

        
    $sql "SELECT * FROM `table`; ";
        
    $rs mysql_query($sql$conn) or die ("error with sql query ".$sql);

        while (
    $row mysql_fetch_array ($rs)){
            
    $id $row['id'];
            
    $alt $row['alt'];
            
    $name $row['name'];
            
            echo 
    '<img src="getimage.php?id=' $id '" alt="' $alt '" title="' $name  .'"/>  <br />';
            
        } 
    in this case (SELECT * FROM `table`; ) I said select all (*) from table.
    the while loop will just pirnt out all rows until it finds no more.

    I wouldn't use ! because if it returns 0 rows that becomes an error.

    You don't need to delcare mysql.php twice (I am assuming it contains the connection data, though you should print it here so we can see it)

    I am really guessing at what you want, perhaps if you give us some test data and the table name and rows, I could give you a 100% picture.

  • #3
    New to the CF scene
    Join Date
    Dec 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post

    what i want it to do is for example it displays my the mysql datbase one row of data which would be title, image, description and then after it then displays the next row of data

    example
    title: dog
    image:
    description: dog is fun

    title: cat
    image:
    description: cat is cute

  • #4
    Regular Coder Redcoder's Avatar
    Join Date
    May 2012
    Location
    /dev/null
    Posts
    331
    Thanks
    2
    Thanked 47 Times in 46 Posts
    You just echo any data in the array wherever you want as Junsee pointed out.

    PHP Code:
    include_once('mysql.php');  

        
    $sql "SELECT * FROM image; ";

    while (
    $row mysql_fetch_array ($rs)){
            
    $id $row['id'];
            
    $alt $row['alt'];
            
    $name $row['name'];
            
    $title $row['title'];
            
    $description $row['description'];

          echo 
    "Name : ".$name;
        
          echo 
    "title : ".$title;
               
          echo 
    'Image: \r\n<img src="getimage.php?id=' $id '" alt="' $alt '" title="' $name  .'"/>  <br />';
        
    echo 
    "Description : ".$description;
            
           
            
        } 
    Last edited by Redcoder; 12-04-2012 at 04:14 PM.


  •  

    Posting Permissions

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