IF in doubt

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

Post Reply
User avatar
mhouldridge
Forum Contributor
Posts: 267
Joined: Wed Jan 26, 2005 5:13 am

IF in doubt

Post by mhouldridge »

Hi,

I need help with the following code, I am getting a parse error on line 30...

Code: Select all

<?
$db = mysql_connect("localhost","","") or die("Problem connecting");
mysql_select_db("audit") or die("Problem selecting database");
$query = "SELECT recon FROM dedicated";
$result = mysql_query($query) or die ("Query failed");
$yesno = $result;

if ($yesno == "Yes"){
	$img2 = 2;
	$result = @mysql_query("SELECT * FROM dedicated WHERE imgid=" . $img2 . ""); 

	while ( $row = mysql_fetch_array($result) ) 
	{ 
	$imgid = $row["imgid"];
	$encodeddata = $row["sixfourdata"];
	}
	echo base64_decode($encodeddata);
	
if ($yesno == "No"){
	$img1 = 1;
	$result = @mysql_query("SELECT * FROM dedicated WHERE imgid=" . $img1 . "");
	}
	while ( $row = mysql_fetch_array($result) ){
	$imgid = $row["imgid"];
	$encodeddata = $row["sixfourdata"]; 
	}
	echo base64_decode($encodeddata);
	
?>
User avatar
harrisonad
Forum Contributor
Posts: 288
Joined: Fri Oct 15, 2004 4:58 am
Location: Philippines
Contact:

Re: IF in doubt

Post by harrisonad »

watch for your structures.

Code: Select all

if ($yesno == "Yes"){
    // do stuff	
}elseif ($yesno == "No"){
    // do stuff
}
neugent
Forum Newbie
Posts: 24
Joined: Wed Jul 06, 2005 3:35 am
Location: Philippines

Post by neugent »

You forgot to close your {} in your first if statement.

Code: Select all

<?
$db = mysql_connect("localhost","","") or die("Problem connecting");
mysql_select_db("audit") or die("Problem selecting database");
$query = "SELECT recon FROM dedicated";
$result = mysql_query($query) or die ("Query failed");
$yesno = $result;

if ($yesno == "Yes"){
    $img2 = 2;
    $result = @mysql_query("SELECT * FROM dedicated WHERE imgid=" . $img2 . "");
    while ( $row = mysql_fetch_array($result) ){
	    $imgid = $row["imgid"];
    	$encodeddata = $row["sixfourdata"];
    }
    echo base64_decode($encodeddata);
}

if ($yesno == "No"){
    $img1 = 1;
    $result = @mysql_query("SELECT * FROM dedicated WHERE imgid=" . $img1 . "");
}

while ( $row = mysql_fetch_array($result) ){
	$imgid = $row["imgid"];
    $encodeddata = $row["sixfourdata"];
}

    echo base64_decode($encodeddata);

?>
User avatar
mhouldridge
Forum Contributor
Posts: 267
Joined: Wed Jan 26, 2005 5:13 am

Post by mhouldridge »

Here is what it looks like now, still getting parse error;

Code: Select all

<?
$db = mysql_connect("localhost","","") or die("Problem connecting");
mysql_select_db("audit") or die("Problem selecting database");
$query = "SELECT recon FROM dedicated";
$result = mysql_query($query) or die ("Query failed");
$yesno = $result;

if ($yesno == "Yes"){
	$result2 = @mysql_query("SELECT * FROM images WHERE imgid=". 2 . ""); 
	}
	while ( $row = mysql_fetch_array($result2) ) 
	{ 
	$imgid = $row["imgid"];
	$encodeddata = $row["sixfourdata"]; 
	} 
	echo base64_decode($encodeddata);
	
}else if ($yesno == "No"){
	$result2 = @mysql_query("SELECT * FROM images WHERE imgid=". 1 . "");
	}
	while ( $row = mysql_fetch_array($result2) ) 
	{ 
	$imgid = $row["imgid"];
	$encodeddata = $row["sixfourdata"]; 
	} 
	echo base64_decode($encodeddata);
?>



I cant pin this one down....
User avatar
harrisonad
Forum Contributor
Posts: 288
Joined: Fri Oct 15, 2004 4:58 am
Location: Philippines
Contact:

Post by harrisonad »

Code: Select all

