PDA

View Full Version : cant display images with mysql


surreal5335
12-17-2009, 06:34 PM
<?
while($website_row = mysql_fetch_array($website_result)) {

?>

<html>
<img src="images/digg.jpg" alt="<?php echo $website_row['url'];?>">
<a href="http://<?php echo $website_row['url'];?>">Digg.com</a><br/>
<a href="mailto: <?php echo $website_row['email'];?>">email support</a><br/>
<?php echo $website_row['description'];

}
?>



I am trying to create a loop to gather the different id's and print out all associated fields into a html interface. I am very comfortable with html but I dont seem to be getting this to work... is it the loop itself that is not working?

I appreciate the help

Old Pedant
12-17-2009, 07:04 PM
Sorry, but I don't see how PHP or MySQL are *at all* involved in the display of the image there.

You do
<img src="images/digg.jpg" >

so that's the one and only image you will ever see.

But you *WILL* be producing illegal HTML, you know.

If there is only one record in $website_result, then your HTML will be:
<html>
<img ...>
<a ...>Digg.com</a>
<a ...>email support</a>
text

which is already illegal. (No <body> tag, at a minimum.)

And if you have multiple records then you will get
<html>
<img ...>
<a ...>Digg.com</a>
<a ...>email support</a>
text
<html>
<img ...>
<a ...>Digg.com</a>
<a ...>email support</a>
text
<html>
<img ...>
<a ...>Digg.com</a>
<a ...>email support</a>
text
...

Multiple <html> tags, still with no <body>, etc.

surreal5335
12-17-2009, 08:42 PM
Thanks for the help, I realized I had not set up the page properly yet, so to avoid quotation issues I changed the file over to an html file.

The updated code is below.


Although html still doesnt seem to recognize the special php characters and just reading them text only. I think it might be the quotes involved, but then wouldnt that cause a problem with html wondering what the href includes in it w/out quotes surrounding it?



<html>
<head>
</head>
<body>


<?php

$connection = @mysql_connect ('localhost', 'surreal5335', 'password');


if(!$connection) {
exit(mysql_error());
}


if (!mysql_select_db('villicus_classifieds')) {
exit(mysql_error());
}

$website_query = "select id, url, description, image, email from websites";


$website_result = mysql_query($website_query);



$category_query = "select id, name from categories";

/*$category_result = mysql_query($category_query) or die(mysql_error());
while($category_row = mysql_fetch_array($category_result)) {

}*/

while($website_row = mysql_fetch_array($website_result)) {

?>


<img src="images/<?php echo $website_row['image'];?>" alt="<?php echo $website_row['url'];?>">
<a href="http://<?php echo $website_row['url'];?>"><?php echo $website_row['url'];?></a><br/>
<a href="mailto: <?php echo $website_row['email'];?>"><?php echo $website_row['email'];?></a><br/>
<?php echo $website_row['description'];
}
?>
</body>
</html>



Thanks for the help

Old Pedant
12-17-2009, 09:10 PM
Ummm...you *can't* "change it over to an html file". If it is not named ".php" then the server will not process the PHP code.

Do we have an impedance mismatch here? Am I misunderstanding you completely??

What kind of server are you running??

surreal5335
12-17-2009, 09:25 PM
Ok that makes sense,

well if I need php to process html, then wouldnt I have to echo the html using single or double quotes? The problem with that is I would need to escape the single or double quotes inside the html. Or is there an easier way?

I am using a wampserver and going through localhost for now.

tomws
12-17-2009, 09:50 PM
well if I need php to process html, then wouldnt I have to echo the html using single or double quotes? The problem with that is I would need to escape the single or double quotes inside the html. Or is there an easier way?


Escaping quotes isn't a "problem", but if you want to avoid it, you have two options:
1) drop in/out of PHP
<html>
<body>
<?php
$pi = 3.14159;
$id = "div1";
$src = "images/favicon.jpg";
?>
<div id="<?php echo $id; ?>"><img src="<?php echo $src; ?>" /></div>
<?php
$somethingElse = date();
?>
</body>
</html>

