Page 1 of 1
IF in doubt
Posted: Wed Jul 20, 2005 3:17 am
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);
?>
Re: IF in doubt
Posted: Wed Jul 20, 2005 3:24 am
by harrisonad
watch for your structures.
Code: Select all
if ($yesno == "Yes"){
// do stuff
}elseif ($yesno == "No"){
// do stuff
}
Posted: Wed Jul 20, 2005 3:27 am
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);
?>
Posted: Wed Jul 20, 2005 3:37 am
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....
Posted: Wed Jul 20, 2005 4:11 am
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);
}
Posted: Wed Jul 20, 2005 4:39 am
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.

Posted: Wed Jul 20, 2005 4:40 am
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?
Posted: Wed Jul 20, 2005 4:56 am
by harrisonad
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];