Page 1 of 1

php search help

Posted: Thu Jan 22, 2015 12:59 pm
by 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");?>

Re: php search help

Posted: Thu Jan 22, 2015 1:25 pm
by 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
}

Re: php search help

Posted: Thu Jan 22, 2015 4:02 pm
by 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! 
}

Re: php search help

Posted: Thu Jan 22, 2015 6:08 pm
by 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.

Re: php search help

Posted: Thu Jan 22, 2015 8:00 pm
by visualtech
THANK GOD SOMEONE IS THERE FOR MYSQLI!!! HI5!! #zachman61 And all I ever get is eyes looking at me, about to slaughter me! :\