Page 1 of 2
if statement causing page not to load
Posted: Mon Jul 19, 2010 4:18 am
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
Re: if statement causing page not to load
Posted: Mon Jul 19, 2010 4:45 am
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
Re: if statement causing page not to load
Posted: Mon Jul 19, 2010 4:55 am
by aravona
Ok I added in
(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.
Re: if statement causing page not to load
Posted: Mon Jul 19, 2010 5:12 am
by pbs
Remove "@" from $result = @mysql_query($sql);
and check whether it is giving any error.
Re: if statement causing page not to load
Posted: Mon Jul 19, 2010 5:18 am
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.
Re: if statement causing page not to load
Posted: Mon Jul 19, 2010 6:26 am
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.
Re: if statement causing page not to load
Posted: Mon Jul 19, 2010 6:38 am
by buckit
add something to the else part of your if statement... sounds like accessory doesnt =1 so its not printing anything.
Re: if statement causing page not to load
Posted: Mon Jul 19, 2010 7:10 am
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.
Re: if statement causing page not to load
Posted: Mon Jul 19, 2010 8:22 am
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.
That will show you the value of the array within $query_data. ie. see what accessories actually equals.
Re: if statement causing page not to load
Posted: Mon Jul 19, 2010 8:30 am
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
Re: if statement causing page not to load
Posted: Mon Jul 19, 2010 8:32 am
by buckit
try
if($result['accessory']){
}
or
if(is_array($result)){
}
Re: if statement causing page not to load
Posted: Mon Jul 19, 2010 8:37 am
by aravona
Neither of those work either. This is the most frustrating IF I've ever worked on!
Re: if statement causing page not to load
Posted: Mon Jul 19, 2010 8:52 am
by buckit
what type of field is accessory? varchar? int? if boolean then it should be tinyint
Re: if statement causing page not to load
Posted: Mon Jul 19, 2010 8:57 am
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.
Re: if statement causing page not to load
Posted: Mon Jul 19, 2010 9:02 am
by buckit
post the exact code you used to get that