if ($yesno == "Yes"){
	$result2 = @mysql_query("SELECT * FROM images WHERE imgid=". 2 . ""); 
	} // <-- this closing bracket doesn't belong here
	while ( $row = mysql_fetch_array($result2) ) 
	{ 
	$imgid = $row["imgid"];
	$encodeddata = $row["sixfourdata"]; 
	} 
	echo base64_decode($encodeddata);
	
}else if ($yesno == "No"){
	$result2 = @mysql_query("SELECT * FROM images WHERE imgid=". 1 . "");
	} // <-- this closing bracket doesn't belong here
	while ( $row = mysql_fetch_array($result2) ) 
	{ 
	$imgid = $row["imgid"];
	$encodeddata = $row["sixfourdata"]; 
	} 
	echo base64_decode($encodeddata);
}
User avatar
harrisonad
Forum Contributor
Posts: 288
Joined: Fri Oct 15, 2004 4:58 am
Location: Philippines
Contact:

Post by harrisonad »

I want to advice you of some pointers about your code.

First of all, try to remove redundancies in your code. I noticed that you repeated this part twice.

Code: Select all

while($row = mysql_fetch_array($result2)){
    $imgid = $row["imgid"];    
    $encodeddata = $row["sixfourdata"];     
}     
echo base64_decode($encodeddata);
only because the two queries have slight difference, which is the value of 'imgid', you tend to repeat this part to serve both of them.

My suggestion is this.

Code: Select all

// test for the value of $yesno
if($yesno=='Yes')
    $imgid = 2; // use another variable
elseif($yesno=='No')
    $imgid = 1;
// execute the repeated part only once.
// notice that we use the variable in this query
$result2 = @mysql_query("SELECT * FROM images WHERE imgid=$imgid");
// the rest of the code ..
That it! We must try to make our life simple.
The second one is about 'getting something we already have'.
exaine your code

Code: Select all

// ..
$result2 = @mysql_query("SELECT * FROM images WHERE imgid=". 2 . ""); // <-- you already know the imgid ...
// ..
while($row=mysql_fetch_array($result2)){   
    $imgid = $row["imgid"]; // <-- but still want to get it from db
    $encodeddata = $row["sixfourdata"];     
}
in this case, 'imgid' will still be 2 no matter what.
I hope you get it.

My appology to forum moderators for being so elementary in this post.
:oops:
User avatar
mhouldridge
Forum Contributor
Posts: 267
Joined: Wed Jan 26, 2005 5:13 am

Post by mhouldridge »

Hi,

Thanks for that...

What I am trying to do is look within a table called dedicated for Yes / No answers, and then run a script to display a particular image for either yes or no.

This script is called from another page as an include.

Here is the full script again;

Code: Select all

<?

$db = mysql_connect("localhost","","") or die("Problem connecting");
mysql_select_db("audit") or die("Problem selecting database");
$query = "SELECT recon FROM dedicated";
$result = mysql_query($query) or die ("Query failed");
$yesno = $result;

if ($yesno == "Yes"){ 
$result2 = @mysql_query("SELECT * FROM images WHERE imgid=". 2 . "");  
while ( $row = mysql_fetch_array($result2) )  {     
$imgid = $row["imgid"];    $encodeddata = $row["sixfourdata"];    
}     
echo base64_decode($encodeddata);    
}
else if ($yesno == "No"){   
$result2 = @mysql_query("SELECT * FROM images WHERE imgid=". 1 . "");     
while ( $row = mysql_fetch_array($result2) )     {     
$imgid = $row["imgid"];    $encodeddata = $row["sixfourdata"];     
}     
echo base64_decode($encodeddata);
}

?>
I first connect to my database and run through the table called dedicated.....

this captures the yes / no results and store them in the $result

I think my IF statement is incorrect - For the action under If, else IF, I have added the following;

Code: Select all

else if ($yesno == "No"){   
$result2 = @mysql_query("SELECT * FROM images WHERE imgid=". 1 . "");
Is this right - I want it to look under another table and do the code to dispaly the image...

Any ideas?
User avatar
harrisonad
Forum Contributor
Posts: 288
Joined: Fri Oct 15, 2004 4:58 am
Location: Philippines
Contact:

Post by harrisonad »

mhouldridge wrote:

Code: Select all

$yesno = $result;
mysql_query() function returns a resource for success, not the value itself.
another mysql function must be used to extract it's value;

Code: Select all

$data = mysql_fetch_row($result);
$yesno = $data[0];
Post Reply