2) use HEREDOC (http://php.net/heredoc) syntax
<?php
$pi = 3.14159;
$id = "div1";
$src = "images/favicon.jpg";
$somethingElse = date();

$wholePage = <<<SOMENAME
<html>
<body>
<div id="$id"><img src="$src" /></div>
</body>
</html>
SOMENAME;

echo $wholePage;
?>

Old Pedant
12-17-2009, 09:55 PM
Your code there look correct to me.

The only change I would make--and it's for clarity, not for necessity, I think--is shown in red.


while($website_row = mysql_fetch_array($website_result)) {
?>
<img src="images/<?php echo $website_row['image'];?>" alt="<?php echo $website_row['url'];?>">
<a href="http://<?php echo $website_row['url'];?>"><?php echo $website_row['url'];?></a><br/>
<a href="mailto: <?php echo $website_row['email'];?>"><?php echo $website_row['email'];?></a><br/>
<?php echo $website_row['description'];?>

<?php
}
?>

Old Pedant
12-17-2009, 09:56 PM
What are you seeing in the browser when you do it that looks wrong?

Do a VIEW==>>SOURCE (or PAGE SOURCE) and show us what it shows you.

surreal5335
12-17-2009, 10:27 PM
well when I view page source all I get in return is:

</body>
</html>

thats it. why is it missing all the other html and php?

Old Pedant
12-18-2009, 12:36 AM
You don't even get the
<html>
<head>
</head>
<body>
that you show in post #3????

Assuming that you do, then obviously your PHP code is finding no records.

Probably means you have an error in your connection code (which you didn't show us, by the way) or maybe your query is returning no records or maybe your query is giving you an error.

At a minimum, you should check the status of $website_result after this line:

$website_result = mysql_query($website_query);

surreal5335
12-18-2009, 05:17 AM
I actually already have been coding in just php and mysql with this file and getting correct responses from the databse. So it does connect, find the databse, tables, and fields in them.

I spent a little time experimenting and came to some interesting findings:

html is showing up but the php inside the html quotes is not, not even considered text, just blank.

Here is my current setup:



<html>
<body>


<?php


$connection = @mysql_connect ('localhost', 'surreal5335', 'password');


if(!$connection) {
exit(mysql_error());
}


if (!mysql_select_db('villicus_classifieds')) {
exit(mysql_error());
}

$website_query = "select id, url, description, image, email from websites";

$website_result = mysql_query($website_query);


while($website_row = mysql_fetch_array($website_result)) {
print($website_row['url'].'<br/>');
}

$category_query = "select id, name from categories";

$category_result = mysql_query($category_query) or die(mysql_error());
while($category_row = mysql_fetch_array($category_result)) {
print($category_row['name'].'<br/>');
}



?>



<img src="/images/<?php echo $website_row['image'];?>" alt="<?php echo $website_row['url'];?>">
<a href="http://<?php echo $website_row['url'];?>">Digg.com</a><br/>
<a href="mailto: <?php echo $website_row['email'];?>">email support</a><br/>
<?php echo $website_row['description'];?>


</body>
</html>



here is the page source:


<html>
<body>


digg.com<br/>reddit.com<br/>social news<br/>


<img src="/images/" alt="">
<a href="http://">Digg.com</a><br/>
<a href="mailto: ">email support</a><br/>


</body>
</html>



I appreciate any help in the matter.

Old Pedant
12-18-2009, 06:20 AM
<shrug>Says to me that you have an error in your PHP code and so you are getting no records with actual data.

I ask again: Have you checked how many records are being returned? I know that PHP has an easy function to make that check. Have you verified that the $category_result is non-null??

What debugging have you done?

surreal5335
12-18-2009, 06:39 AM
Not much I would imagine, just the mysql_error() function for connection and acessing issues so far.

I tried to find the function you were refering to but all I could find was the general:

error_reporting(E_ALL);

which gave me nothing in return.

Would you happen to know of the function you speak of?

Thanks for the help

tomws
12-18-2009, 02:25 PM
If I'm not mistaken, you're getting output. See this line in the page source you posted above:
digg.com<br/>reddit.com<br/>social news<br/>
I'm betting the first two are from the first query, while the last is from the second query. You're dumping output in the wrong place. That's what the print lines are doing.

surreal5335
12-18-2009, 05:02 PM
I understand that what print does, thats how the code started, to check everything was copacetic. I even commented out those lines and just had the html with the php inserts to see my output. Which the source now tells me php isnt being registered inside of html, or its not accessing the database properly anymore.

I guess I could just echo all my html that should make reading the php inside it easier to implement... But I would prefer to get it to work with this method of jumping in and out of php, I find it cleaner and less error prone.

But apparently I am wrong on the last point :P

Thanks for the help

tomws
12-18-2009, 06:24 PM
The PHP is being "registered", but by that time the values are null, which is exactly what's being printed to screen. Your loops above follow the typical pattern:
while ($row = mysql_fetch_array($result)){}
I'm sure you know what condition causes those loops to exit. That's right: $row is false or null or an empty string. You also know that it's getting past those loops because it prints out the HTML past that point. So, then, look at what you're attempting to print: nothing. Look at what is actually being printed: nothing.

In short, your code is working correctly and doing exactly what you've asked. What you have is a logic problem - you expect one thing, but you've coded something else.

Old Pedant
12-18-2009, 07:20 PM
So you changed the code a *LOT* between post #3 and post #11, and I apologize that I didn't see that #11 was meant to just be a "debug" page, to demo that you are getting records from the db.

So if #11 works, doing
$website_query = "select id, url, description, image, email from websites";
$website_result = mysql_query($website_query);
while($website_row = mysql_fetch_array($website_result)) {
print($website_row['url'].'<br/>');
}

then I do *NOT* see why using my code from post #7 wouldn't then also work:
$website_query = "select id, url, description, image, email from websites";
$website_result = mysql_query($website_query);
while($website_row = mysql_fetch_array($website_result)) {
?>
<img src="images/<?php echo $website_row['image'];?>" alt="<?php echo $website_row['url'];?>">
<a href="http://<?php echo $website_row['url'];?>"><?php echo $website_row['url'];?></a><br/>
<a href="mailto: <?php echo $website_row['email'];?>"><?php echo $website_row['email'];?></a><br/>
<?php echo $website_row['description'];?>
<?php
}
?>

I think maybe Tom missed the big change between #3 and #11, too.

tomws
12-18-2009, 07:23 PM
I think maybe Tom missed the big change between #3 and #11, too.

I noticed that the output is outside of all of the logic and the loops. Is there something else?

Old Pedant
12-18-2009, 08:32 PM
In his post #11 the logic is out of the loop, just as you said.

But in his original code (post #3) it's where it belongs.

What I meant to say is that I *assumed* that #11 was strictly for debug purposes. But maybe not. Maybe he really didn't see the difference.