Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Variables being ignored?

Status
Not open for further replies.

frozenpeas

Technical User
Sep 13, 2001
893
0
0
CA
Hello. A few of you have already helped me on a related issue. Here is the page in question:


The box links on the left are generated from $myQuery below.

Code:
mysql_select_db($database_connect, $connect);
if(!$type){
	$type = "web";
}
$query_myQuery = "SELECT * FROM folio WHERE type = '".$type."'";
$myQuery = mysql_query($query_myQuery, $connect) or die("Could not access database. ".mysql_error());
$row_myQuery = mysql_fetch_assoc($myQuery);
$totalRows_myQuery = mysql_num_rows($myQuery);
if(isset($project)){
	$query_myQuery2 = "SELECT * FROM folio WHERE type = '".$type."' AND project = '".$project."'";
	$myQuery2 = mysql_query($query_myQuery2, $connect) or die("Could not access database. ".mysql_error());
	$row_myQuery2 = mysql_fetch_assoc($myQuery2);
	$totalRows_myQuery2 = mysql_num_rows($myQuery2);
}

When you click on a link, the following code should display its details using $myQuery2:

Code:
<? if($row_myQuery2){ ?>
	<p class="center"><img src="<? echo $type;?>/images/<? echo $row_myQuery2['pic'];?>" border="0" title="<? echo $row_myQuery2['name'];?>" /></p>
	<p><strong><? echo $row_myQuery2['name'];?></strong>
	<? if($row_myQuery2['url']){?>
	 | <a href="<? echo $row_myQuery2['url'];?>" target="_blank">launch website</a></p>
	<? }?>
	<p><? echo $row_myQuery2['description'];?></p>
	<? if($row_myQuery2['samples']){?>
	<p><? echo $row_myQuery2['samples'];?></p>
	<? }

The $project and $type variables are indeed being set. From what I can tell, $row_myQuery2 is either empty or being reset. Does anyone have an idea as to why?

Thanks.

frozenpeas
 
Well I can't look into your database. Maybe there aren't any records?

If echo the query and paste it in phpMyAdmin does that generate results?
 
There are records in the database. Like I said, the results for the first query work fine. I was able to pull all the records from the database in this manner but just can't get it to work with a second query.

frozenpeas
 
So have you echoed the query and tested it?

Obviously something must be wrong with $row_myQuery2

Maybe you can display the content of that.
var_dump($row_myQuery2)

Maybe that will point you to a solution.
 
There are a few things that should be commented on:
1. It appears that you have register_globals set to ON. Not such a good idea, for reasons illustreated in detail in the PHP manual. Start using $_GET['type'] instead.
2. I don't know how your error reporting level is set, but when there is no result row and you try a mysql_fetch_assoc() PHP would spit out a Warning.
3. You write your code in context switching mode. A bit of PHP, some HTML, some more PHP. This is resource intensive and your script will be much slower than staying in one mode. It's better to produce the HTML output with echo or print statments rather than switching context.
4. The error checing after the mysql_query() should read "Query error" since an error occuring at this stage has nothing to do with the connection to the database.

Of course, these are only recommendations.

I recommend after doing the query to:
1. echo out the SQL string and inspect the query
2. inspect mysql_num_rows() to ascertain the number of rows returned.

 
Thanks for the advice. I've cleaned things up a bit and have been able to narrow my problem down better.

Everything checks out until:

Code:
$row_myQuery2 = mysql_fetch_assoc($myQuery2);

So when I am using this in my while loop, it is returned as false.

I don't have any idea as to why this is a trouble spot. I have also tried:

Code:
$row_myQuery2 = mysql_fetch_array($myQuery2);

but to no avail. It is odd... the set using $myQuery is set up the same way and works fine.

Thank you again.

frozenpeas
 
What is your while loop based on?
Maybe you should just do
Code:
while ($row_myQuery2 = mysql_fetch_assoc($myQuery2)){
   # do hwat you need to do etc....
}

Also, what is the exact error message you get?
 
I am using a do/while loop:

Code:
do{
	echo "pic = ".$row_myQuery2['pic'];
}while($row_myQuery2 = mysql_fetch_assoc($myQuery2));

No error message is generated. The echo returns "pic = ".

If I use:

Code:
while($row_myQuery2 = mysql_fetch_assoc($myQuery2)){
	echo "pic = ".$row_myQuery2['pic'];
}

No error message is generated. Nothing is echoed.

frozenpeas
 
Let's repeat the basic troubleshooting steps:
1. Have we checked if the query fails?
2. Have we inspected the number of rows returned?
3. Have we cheked the content of the retrieved rows?

For #3:
Just put a print_r($row-myQuery2) in the second loop shown above. However, I think that there are no rows returned. Check steps 1 and 2.

For #1:
echo out the SQL string. Past it in your favorite MySQL admin tool and see if it works there.

The first loop will not work. Be aware that a do-while construct checks the condition at the end of the loop, not the beginning. Your expression retrieves the row at the end of the loop, that means the instruction in the do portion will fail the first time since the row itself has not yet been retrieved.

Let's go from here.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top