Jump to content

Recommended Posts

Hi,

I have written a fairly simple search script for a website I am working on, the search box works great, it shows the results and if it cannot find the text that is entered it redirects to a page telling the user that it cannot find information.

Now im stuck with the code to write incase the user doesn't enter anything into the search box.

This is my code for the current redirect: -

[CODE]
<?
$database="database"; 
mysql_connect ("localhost", "user", "pass");
@mysql_select_db($database) or die( "Unable to select database");
$sql = "select emulator, version, os, platform, details from windows_atari2600 where emulator LIKE '%".mysql_real_escape_string($_POST["emusearch"])."%' OR platform LIKE '%".mysql_real_escape_string($_POST["emusearch"])."%' order by platform asc, emulator asc";
$result = mysql_query($sql);
$num_rows = mysql_num_rows($result);
if($num_rows < 1)
{
    echo "<meta http-equiv='refresh' content='0; URL=/searchnoresults.php'>";
}
else
if(

  {
include ('http://www.emulators.cc/includes/head.php');
?>
[/CODE]

Im planning on putting the redirect code for when nothing is entered into the last 'If(' part.

Any help would be appreciated.

Thanks in advance.  :)
You actually would want to check for the empty string first. That way, you're not performing the search when you're going to end up redirecting them anyway:
[code]
<?php
if (empty($_POST['emusearch'])) {
  // redirect to page of your choice
  header("Location: index.php");
  exit();
} else {
  // run your search here
}
?>
[/code]
Thanks that worked.

Is it possible to include a file at the top of a page that contains your MySQL connection information?

I tried this and while the file works and connects to mysql if I type it directly into my browser, it doesnt work when the file is included from my other pages?
Actually, it is quite common to have a single database connection document that is included in all your application pages. It would be something like this:

inc.dbConn.php:
[code]
<?php
$HOST = 'localhost';
$USER = 'myUser';
$PASS = 'myPass';
$NAME = 'dbName';

$conn = mysql_connect($HOST, $USER, $PASS);
if (!$conn) {
  // database connection failed!
  die("Error connecting to database!");
}

mysql_select_db($NAME, $conn);
?>
[/code]

Then, in all your normal files, just require that database file:
[code]
<?php
require_once('inc.dbConn.php');

// Page content goes here
?>
[/code]

Hope that helps!
[quote author=87dave87 link=topic=114816.msg467344#msg467344 date=1163441415]
The code you just used does the same as what I was using before, it flashes this for a split second:

Warning: mysql_real_escape_string(): Access denied for user nobody@localhost using password: no
[/quote]

The error you are showing is actually not for the connection string, but rather for when you are calling mysql_real_escape_string(). Is this function being called [b]before[/b] you are making your database connection? If so, there is your problem. Your included files should be the first thing you do on your pages in most cases.
Okay, The way I have this set up is: -

First thing in code, include for dbconnect.php (with connect info)

Then if this is the search page I have my $sql code for selecting tables from database and comparing the string data, which is in seperate php tags.

Then further down I have sql code again where it is displaying the rows.

It appears to be connecting on my front page and sub page.

code: -

[code]
<?php
require_once('http://www.emulators.cc/includes/dbconnect.php');
?>

<?php
$sql = "select emulator, version, os, platform, details from windows_atari2600 where emulator LIKE '%".mysql_real_escape_string($_POST["emusearch"])."%' OR platform LIKE '%".mysql_real_escape_string($_POST["emusearch"])."%' order by platform asc, emulator asc";

if (empty($_POST['emusearch'])) {
  echo "<meta http-equiv='refresh' content='0; URL=/searchnoresults.php'>";
  exit();
} else {

$result = mysql_query($sql);
$num_rows = mysql_num_rows($result);
if($num_rows < 1)
{
    echo "<meta http-equiv='refresh' content='0; URL=/searchnoresults.php'>";
exit();
}
else 
include ('http://www.emulators.cc/includes/head.php');
?>

<table width="600" border="0">
  <tr>
    <td>
    <table width=600 border=0 cellpadding=0 cellspacing=0>
    <tr>
        <td>
            <img src="/images/searchresults.jpg" width=300 height=30 alt="Search Results"></td>
        <td>
            <table border="0" cellpadding="0" cellspacing="0" width="290" height="30" background="/images/barmid.jpg">
              <tr>
                <td>
                <div align="right">
<?php
echo "<p class='footer'>";
echo $num_rows;
if ($num_rows == "1")
{
echo " emulator found</p>";
}
else{
echo " emulators found</p>";}
?>
                </div></td>
              </tr>
            </table>
        </td>
        <td>
            <img src="/images/barend.jpg" width=10 height=30 alt="Search Results"></td>
    </tr>
</table>
    </td>
  </tr>
  <tr>
    <td><table width="100%" border="0" cellpadding="0" cellspacing="0">
        <tr class="tablehead">
          <td width="240">Emulator</td>
          <td width="120">Version</td>
          <td width="120">OS</td>
          <td width="120">Platform</td>
          <td width="120">Details</td>
        </tr>
<?php
if (isset($_POST['emusearch']) && strlen(trim($_POST['emusearch'])) > 0){
  $sql = "select emulator, version, os, platform, details from windows_atari2600 where emulator LIKE '%".mysql_real_escape_string($_POST["emusearch"])."%' OR platform LIKE '%".mysql_real_escape_string($_POST["emusearch"])."%' order by platform asc, emulator asc";

  if(!$rs = mysql_query($sql)){
      echo "Error<br>".mysql_error()."<br>".$sql;
      exit();
  }


  else{
      $num_rows = mysql_num_rows($rs);
      while ($get_info = mysql_fetch_row($rs)) {
        echo "<tr>";
        foreach ($get_info as $field){
            echo "<td>$field</td>\n";
        }
            echo "</tr>\n";
      }
  }
}
?>
      </table>
</table>

<?php
include ('http://www.emulators.cc/includes/search.php');
include ('http://www.emulators.cc/includes/foot.php');

?>
[/code]

maybe this is occuring because $sql is in there twice?
When you include a file, especially PHP files
you don't use the URLs

give a relative location
like
require_once "../includes/dbconnect.php";


reason for that is to keep people from stealing other people's scripts
[quote author=zanus link=topic=114816.msg467374#msg467374 date=1163444154]
When you include a file, especially PHP files
you don't use the URLs

give a relative location
like
require_once "../includes/dbconnect.php";


reason for that is to keep people from stealing other people's scripts
[/quote]

Further reason for being careful how you include files:
[quote author=PHP Manual]
Files for including are first looked in include_path relative to the current working directory and then in include_path relative to the directory of current script. E.g. if your include_path is ., current working directory is /www/, you included include/a.php and there is include "b.php"  in that file, b.php is first looked in /www/  and then in /www/include/. If filename begins with ./ or ../, it is looked only in include_path relative to the current working directory.
[/quote]
The include works, ive got another error before redirect now when I try typing something thats not found 'gdsogsgoisog' or something that is in the database 'Atari'


Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/irrm3a/public_html/search.php on line 14


I cannot see anything wrong with this: -

[code]
if (empty($_POST['emusearch'])) {
  echo "<meta http-equiv='refresh' content='0; URL=/searchnoresults.php'>";
  exit();
} else {

$result = mysql_query($sql);
$num_rows = mysql_num_rows($result);
if($num_rows < 1)
{
    echo "<meta http-equiv='refresh' content='0; URL=/searchnoresults.php'>";
exit();
}
else 
include ('http://www.emulators.cc/includes/head.php');
[/code]
[quote author=87dave87 link=topic=114816.msg467406#msg467406 date=1163446878]
I cannot see anything wrong with this: -

[code]
if (empty($_POST['emusearch'])) {
  echo "<meta http-equiv='refresh' content='0; URL=/searchnoresults.php'>";
  exit();
} else {

$result = mysql_query($sql);
$num_rows = mysql_num_rows($result);
if($num_rows < 1)
{
    echo "<meta http-equiv='refresh' content='0; URL=/searchnoresults.php'>";
exit();
}
else 
include ('http://www.emulators.cc/includes/head.php');
[/code]
[/quote]

That error typically means that the actual query itself has issues. Try doing some error checking on it for now and see what it returns:
[code]
<?php
$result = mysql_query($sql) or die("Error encountered: " . mysql_error());
?>
[/code]

What does that return now?
[quote author=87dave87 link=topic=114816.msg467419#msg467419 date=1163447830]
Error encountered: No database selected
[/quote]

There's your problem. You're not selecting a database to connect to in your dbconnect.php file. If you have a mysql_select_db() function called in that file, you ought to try some error reporting on it as well and find out [b]why[/b] it's not selecting properly.
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.