PDA

View Full Version : retrieving data from a table



carp
Sat 12th Aug '00, 9:10pm
I need help

lets say that I have a table set up with three things in it:
title
graphic
link


the title contains the text for the title above a page. the graphic contains the url to a graphic (graphics/logo.gif), and the link contains the link color (#000080).

how would i retrieve this and put it in html?

i tried this:


$query = "select * from templates";
$result = mysql_db_query ($database, $query);

$numOfRows = mysql_num_rows ($result);
for ($i = 0; $i < $numOfRows; $i++){
$title = mysql_result ($result, $i, "title");
$link = mysql_result ($result, $i, "link");
$graphic = mysql_result ($result, $i, "graphic");

function showheader($titlemessage) {
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
<HEAD>
<TITLE> <?php echo $title ?> - <?php echo $titlemessage ?> </TITLE>
</HEAD>

<BODY BGCOLOR="#FFFFFF" LINK="<?php echo $link ?>" VLINK="<?php echo $link ?>">
<img src="<?php echo $graphic ?>"><br>


what am i doing wrong???

[Edited by carp on 08-12-2000 at 08:11 PM]

Mike Sullivan
Sat 12th Aug '00, 11:02pm
Untested, but I'm pretty sure this should work.

FYI, in your original code, you had some big mistakes:
1. not closing the "for" block
2. not closing the function
3. not "globaling" the variables required


<?php

$mysql = mysql_connect("localhost", "root", ""); //server, user, pass
$db = mysql_select_db($database, $mysql);

$query = "select title,graphic,link from templates"; //Note, you need some way to limit this to one result set per query
$result = mysql_fetch_array(mysql_query($database, $query));

function showheader($titlemessage) {
global $result;
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
<HEAD>
<TITLE> <?php echo $result[title] ?> - <?php echo $titlemessage ?> </TITLE>
</HEAD>

<BODY BGCOLOR="#FFFFFF" LINK="<?php echo $result[link] ?>" VLINK="<?php echo $result[link] ?>">
<img src="<?php echo $result[graphic] ?>"><br>

<?php
} #end func

showheader("The title message here");

?>

carp
Sat 12th Aug '00, 11:42pm
i know i didn't finish them in there, but i did in my script.

I'll try yours and see what happens!
THANKS!