homepage Welcome to WebmasterWorld Guest from 54.166.14.218
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Global variable
I think that's what I need
ksdfla

10+ Year Member



 
Msg#: 4141 posted 2:08 pm on Jun 18, 2004 (gmt 0)

The following query works fine in the body of my page:


<?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

 

httpwebwitch

WebmasterWorld Administrator httpwebwitch us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4141 posted 3:38 pm on Jun 18, 2004 (gmt 0)

Don't do multiple hits to the database. Just do one query at the top of the page, and reuse the returned array wherever you need to on the page.


$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!

DigitalSorceress

10+ Year Member



 
Msg#: 4141 posted 6:30 pm on Jun 18, 2004 (gmt 0)

I'd like to add to httpwebwitch's info a little... the query used like that will give you the contributor column value for the first row returned on the query, but it will also then advance the result set cursor to the next row ... mysql_fetch_array() does that - each time you call it, it returns an array of the whole row AND advances the position in the resultset to the next one.

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.

ksdfla

10+ Year Member



 
Msg#: 4141 posted 8:18 pm on Jun 21, 2004 (gmt 0)

Thank you both for your responses. I'm sorry I wasn't able to try them until today ...

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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved