php search help

5 posts Page 1 of 1
Contributors
User avatar
Mark
VIP - Donator
VIP - Donator
Posts: 372
Joined: Mon Aug 17, 2009 10:35 pm

php search help
Mark
hello,

can anyone help me with my search page please.

ive got it showing the search results all I need to do now if none is return display a message saying no posts here is my code.
Code: Select all
<?php require("./styles/top.php");?>

<h1>Here is your search result</h1>

<?php

include("includes/connect.php");

if(isset($_GET['search'])){
    
    $search_id = $_GET['value'];
    
    $search_query = "select * from posts where post_keywords like '$search_id'";
    
    $run_query = mysql_query($search_query);
    
        while ($search_row=mysql_fetch_array($run_query)){
        
        $post_id = $search_row['post_id'];
        $post_title = $search_row['post_title'];
        $post_image = $search_row['post_image'];
        $post_date  = $search_row['post_date'];
        $post_author = $search_row['post_author'];
        $post_content = substr($search_row['post_content'],0,150);
          
?>
 
<div id="container">
  <div id="image"><?php if($post_image != ""){ ?><img src="/cms/pics/<?php echo $post_image; ?>" /> <?php } ?></div>
  <div id="title"><?php echo $post_title; ?></div>
  <div id="posted_by">Published on: <?php echo $post_date ?> By <?php echo $post_author ?></div>
  <hr style="width: 75%;" />
  <div id="content"><?php echo $post_content; ?></div>
</div>


<?php } } ?>

<?php require("./styles/bottom.php");?>
http://www.mbappz.com
User avatar
comathi
Coding God
Coding God
Posts: 1242
Joined: Fri Mar 26, 2010 1:59 pm

Re: php search help
comathi
You can use PHP's empty() function to make sure that $run_query isn't empty:
Code: Select all
if(!empty($run_query)){
//Display the results
}
User avatar
visualtech
VIP - Donator
VIP - Donator
Posts: 265
Joined: Sat Nov 19, 2011 2:19 pm

Re: php search help
visualtech
#comathi, I won't recommend doing that! I've had BAD experiences with the empty() function before, and I believe that its a torture, that no one should bear.

A little "mysql-oriented" way, also, the perfect way would be to use the MySQL's num_row's function. Have a look below:
Code: Select all
if (mysql_num_rows($run_query) != 0) {
  // Do your mojo here! 
}
Image
User avatar
zachman61
VIP - Donator
VIP - Donator
Posts: 1891
Joined: Wed Dec 16, 2009 9:56 pm

Re: php search help
zachman61
visualtech wrote:
#comathi, I won't recommend doing that! I've had BAD experiences with the empty() function before, and I believe that its a torture, that no one should bear.

A little "mysql-oriented" way, also, the perfect way would be to use the MySQL's num_row's function. Have a look below:
Code: Select all
if (mysql_num_rows($run_query) != 0) {
  // Do your mojo here! 
}
All mysql functions are deprecated. mysqli should be used in all instanced where PDO is not used.
Nailing my feet to the floor is easier than using my own muscles to balance, but you don't see me doing that :)
User avatar
visualtech
VIP - Donator
VIP - Donator
Posts: 265
Joined: Sat Nov 19, 2011 2:19 pm

Re: php search help
visualtech
THANK GOD SOMEONE IS THERE FOR MYSQLI!!! HI5!! #zachman61 And all I ever get is eyes looking at me, about to slaughter me! :\
Image
5 posts Page 1 of 1
Return to “Help & Support”