if statement causing page not to load

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

aravona
Forum Contributor
Posts: 347
Joined: Sat Jun 13, 2009 3:59 pm
Location: England

if statement causing page not to load

Post by aravona »

I've got a page to display some products, and I need it to display a new set of code if a product has an 'accessory'

I've added a TinyInt to my database and if its set 1 I want to display the new code, however the if statement I've put in just lags the page out and wont display at all (cannot display the webpage error)

Code: Select all

<?php 
	$sql = "SELECT accessory FROM ver_pro";
	$result = @mysql_query($sql);
	$query_data = mysql_fetch_assoc($result);
		if ($query_data==1){
		?>
		<tr>
		<td> code to come </td>
		</tr>
		<?php
		}
		else {}
		?>
Probs just me being an idiot, but any help would be awesome.

Thanks,

Aravona
pbs
Forum Contributor
Posts: 230
Joined: Fri Nov 07, 2008 5:31 am
Location: Nashik, India
Contact:

Re: if statement causing page not to load

Post by pbs »

Code: Select all

$query_data = mysql_fetch_assoc($result)
above code will return you an array, so your if condition will be live this

Code: Select all

if ($query_data[FIELDNAME] == 1)
{
//code here
}
you are just checking $query_data = 1, which will not work
aravona
Forum Contributor
Posts: 347
Joined: Sat Jun 13, 2009 3:59 pm
Location: England

Re: if statement causing page not to load

Post by aravona »

Ok I added in

Code: Select all

if ($query_data['accessory']==1)
(tried with and without quotes) but still nothing, pages just try and load and simply crash. I didn't think I needed any of that cos I'm pulling a tinyint from the server, so it can only be 1 number, and theres only 1 product on the page at any one time.

But, alas :( didnt work.
pbs
Forum Contributor
Posts: 230
Joined: Fri Nov 07, 2008 5:31 am
Location: Nashik, India
Contact:

Re: if statement causing page not to load

Post by pbs »

Remove "@" from $result = @mysql_query($sql);

and check whether it is giving any error.
aravona
Forum Contributor
Posts: 347
Joined: Sat Jun 13, 2009 3:59 pm
Location: England

Re: if statement causing page not to load

Post by aravona »

Bit better I guess, instead of it failing the load the page it just loads everything completely blank > nothings read at all.

No IE error this time. But no php error either?

EDIT: oh no, second time around just the IE fail to load again.
aravona
Forum Contributor
Posts: 347
Joined: Sat Jun 13, 2009 3:59 pm
Location: England

Re: if statement causing page not to load

Post by aravona »

I've even tried hard coding the if statement with the product code to see if it was getting confused.

Code: Select all

$sql = "SELECT accessory FROM ver_pro where prCode = 1000107";
Still the same issue and that with it definately looking at only 1 product.
buckit
Forum Contributor
Posts: 169
Joined: Fri Jan 01, 2010 10:21 am

Re: if statement causing page not to load

Post by buckit »

add something to the else part of your if statement... sounds like accessory doesnt =1 so its not printing anything.
aravona
Forum Contributor
Posts: 347
Joined: Sat Jun 13, 2009 3:59 pm
Location: England

Re: if statement causing page not to load

Post by aravona »

Code: Select all

<?php 
	$sql = "SELECT accessory FROM ver_pro where pID='1' AND prCode = 1000107";
	$result = mysql_query($sql);
	$query_data = mysql_fetch_assoc($result);
	if ($query_data == 1){
?>
				
// code to come

<?php
}
else {
echo 'hello';
}
?>
Thats what the codes like now, and I am jsut getting 'hello' posted on the page, but at least the page is loading. So its not recognising that it needs to only select accessory from the products table based on the product code / pId >.< very annoying... but I can't see why ?

If I add back in the [accessory] that was suggested before, the 'code to come' turns up, however... it turns up then on EVERY page, and it should only be on the one.
buckit
Forum Contributor
Posts: 169
Joined: Fri Jan 01, 2010 10:21 am

Re: if statement causing page not to load

Post by buckit »

It was loading before... it just didnt have anything to display so you got a white screen.

$query_data is an array... thats how data is returned to you even if its only 1 variable. so where you put $query_data == 1... $query_data will never equal 1. $query_data will equal 'array' at best.

now you still have the problem that your returned recordset field called 'accessories' does NOT equal 1. thats why you are getting a blank page.

first things first... find out what MySQL is returning to you. remove your if statement and do the following.

Code: Select all

echo print_r($query_data)
That will show you the value of the array within $query_data. ie. see what accessories actually equals.
aravona
Forum Contributor
Posts: 347
Joined: Sat Jun 13, 2009 3:59 pm
Location: England

Re: if statement causing page not to load

Post by aravona »

Since I want a boolean, I also tried taking out some of that code and going with:

Code: Select all

$sql = "SELECT accessory FROM ver_pro where pID='1' AND prCode = '1000107'";
$result = mysql_query($sql);
if ($result == true){
//code
}
but this didnt work either, I can no longer tell if its my internet making the pages slow or not as they'll only load on code I know doesnt work!

I double checked the SQL in phpmyadmin, and the result was (in the phptable) Accessory 1 > which is exactly what I expected.

EDIT: also the snippet of code you suggested posted this: Array ( [accessory] => 1 ) 1
Last edited by aravona on Mon Jul 19, 2010 8:33 am, edited 1 time in total.
buckit
Forum Contributor
Posts: 169
Joined: Fri Jan 01, 2010 10:21 am

Re: if statement causing page not to load

Post by buckit »

try
if($result['accessory']){
}

or
if(is_array($result)){
}
aravona
Forum Contributor
Posts: 347
Joined: Sat Jun 13, 2009 3:59 pm
Location: England

Re: if statement causing page not to load

Post by aravona »

Neither of those work either. This is the most frustrating IF I've ever worked on!
buckit
Forum Contributor
Posts: 169
Joined: Fri Jan 01, 2010 10:21 am

Re: if statement causing page not to load

Post by buckit »

what type of field is accessory? varchar? int? if boolean then it should be tinyint
aravona
Forum Contributor
Posts: 347
Joined: Sat Jun 13, 2009 3:59 pm
Location: England

Re: if statement causing page not to load

Post by aravona »

It is a Tiny Int already, well TinyInt(1)

Well at least somethings working.... I get Array ( [accessory] => 1 ) 1fail on the page WITH accessory set to 1 in the DB and Array ( [accessory] => 0 ) 1fail for the page without accessory set to 1... so its understanding that at least.
buckit
Forum Contributor
Posts: 169
Joined: Fri Jan 01, 2010 10:21 am

Re: if statement causing page not to load

Post by buckit »

post the exact code you used to get that
Post Reply