Board index   FAQ   Search  
Register  Login
Board index php forum :: Database mySQL & php coding

Simple search PHP & MySQL

Codes here !

Moderators: macek, egami, gesf

Simple search PHP & MySQL

Postby bobocode » Wed Sep 04, 2013 2:10 am

Hi there,
I've created this simple search form in DW CS5.5:
Code: Select all
<head>
    <title>Search</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
    <form action="search.php" method="GET">
        <input type="text" name="query" />
        <input type="submit" value="Search" />
    </form>
</body>
</html>

And this search.php :
Code: Select all
<?php
mysql_connect("storage", "root", "") or die("Error connecting to database: ".mysql_error());

    mysql_select_db("tutorial_search") or die(mysql_error());
   
?>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Search results</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
<p>
  <?php
    $query = $_GET['query'];
       
    $min_length = 3;
         
    if(strlen($query) >= $min_length){
         
        $query = htmlspecialchars($query);
                 
        $query = mysql_real_escape_string($query);
                 
        $raw_results = mysql_query("SELECT * FROM articles
            WHERE (`title` LIKE '%".$query."%') OR (`text` LIKE '%".$query."%')") or die(mysql_error());   
       
        if(mysql_num_rows($raw_results) > 0){
             
            while($results = mysql_fetch_array($raw_results)){
                         
                echo "<p><h3>".$results['title']."</h3>".$results['text']."</p>";
                // posts results gotten from database(title and text) you can also show id ($results['id'])
            }
             
        }
        else{
            echo "No results";
        }
         
    }
    else{
        echo "Minimum length is ".$min_length;
    }
?>
<table width="348" border="1" cellpadding="2">
  <tr>
    <th width="336" height="45" scope="row"></th>
  </tr>
  <tr>
    <th height="65" scope="row">&nbsp;</th>
  </tr>
</table>
</body>
</html>

Everything works fine but I want to display results in a table field eg:
Untitled.png
image
Untitled.png (6.06 KiB) Viewed 283 times

Can anyone help me with this? I appreciate any help... Tnx in advance
bobocode
New php-forum User
New php-forum User
 
Posts: 2
Joined: Tue Sep 03, 2013 4:55 am

Re: Simple search PHP & MySQL

Postby johnj » Thu Sep 05, 2013 1:55 am

Use <table> tag in your html output.
johnj
php-forum Super User
php-forum Super User
 
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

SOLVED: Simple search PHP & MySQL

Postby bobocode » Thu Sep 05, 2013 11:17 pm

I add this after
Code: Select all
while($results = mysql_fetch_array($raw_results)){


Code: Select all

?>
           
<table width="50" border="1">
  <tr>
    <th scope="row"><?php echo $results['Id']?>&nbsp;</th>
  </tr>
</table>
<table width="50" border="1">
  <tr>
    <th scope="row"><?php echo $results['Ime']?>&nbsp;</th>
  </tr>
</table>
<p>
  <?php         }
             
        }
        else{
            echo "No results";
        }
         
    }
    else{
        echo "Minimum length is ".$min_length;
    }
?>
  </p>
<p>&nbsp;</p>
</body>
</html>


works like a charm :)
bobocode
New php-forum User
New php-forum User
 
Posts: 2
Joined: Tue Sep 03, 2013 4:55 am


Return to mySQL & php coding

Who is online

Users browsing this forum: No registered users and 0 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron