Forum Moderators: coopster
<?php
// connect to the database
require_once ('../Connections/mysql_connect2.php');// generate and execute query
$query = "SELECT * FROM recipe_titles WHERE titleID = '$titleID'";
$result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());
// if a result is returned
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
?>
<p><a href="/phpprint.php">Printer-friendly version</a></p>
<!-- startprint -->
<p><img src="<?php echo $row['imagepath'];?>"></p>
<h4><strong><?php echo $row['title'];?></strong></h4>
<p><strong>Ingredients:</strong><br>
<?php echo $row['ingredients'];?></p>
<p><strong>Directions:</strong><br>
<?php echo $row['directions'];?></p>
<p>Contributed by <em><?php echo $row['contributor'];?></em></p>
<!-- stopprint -->
<?php
}
?>
But, I also want to use the <?php echo $row['title'];?> in my title tag to generate a title for that page.
So, I added this between my title tags:
<title><?php
// connect to the database
require_once ('../Connections/mysql_connect2.php');
// generate and execute query
$query = "SELECT title FROM recipe_titles WHERE titleID = '$titleID'";
$result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());
// if a result is returned
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
?>
<?php echo $row['title'];?></title><?php }?>
As a result, I get the title, but the second query in the body generates an error message:
Error in query: SELECT * FROM recipe_titles WHERE titleID = '8'. Unknown column 'titleID' in 'where clause'
I've tried putting this at the top of the page:
global $titleID;
$titleID = 'titleID';
but that didn't help. The titleID is passed to this page from a link on a previous page.
What do I need to do so that both queries will work?
Thanks,
ksp
$query="SELECT * FROM ... etc";
$result=mysql_query($query);
$row=mysql_fetch_array($result);
No need to "globalize" anything; you can print() bits of $row whenever and wherever you want.
<title><?php print($row['title'])?></title><body>
<i><?php print($row['ingredients']);?></i>
</body>
good luck!
You can reset the position by calling
mysql_data_seek ( resource result_identifier, int row_number)
//in your particular case
mysql_data_seek ( $result, 0);
just before the while statement - this will reset the result set to the first row.
Unfortunately, I didn't have any luck... I should mention too that between the title and where the code is added again in the body, there are includes and another different database connection for the template files this page is based on. Perhaps that makes a difference?
Here's what I used first, for the title--I could not get it to work with an *, but had to specifically call out the name, title, in order for the title to show:
<?php
// connect to the database
require ('../Connections/mysql_connect2.php');// generate and execute query
$query = "SELECT title FROM recipe_titles WHERE titleID = '$titleID'";
$result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());
// if a result is returned
mysql_data_seek ($result, 0);
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
?>
<?php echo $row['title'];?>
</title><?php }?>
Later in the body of the page, after the includes, I have this:
<p><img src="<?php echo $row['imagepath'];?>"></p>
<h4><strong><?php echo $row['title'];?></strong></h4>
<p><strong>Ingredients:</strong><br>
<?php echo $row['ingredients'];?></p>
<p><strong>Directions:</strong><br>
<?php echo $row['directions'];?></p>
<p>Contributed by <em><?php echo $row['contributor'];?></em></p>
But the above code only prints out Ingredients:,
Directions:, Contributed by: without adding any info from the database, even if I have an asterisk * instead of the field name in the query.
Any thoughts on why I'm not getting the info returned?
Thanks again,
ksd