Jump to content

[SOLVED] search box for website?


artweb

Recommended Posts

I need a search box for my website. So that a user can come and find things on my site just by typing in a word. When they click search it will give them a list of pages on my site that has to do with that word they searched for, with the links to the pages. I use mysql and php. I don't want to use google or any pre made search.

Link to comment
Share on other sites

Hi if you have a database with all your pages in it you could make a search box like this

 

<table>
<?php
$con=mysql_connect('host','user','pass');
mysql_select_db('site_info',$con);
$query="SELECT * FROM `pages` WHERE `page` LIKE ('%".$_REQUEST['search']."%');";
$result=mysql_query($query,$con);
while($row=mysql_fetch_array($result)){
echo '
  <tr>
    <td><a href="'.$row['page_url'].'">'.$row['page_name'].'</a></td>
<td>'.$row['page_description'].'</td>
  </tr>';
}
?>
</table>
<form action="search.php" method="get">
<center><input type="text" name="search" /><input type="submit" value="Search"  /></center>
</form>

 

Scott.

Link to comment
Share on other sites

Ok,  :)

I did what you said. I made a table in my database called site_info and put alot of my sites urls and page names into it. Then I changed the php to match my database. But I keep getting this error.

 

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /html/search.php on line 308

 

here's my code:

 

<?php

$con=mysql_connect('databasename','password','username');

mysql_select_db('site_info',$con);

$query="SELECT * FROM site_info WHERE page_url,page_names LIKE ('%".$_REQUEST['search']."%');";

$result=mysql_query($query,$con);

while($row=mysql_fetch_array($result)){  This line 308 for me

echo '

  <tr>

    <td><a href="'.$row['page_url'].'">'.$row['page_name'].'</a></td>

 

  </tr>';

}

?>

 

        </table>

<form action="search.php" method="get">

<center><input type="text" name="search" /><input type="submit" value="Search"  /></center>

</form>

 

Thank you for your help.

Link to comment
Share on other sites

Try this

 

<?php
$con = mysql_connect('databasename', 'password', 'username');
mysql_select_db('site_info', $con);

$query = "SELECT * FROM `site_info` WHERE `page_url`, `page_names` LIKE ('%" . $_POST['search'] . "%')";
$result = mysql_query($query, $con) or die('Error: ' . mysql_error()); // Always use error handling
while($row = mysql_fetch_array($result)) {   This line 308 for me
   echo '
    <tr>
      <td><a href="' . $row['page_url'] . '">' . $row['page_name'] . '</a></td>
    </tr>
  </table>';
}

if(isset($_POST['submit'])) {
  echo '
   <form action="' . $_SERVER['PHP_SELF'] . '" method="post">
     <center><input type="text" name="search" /><input type="submit" value="Search"  name="submit" /></center>
   </form>';
}
?>

Link to comment
Share on other sites

Oops that's wrong

 

<?php
$con = mysql_connect('databasename', 'password', 'username');
mysql_select_db('site_info', $con);

if(!isset($_POST['submit'])) {
  echo '
   <form action="' . $_SERVER['PHP_SELF'] . '" method="post">
     <center><input type="text" name="search" /><input type="submit" value="Search"  name="submit" /></center>
   </form>';
} else {
  $query = "SELECT * FROM `site_info` WHERE `page_url`, `page_names` LIKE ('%" . $_POST['search'] . "%')";
  $result = mysql_query($query, $con) or die('Error: ' . mysql_error()); // Always use error handling
  while($row = mysql_fetch_array($result)) {   This line 308 for me
     echo '
      <tr>
        <td><a href="' . $row['page_url'] . '">' . $row['page_name'] . '</a></td>
      </tr>
    </table>';
  }
}
?>

Link to comment
Share on other sites

Ok,

I did that, and it helped but now I get a new error.

 

Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' page_name, LIKE ('%blaster%')' at line 1

 

My database table is set up like this.

 

CREATE TABLE site_info

(

id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,

page_url varchar(100),

page_name varchar(100)

);

 

Thank you again for all your help. ;D

Link to comment
Share on other sites

Sorry typo, i spelled it "names" rather than "name"

 

Use this:

 

<?php
$con = mysql_connect('databasename', 'password', 'username');
mysql_select_db('site_info', $con);

if(!isset($_POST['submit'])) {
  echo '
   <form action="' . $_SERVER['PHP_SELF'] . '" method="post">
     <center><input type="text" name="search" /><input type="submit" value="Search"  name="submit" /></center>
   </form>';
} else {
  $query = "SELECT * FROM `site_info` WHERE `page_url`, `page_name` LIKE ('%" . $_POST['search'] . "%')";
  $result = mysql_query($query, $con) or die('Error: ' . mysql_error()); // Always use error handling
  while($row = mysql_fetch_array($result)) {   This line 308 for me
     echo '
      <tr>
        <td><a href="' . $row['page_url'] . '">' . $row['page_name'] . '</a></td>
      </tr>
    </table>';
  }
}
?>

Link to comment
Share on other sites

<?php
$con = mysql_connect('databasename', 'password', 'username');
mysql_select_db('site_info', $con);

if(!isset($_POST['submit'])) {
  echo '
   <form action="' . $_SERVER['PHP_SELF'] . '" method="post">
     <center><input type="text" name="search" /><input type="submit" value="Search"  name="submit" /></center>
   </form>';
} else {
  $query = "SELECT * FROM `site_info` WHERE `page_url`, `page_name` LIKE '%" . $_POST['search'] . "%'";
  $result = mysql_query($query, $con) or die('Error: ' . mysql_error()); // Always use error handling
  while($row = mysql_fetch_array($result)) {   This line 308 for me
     echo '
      <tr>
        <td><a href="' . $row['page_url'] . '">' . $row['page_name'] . '</a></td>
      </tr>
    </table>';
  }
}
?>

I think it may be the brackets. Try that

 

Link to comment
Share on other sites

<?php
$con = mysql_connect('databasename', 'password', 'username');
mysql_select_db('site_info', $con);

if(!isset($_POST['submit'])) {
  echo '
   <form action="' . $_SERVER['PHP_SELF'] . '" method="post">
     <center><input type="text" name="search" /><input type="submit" value="Search"  name="submit" /></center>
   </form>';
} else {
  $query = "SELECT * FROM `site_info` WHERE `page_url`, `page_name` LIKE '%" . $_POST['search'] . "%'";
  $result = mysql_query($query, $con) or die('Error: ' . mysql_error()); // Always use error handling
  while($row = mysql_fetch_array($result)) {
     echo '
      <tr>
        <td><a href="' . $row['page_url'] . '">' . $row['page_name'] . '</a></td>
      </tr>
    </table>';
  }
}
?>

 

Try that WITHOUT changing a thing, then copy and paste the error here.

Link to comment
Share on other sites

you can't put 2 fields to look in 1 like call

 

$query = "SELECT * FROM `site_info` WHERE `page_url` LIKE '%" . $_POST['search'] . "%' OR `page_name` LIKE '%" . $_POST['search'] . "%'";

 

Also WHERE statements are always seperated by AND, OR not a comma

 

Ray

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.