Jump to content

MYSQL search not working


jamesxg1

Recommended Posts

Iv made this script but it doesnt show any at all results heres the code

 

<?php session_start(); 

require("../db/db.php"); //include database file
require("../db/config.php"); //include configuration file
require("../db/util.php");

isloggedin();
accessneeded("C");

?>
<?
$search=$_POST["search"];

//get the mysql and store them in $result
//change whatevertable to the mysql table you're using
//change whatevercolumn to the column in the table you want to search
$result = mysql_query("SELECT * FROM users WHERE username, lastname, firstname, email LIKE '%$search%'");

//grab all the content
while($row=@mysql_fetch_array($result))
{	
   //the format is $variable = $r["nameofmysqlcolumn"];
   //modify these to match your mysql table columns
  
   $lastname=$row["lastname"];
   $firstname=$row["firstname"];
   $username=$row["username"];
   $email=$row["email"];
   
   //display the row
   echo "$firstname <br> $lastname <br> $username <br> $email";
}
?>

 

 

This is the form for it

 

<form method="post" action="search.php">
<input type="text" name="search" size=25 maxlength=25>
<input type="Submit" name="Submit" value="Submit">
</form>

Link to comment
https://forums.phpfreaks.com/topic/142866-mysql-search-not-working/
Share on other sites

You never actually check for any results.

 

if ($result = mysql_query("SELECT * FROM users WHERE username, lastname, firstname, email LIKE '%$search%'")) {
  if (mysql_num_rows($result)) {
    //grab all the content
    while($row=@mysql_fetch_array($result))
    {	
       //the format is $variable = $r["nameofmysqlcolumn"];
       //modify these to match your mysql table columns
  
       $lastname=$row["lastname"];
       $firstname=$row["firstname"];
       $username=$row["username"];
       $email=$row["email"];
   
       //display the row
       echo "$firstname <br> $lastname <br> $username <br> $email";
    }
  } else {
    echo "No results found";
  }
} else {
  echo "Query failed<br />" mysql_error();
}

Never even noticed it before but your query is all sorts of messed up. Try...

 

if (isset($_POST['Submit'])) {
  $search = mysql_real_escape_string($_POST["search"]);

  $sql = "SELECT firstname, lastname, username, email FROM users WHERE username LIKE '%$search%' || lastname LIKE '%$search%' || firstname LIKE '%$search%' || email LIKE '%$search%'";

  if ($result = mysql_query($sql)) {
    if (mysql_num_rows($result)) {
      //grab all the content
      while($row=@mysql_fetch_array($result))
      {	
         //the format is $variable = $r["nameofmysqlcolumn"];
         //modify these to match your mysql table columns
  
         $lastname=$row["lastname"];
         $firstname=$row["firstname"];
         $username=$row["username"];
         $email=$row["email"];
   
         //display the row
         echo "$firstname <br> $lastname <br> $username <br> $email";
      }
    } else {
      echo "No results found";
    }
  } else {
    echo "Query failed<br />" mysql_error();
  }
}

You never actually check for any results.

 

if ($result = mysql_query("SELECT * FROM users WHERE username, lastname, firstname, email LIKE '%$search%'")) {
  if (mysql_num_rows($result)) {
    //grab all the content
    while($row=@mysql_fetch_array($result))
    {	
       //the format is $variable = $r["nameofmysqlcolumn"];
       //modify these to match your mysql table columns
  
       $lastname=$row["lastname"];
       $firstname=$row["firstname"];
       $username=$row["username"];
       $email=$row["email"];
   
       //display the row
       echo "$firstname <br> $lastname <br> $username <br> $email";
    }
  } else {
    echo "No results found";
  }
} else {
  echo "Query failed<br />" mysql_error();
}

 

 

 

Ok with this code it still have no results.

 

<?php session_start(); 

require("../db/db.php"); //include database file
require("../db/config.php"); //include configuration file
require("../db/util.php");

isloggedin();
accessneeded("C");

?>
<?
$search=$_POST["search"];

if (isset($_POST['Submit'])) {
  $search = mysql_real_escape_string($_POST["search"]);

  $sql = "SELECT firstname, lastname, username, email FROM users WHERE username LIKE '%$search%' || lastname LIKE '%$search%' || firstname LIKE '%$search%' || email LIKE '%$search%'";

  if ($result = mysql_query($sql)) {
    if (mysql_num_rows($result)) {
      //grab all the content
      while($row=@mysql_fetch_array($result))
      {	
         //the format is $variable = $r["nameofmysqlcolumn"];
         //modify these to match your mysql table columns
  
         $lastname=$row["lastname"];
         $firstname=$row["firstname"];
         $username=$row["username"];
         $email=$row["email"];
   
         //display the row
         echo "$firstname <br> $lastname <br> $username <br> $email";
      }
    } else {
      echo "No results found";
    }
  } else {
    echo ("Query failed<br />");
  }
}

?>

Use <?php not <? and what message is being displayed if any?

 

Hiya,

 

Iv used <?php and still nothing.... i just get a empy screen :S,

 

Could it be something to do with my db.php file seeing as this is my Database connection script and i do have ott security on it ?

Put this at the top of your script.

 

<?php error_reporting(E_ALL) ; ini_set('display_errors','1'); ?>

 

 

i now get

 

Notice: Undefined index: search in D:\xampp\htdocs\main\search.php on line 17

 

 

 

ok now i have put single quotes on the 17th lines i get a error on line 13

 

<?php session_start(); 

require("../db/db.php"); //include database file
require("../db/config.php"); //include configuration file
require("../db/util.php");

isloggedin();
accessneeded("C");

?>
<?php error_reporting(E_ALL) ; ini_set('display_errors','1'); ?>
<?php
$search=$_POST["search"];

if (isset($_POST['Submit'])) {
  $search = mysql_real_escape_string($_POST["search"]);
  $sql = 'SELECT firstname, lastname, username, email FROM users WHERE username LIKE '%$search%' || lastname LIKE '%$search%' || firstname LIKE '%$search%' || email LIKE '%$search%'';

  if ($result = mysql_query($sql)) {
    if (mysql_num_rows($result)) {
      //grab all the content
      while($row=@mysql_fetch_array($result))
      {	
         //the format is $variable = $r["nameofmysqlcolumn"];
         //modify these to match your mysql table columns
  
         $lastname=$row["lastname"];
         $firstname=$row["firstname"];
         $username=$row["username"];
         $email=$row["email"];
   
         //display the row
         echo "$firstname <br> $lastname <br> $username <br> $email";
      }
    } else {
      echo "No results found";
    }
  } else {
    echo ("Query failed<br />");
  }
}

?>

This meens your form isn't submitting properly. Is that your exact form and is this script called search.php?

 

 

 

Anyone any ideas to why it isnt working?

 

 

yes every script i have posted on this thread is exact to what i have in my dir...

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.