philbob Posted November 8, 2015 Share Posted November 8, 2015 Hi to you all can i say am very very new to php, and any help would be appreciated thanks. I keep getting this error Parse error: syntax error, unexpected 'else' (T_ELSE) if ($xcatname) $resa = mysql_query("SELECT catdesc FROM `$t_cats` WHERE `catname` LIKE '".$xcatname."'"); $rowa = mysql_fetch_array($resa); $desc_msg = $rowa['catdesc']; { echo '<div id="showad-textbox">'.$rowa['catdesc'].'</div><br>'; } elseif($xsubcatname) $resa = mysql_query("SELECT subdesc FROM `$t_subcats` WHERE `subcatname` LIKE '".$xsubcatname."'"); $rowa = mysql_fetch_array($resa); $desc_msg = $rowa['subdesc']; { echo '<div id="showad-textbox">'.$rowa['subdesc'].'</div><br>'; } If i change the elseif to if then no error but i see both text descriptions any help thanks phil Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted November 8, 2015 Share Posted November 8, 2015 (edited) You have a malformed if/else syntax, The lines highlighted in red need to be within the braces { } if ($xcatname) $resa = mysql_query("SELECT catdesc FROM `$t_cats` WHERE `catname` LIKE '".$xcatname."'");$rowa = mysql_fetch_array($resa);$desc_msg = $rowa['catdesc'];{echo '<div id="showad-textbox">'.$rowa['catdesc'].'</div><br>';}elseif($xsubcatname) $resa = mysql_query("SELECT subdesc FROM `$t_subcats` WHERE `subcatname` LIKE '".$xsubcatname."'");$rowa = mysql_fetch_array($resa);$desc_msg = $rowa['subdesc'];{ echo '<div id="showad-textbox">'.$rowa['subdesc'].'</div><br>';} Edited November 8, 2015 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
philbob Posted November 8, 2015 Author Share Posted November 8, 2015 Thanks you for your quick reply I tried if ($xcatname $resa = mysql_query("SELECT catdesc FROM `$t_cats` WHERE `catname` LIKE '".$xcatname."'"); $rowa = mysql_fetch_array($resa); $desc_msg = $rowa['catdesc']; ) { echo '<div id="showad-textbox">'.$rowa['catdesc'].'</div><br>'; } elseif($xsubcatname $resa = mysql_query("SELECT subdesc FROM `$t_subcats` WHERE `subcatname` LIKE '".$xsubcatname."'"); $rowa = mysql_fetch_array($resa); $desc_msg = $rowa['subdesc']; ) { echo '<div id="showad-textbox">'.$rowa['subdesc'].'</div><br>'; } but now getting error syntax error, unexpected '$resa' (T_VARIABLE) in your code on line 1 Phil Quote Link to comment Share on other sites More sharing options...
Barand Posted November 8, 2015 Share Posted November 8, 2015 If you have "syntax error, unexpected xxx" then the error is just before the xxx. That is what makes it "unexpected" Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted November 8, 2015 Share Posted November 8, 2015 Thanks you for your quick reply I tried WHAT!? How did you come up with that? Was my post not clear enough? Quote Link to comment Share on other sites More sharing options...
philbob Posted November 8, 2015 Author Share Posted November 8, 2015 WHAT!? How did you come up with that? Was my post not clear enough? Sorry mis read post, still having same error syntax error, unexpected 'elseif' (T_ELSEIF) in your code on line 9 if ($xcatname) {$resa = mysql_query("SELECT catdesc FROM `$t_cats` WHERE `catname` LIKE '".$xcatname."'");} {$rowa = mysql_fetch_array($resa);} {$desc_msg = $rowa['catdesc'];} { echo '<div id="showad-textbox">'.$rowa['catdesc'].'</div><br>'; } elseif($xsubcatname) {$resa = mysql_query("SELECT subdesc FROM `$t_subcats` WHERE `subcatname` LIKE '".$xsubcatname."'");} {$rowa = mysql_fetch_array($resa);} {$desc_msg = $rowa['subdesc'];} { echo '<div id="showad-textbox">'.$rowa['subdesc'].'</div><br>'; } thanks again for your help Quote Link to comment Share on other sites More sharing options...
philbob Posted November 8, 2015 Author Share Posted November 8, 2015 If you have "syntax error, unexpected xxx" then the error is just before the xxx. That is what makes it "unexpected" thanks Barand i'll check it out...phil Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted November 9, 2015 Share Posted November 9, 2015 Sorry mis read post, still having same error syntax error, unexpected 'elseif' (T_ELSEIF) in your code on line 9 Are you serious? You got to be trolling me right? 1 Quote Link to comment Share on other sites More sharing options...
Solution QuickOldCar Posted November 9, 2015 Solution Share Posted November 9, 2015 Could do with two if's and actually use the variable are setting, then echo after if($xcatname) { $query = "SELECT catdesc FROM `$t_cats` WHERE `catname` = '".$xcatname."'"; if ($resa = mysql_query($query)) { while($rowa = mysql_fetch_assoc($resa)){ $desc_msg = $rowa['catdesc']; } } } if($xsubcatname) { $query = "SELECT subdesc FROM `$t_subcats` WHERE `subcatname` = '".$xsubcatname."'"; if ($resa = mysql_query($query)) { while($rowa = mysql_fetch_assoc($resa)){ $desc_msg = $rowa['subdesc']; } } } if($desc_msg){ echo '<div id="showad-textbox">'.$desc_msg.'</div><br>'; } Shouldn't use the outdated mysql_ functions and use mysqli_ or pdo Should also escape or use prepared statements for anything inserted into queries. Another thing is that when you fetch an array it returns both the numerical index and associative array, double the data. If you do not need the numerical array use mysqli_fetch_assoc There are example codes there can look over. Any reason are using LIKE? I would assume your categories are specific since not doing a while loop, in that case can just use an = versus LIKE If using LIKE for searching... Firstly would be using a while loop for multiple results. % are for wildcards If did have categories with similar words would want to place a % in front and after the term so can find any similar that at least contains the term. "SELECT catdesc FROM `$t_cats` WHERE `catname` LIKE '%".$xcatname."%'" "SELECT subdesc FROM `$t_subcats` WHERE `subcatname`LIKE '%".$xsubcatname."%'" I personally like using full-text search in boolean mode, When you get to using multiple where/and , then also multiple columns, full-text simplifies the queries and additionally makes for very specific searches. Quote Link to comment Share on other sites More sharing options...
philbob Posted November 9, 2015 Author Share Posted November 9, 2015 @Quickoldcar Thank you so much for your expert advice…Phil Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.