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
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();
}

Link to comment
Share on other sites

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();
  }
}

Link to comment
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();
}

 

 

 

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 />");
  }
}

?>

Link to comment
Share on other sites

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 ?

Link to comment
Share on other sites

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 />");
  }
}

?>

Link to comment
Share on other sites

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

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.