How to use Php inside Php?
Can someone tell/show me how to use PHP inside PHP. I'm trying to make the URL of an image chang开发者_开发技巧e depending on what value is in a MySQL database. Here's an example of what I'm trying to do. Bear in mind that $idx already has a value from the URL of the page.
<?php
$query = "SELECT * FROM comment WHERE uname='$idx'";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo "<img src='' name='comm' width='75px' height='60px' id='mainimage' />";
}
?>
How would I make the source value, for the image, come from a different table?
You can join data from multiple tables in a single SQL query. See: http://www.w3schools.com/sql/sql_join.asp
Example:
SELECT column_name(s)
FROM table_name1
JOIN table_name2
ON table_name1.column_name=table_name2.column_name
You'd do another SQL query inside the while
loop. I like how you put it, "Php inside Php", that's pretty much what you do.
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$image_query = "SELECT image_url FROM your_table";
$image_result = mysql_query($image_query);
$image = mysql_fetch_assoc($image_result);
echo "<img src='" . $image['image_url'] . "' name='comm' width='75px' height='60px' id='mainimage' />";
}
Make sure the variable names for your query and result are different from your original query, because you're still using the $result
variable from the original query each iteration of the loop. So here I've prefixed them with "image_".
Assuming at a random guess that the other table might be called accounts
, and uname
is its primary key, you can do a JOIN
to grab the data from the both tables in one go. If you can fit what you want to do in a single query that's typically much faster than doing an extra query per row
<?php
$result = mysql_query(
"SELECT comment.*, account.url AS imgurl ".
"FROM comment JOIN account ON comment.uname=account.uname ".
"WHERE comment.uname='".mysql_real_escape_string($idx)."'"
);
?>
<?php while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { ?>
<img src="<?php echo htmlspecialchars($row['imgurl']) ?>" alt="" class="mainimage" />
<?php } ?>
Notes:
you need to escape text you insert into an SQL query, or you've get serious security problems. Use
mysql_real_escape_string
, or let parameterised queries handle it;similarly when putting text into an HTML string,
htmlspecialchars
is needed or you've got other (less serious, but still not good) security issues;you mustn't use the same
id
on an element more than once (which it will be, in a loop). The same goes forname
on<img>
, although there's little reason you'd really ever want to useimg name
;width="75px"
won't work in an attribute aspx
-unit measurements are CSS not HTML. Omit the unit, or, better, set a rule in CSS (.mainimage { width: 75px; height: 60px; }
) to do them all at once.
How would I make the source value, for the image, come from a different table?
You have to simply ask the different table. You can ask by creating subquery (or join) or create second query inside loop.
精彩评论