php - Will a LIKE function give me better results when Match Against function does not give any results in Mysql -
currently, when type 'm' in search engine not results inspite of having 'math' value in database. (i using match against function of mysql on website). decided make code first runs match against function , if no output obtained, again runs search time using 'like' function of sql , if still no result obtained shows alert 'no values found'. gist of code this..
mysql(match against function) if (mysqli_num_rows > 0) {output} elseif (mysqli_num_rows < 1) { mysqli (like function) show output} else {show alert'no results'} will code give result 'math' when enter 'm' in search engine.
the actual code.
<?php if (isset($_post['go'])) { $search = $_post['search']; $college = $_post['colleges']; if (!empty($search)) { if(isset($_post['colleges']) ) { $query1 = "select filename, description, groupid, emailid, college, upload_date images match(description) against('$search') , college = '$college' group groupid order match(description) against('$search') desc"; $query_run1 = mysqli_query($con, $query1) ; } else { $query1 = "select filename, description, groupid, emailid, college,upload_date images match(description) against('$search') group groupid order match(description) against('$search') desc"; $query_run1 = mysqli_query($con, $query1) ; $searchresults = mysqli_num_rows($query_run1); ?><p class = "totalresults"> <br /> <br />     <font size= "4" >total results : <?php echo $searchresults ; ?> </font> </p> <?php if(mysqli_num_rows($query_run1)>0) { ?> <table class="searchenginelist" style="color: black;"> <tr> <th> uploader </th> <th> title </th> <th> date </th> <th> upvotes </th> <th> downvotes </th> <th> college </th> </tr> <?php while( $rows = mysqli_fetch_assoc($query_run1)) { //$imagedisplay = $rows['file']; $imagename = $rows['filename']; // mysqli_real_escape_string($con,$imagedisplay); $descrip = $rows['description']; $groupid = $rows['groupid']; //$groupid = $_session['groupid'] ; $uploader_emailid = $rows['emailid']; $college = $rows['college']; ?> <tr> <td width="70px"> <?php $query = " select username userinfo emailid = '$uploader_emailid' " ; $query_run = mysqli_query($con,$query ); $raw = mysqli_fetch_assoc($query_run); echo $raw['username']; ?> </td> <td width="450px"> <a href="imagespace.php?groupid=<?php echo $groupid ;?>"> <?php echo $descrip ; ?> </a> </td> <td style="font-size:15px;" width="85px"> <?php echo $rows['upload_date'] ; ?> </td> <td align="center" style="color:green;" width="50px"> <?php $query = "select vote votes groupid = '$groupid' , vote = '1' "; $query_run= mysqli_query($con, $query); $upvote=mysqli_num_rows($query_run) ; echo $upvote; ?> </td> <td align="center" style="color:red;" width="50px"> <?php $query = "select vote votes groupid = '$groupid' , vote = '0' "; $query_run= mysqli_query($con, $query); $downvote=mysqli_num_rows($query_run) ; echo $downvote; ?> </td> <td style="font-size:13px;" width="130px"> <?php echo $college; ?> </td> </tr> <?php } ?> </table> <?php } elseif(mysqli_num_rows($query_run1) < 1) { $query12 = "select * images description '%".mysqli_real_escape_string($con,$search)."%' group groupid order groupid desc"; $query_run12 = mysqli_query($con, $query12) ; ?> <table class="searchenginelist" style="color: black;"> <tr> <th> uploader </th> <th> title </th> <th> date </th> <th> upvotes </th> <th> downvotes </th> <th> college </th> </tr> <?php while( $rows = mysqli_fetch_assoc($query_run12)) { //$imagedisplay = $rows['file']; $imagename = $rows['filename']; // mysqli_real_escape_string($con,$imagedisplay); $descrip = $rows['description']; $groupid = $rows['groupid']; //$groupid = $_session['groupid'] ; $uploader_emailid = $rows['emailid']; $college = $rows['college']; ?> <tr> <td width="70px"> <?php $query = " select username userinfo emailid = '$uploader_emailid' " ; $query_run = mysqli_query($con,$query ); $raw = mysqli_fetch_assoc($query_run); echo $raw['username']; ?> </td> <td width="450px"> <a href="imagespace.php?groupid=<?php echo $groupid ;?>"> <?php echo $descrip ; ?> </a> </td> <td style="font-size:15px;" width="85px"> <?php echo $rows['upload_date'] ; ?> </td> <td align="center" style="color:green;" width="50px"> <?php $query = "select vote votes groupid = '$groupid' , vote = '1' "; $query_run= mysqli_query($con, $query); $upvote=mysqli_num_rows($query_run) ; echo $upvote; ?> </td> <td align="center" style="color:red;" width="50px"> <?php $query = "select vote votes groupid = '$groupid' , vote = '0' "; $query_run= mysqli_query($con, $query); $downvote=mysqli_num_rows($query_run) ; echo $downvote; ?> </td> <td style="font-size:13px;" width="130px"> <?php echo $college; ?> </td> </tr> } ?> </table> <?php } else { echo '<script type="text/javascript"> alert ("no results found") </script>' ; } } else { echo '<script type="text/javascript"> alert ("no data entered") </script>'; } } }
you have syntax issues in code, else statements not lined properly, page not working. (for example, had } else { lined } else {).
i went through of code , formatted see issue was, remember, sensible code indentation idea. helps read code , more importantly you debug code. take quick @ coding standard own benefit. may asked amend code in few weeks/months , thank me in end.
<?php if (isset($_post['go'])) { $search = $_post['search']; $college = $_post['colleges']; if (!empty($search)) { if(isset($_post['colleges']) ) { $query1 = "select filename, description, groupid, emailid, college, upload_date images match(description) against('$search') , college = '$college' group groupid order match(description) against('$search') desc"; $query_run1 = mysqli_query($con, $query1); } else { $query1 = "select filename, description, groupid, emailid, college,upload_date images match(description) against('$search') group groupid order match(description) against('$search') desc"; $query_run1 = mysqli_query($con, $query1); $searchresults = mysqli_num_rows($query_run1); ?> <p class = "totalresults"> <br /> <br />     <font size= "4" >total results: <?php echo $searchresults; ?></font> </p> <?php if(mysqli_num_rows($query_run1)>0) { ?> <table class="searchenginelist" style="color: black;"> <tr> <th> uploader </th> <th> title </th> <th> date </th> <th> upvotes </th> <th> downvotes </th> <th> college </th> </tr> <?php while( $rows = mysqli_fetch_assoc($query_run1)) { //$imagedisplay = $rows['file']; $imagename = $rows['filename']; // mysqli_real_escape_string($con,$imagedisplay); $descrip = $rows['description']; $groupid = $rows['groupid']; //$groupid = $_session['groupid'] ; $uploader_emailid = $rows['emailid']; $college = $rows['college']; ?> <tr> <td width="70px"> <?php $query = " select username userinfo emailid = '$uploader_emailid' " ; $query_run = mysqli_query($con,$query ); $raw = mysqli_fetch_assoc($query_run); echo $raw['username']; ?> </td> <td width="450px"> <a href="imagespace.php?groupid=<?php echo $groupid ;?>"> <?php echo $descrip; ?> </a> </td> <td style="font-size:15px;" width="85px"> <?php echo $rows['upload_date'] ; ?> </td> <td align="center" style="color:green;" width="50px"> <?php $query = "select vote votes groupid = '$groupid' , vote = '1' "; $query_run= mysqli_query($con, $query); $upvote=mysqli_num_rows($query_run) ; echo $upvote; ?> </td> <td align="center" style="color:red;" width="50px"> <?php $query = "select vote votes groupid = '$groupid' , vote = '0' "; $query_run= mysqli_query($con, $query); $downvote=mysqli_num_rows($query_run) ; echo $downvote; ?> </td> <td style="font-size:13px;" width="130px"> <?php echo $college; ?> </td> </tr> <?php } //end while loop ?> </table> <?php } else if(mysqli_num_rows($query_run1) < 1) { $query12 = "select * images description '%".mysqli_real_escape_string($con,$search)."%' group groupid order groupid desc"; $query_run12 = mysqli_query($con, $query12) ; ?> <table class="searchenginelist" style="color: black;"> <tr> <th> uploader </th> <th> title </th> <th> date </th> <th> upvotes </th> <th> downvotes </th> <th> college </th> </tr> <?php while( $rows = mysqli_fetch_assoc($query_run12)) { //$imagedisplay = $rows['file']; $imagename = $rows['filename']; // mysqli_real_escape_string($con,$imagedisplay); $descrip = $rows['description']; $groupid = $rows['groupid']; //$groupid = $_session['groupid'] ; $uploader_emailid = $rows['emailid']; $college = $rows['college']; ?> <tr> <td width="70px"> <?php $query = " select username userinfo emailid = '$uploader_emailid' " ; $query_run = mysqli_query($con,$query ); $raw = mysqli_fetch_assoc($query_run); echo $raw['username']; ?> </td> <td width="450px"> <a href="imagespace.php?groupid=<?php echo $groupid ;?>"> <?php echo $descrip ; ?> </a> </td> <td style="font-size:15px;" width="85px"> <?php echo $rows['upload_date'] ; ?> </td> <td align="center" style="color:green;" width="50px"> <?php $query = "select vote votes groupid = '$groupid' , vote = '1' "; $query_run= mysqli_query($con, $query); $upvote=mysqli_num_rows($query_run) ; echo $upvote; ?> </td> <td align="center" style="color:red;" width="50px"> <?php $query = "select vote votes groupid = '$groupid' , vote = '0' "; $query_run= mysqli_query($con, $query); $downvote=mysqli_num_rows($query_run) ; echo $downvote; ?> </td> <td style="font-size:13px;" width="130px"> <?php echo $college; ?> </td> </tr> </table> <?php } //end while loop } else { //num rows < 1 echo '<script type="text/javascript"> alert ("no results found") </script>' ; } } } else { //no searches returned echo '<script type="text/javascript"> alert ("no data entered") </script>'; } } i suggest, complex, nested structures, comment each ending bracket goes did in code. put comments see } ending.
warning: this code may vulnerable sql injection attacks. should learn prepared statements mysqli or pdo. recommend
pdo, wrote function for make extremely easy, clean, , way more secure using non-parameterized queries.
Comments
Post a Comment