...

View Full Version : Noob question need help!!



JamesNebeker
09-12-2011, 10:46 PM
Hi guys. I'm new to PHP and MySQL and I am trying to put together a simple script that calls image urls from the database and places them as the img src in an html tag for viewing. So far, I have run into a few problems. First, when I run the query without a limitation of 1, the query sets ALL of my results for the entire table to the variable result_row['style]. Style is only one column in the table that contains the URL for one image, and there are multiple rows of 'style' that contain one url listing each, but the query returns everything into a single array element which is useless. Also, when I place the image into a html image tag, it won't display the image, even though the URL is perfectly valid and if checked in my browser will display the picture.

Anyway, here is the code I hope you can tell me what is wrong


$result = mysql_query("SELECT style FROM GiftWrap WHERE GiftWrap = 1 ");
if (!$result)
{
die ("Impossible command". mysql_error());
}

while ($result_row = mysql_fetch_array(($result)))
{

echo $result_row['style'];

}



?>
<html>
<img src="<? echo $result_row['style'];?>" align = 'middle">


</html>
<?




mysql_close($connection);
?>

myfayt
09-12-2011, 10:54 PM
$result = mysql_query("SELECT style FROM GiftWrap WHERE GiftWrap='1' LIMIT 1");

$result2 = mysql_fetch_array($result);

echo "<img src=\"".$result2['style']."\">";

Assuming your images are stored inside an images folder.

JamesNebeker
09-12-2011, 11:03 PM
The style table returns the entire link i.e www.site.com/images/image.jpg
Would I still need the extra images?

And how would I fix the issue with all results being thrown into one element?
The way it sits now, if I try to query more than one result, it looks like this

echo $result['style'] << outputs: img1urlimg2urlimg3urlimg4url

Old Pedant
09-12-2011, 11:13 PM
<html>
<head>
<title>pix</title>
</head>
<body>
<?php

// make your mysql connection here

$result = mysql_query("SELECT style FROM GiftWrap WHERE GiftWrap = 1 ");
if (!$result)
{
die ("Impossible command". mysql_error());
}
while ($result_row = mysql_fetch_array(($result)))
{
echo '<img src="' . $result_row['style'] . '" alt="picture" style="whatever" />';
}
// best to close result and connection here, but will work if you don't
?>
</body>
</html>

myfayt
09-12-2011, 11:41 PM
Updated my post, try that. It will result full url, and only display 1.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum