Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-01-2012, 05:38 PM   PM User | #1
dekon
New to the CF scene

 
Join Date: Dec 2012
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
dekon is an unknown quantity at this point
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>';
}
?>
dekon is offline   Reply With Quote
Old 12-01-2012, 07:40 PM   PM User | #2
Junsee
New Coder

 
Join Date: Jun 2012
Posts: 42
Thanks: 4
Thanked 5 Times in 5 Posts
Junsee is an unknown quantity at this point
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.
Junsee is offline   Reply With Quote
Old 12-04-2012, 12:09 PM   PM User | #3
dekon
New to the CF scene

 
Join Date: Dec 2012
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
dekon is an unknown quantity at this point
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
dekon is offline   Reply With Quote
Old 12-04-2012, 04:11 PM   PM User | #4
Redcoder
Regular Coder

 
Redcoder's Avatar
 
Join Date: May 2012
Location: /dev/couch
Posts: 309
Thanks: 2
Thanked 46 Times in 45 Posts
Redcoder has a little shameless behaviour in the past
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;
        
       
        
    } 
__________________
For professional Hosting and Web design.....


NetEssentials.co.uk

Last edited by Redcoder; 12-04-2012 at 04:14 PM..
Redcoder is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:27 PM.


Advertisement
Log in to turn off these ads.