Jump to content

php echoing out search results not working


silverglade

Recommended Posts

Hi, I have been trying for 2 days to get this to work. I am trying to put in a list of search keywords in the form, run a LIKE query on the database, and echo out all of the matching terms. Please if someone could please show me how to echo out this information I would be very grateful. Thank you.

 

<?php 
$host		= "";
$database 	= "";
$username 	= "";
$password 	= "";
$tbl_name   = "users";
$conn = mysql_connect($host, $username, $password) or die("Could not connect: " . mysql_error());
mysql_select_db($database);



include('bouncer.php');




$currentUser = $_SESSION['myusername'];

if(isset($_POST['submit'])) 

{				 


$first = mysql_real_escape_string( $_POST['first']);
$last = mysql_real_escape_string( $_POST['last']);
$dob = mysql_real_escape_string( $_POST['dob']);
$gender = mysql_real_escape_string( $_POST['gender']);
$ethnic = mysql_real_escape_string( $_POST['ethnic']);
$country = mysql_real_escape_string( $_POST['country']);
$state = mysql_real_escape_string( $_POST['state']);
$town = mysql_real_escape_string( $_POST['town']);
$zip = mysql_real_escape_string( $_POST['zip']);
$email = mysql_real_escape_string( $_POST['email']);
$hobbies = mysql_real_escape_string( $_POST['hobbies']);
$job = mysql_real_escape_string( $_POST['job']);
$business = mysql_real_escape_string( $_POST['business']);
$religion = mysql_real_escape_string( $_POST['religion']);
$social = mysql_real_escape_string( $_POST['social']);
$political = mysql_real_escape_string( $_POST['political']);
$affiliations = mysql_real_escape_string( $_POST['affiliations']);
$buying = mysql_real_escape_string( $_POST['buying']);
$selling = mysql_real_escape_string( $_POST['selling']);
$likes = mysql_real_escape_string( $_POST['likes']);
$dislikes = mysql_real_escape_string( $_POST['dislikes']);
$links = mysql_real_escape_string( $_POST['links']); 




 	//THE SEARCH FUNCTION
$result = mysql_query ( "SELECT * FROM users WHERE dob  LIKE '%$dob%' OR gender LIKE '%$gender%' OR race  LIKE 	'%$ethnic%' OR job  LIKE '%$job%' OR country  LIKE '%$country%' OR state  LIKE '%$state%' OR town  LIKE '%$town%'  OR zip  LIKE'%$zip%' OR hobbies  LIKE'%hobbies%' OR business  LIKE '%$business%' OR religion  LIKE'%$religion%' OR social_groups  LIKE '%$social%' OR political_groups  LIKE'%$political%' OR other_affiliations LIKE ' %$affiliations%' OR  buying  LIKE '%$buying%' OR selling  LIKE '%$selling%' OR links  LIKE '%$links%' OR  likes  LIKE '%$likes%' OR dislikes  LIKE'%$dislikes%'");  			 



if (!$result) {
    echo 'Could not run query: ' . mysql_error();
    exit;
}
$row = mysql_fetch_row($result);

echo $row[0]; 
echo $row[1]; // I HAVE NO IDEA WHERE TO GO FROM HERE.


}
?>
<html>
<body>
<p>Login Successful</p>
<p> </p>

<form action="login_success6.php" method="post">
  <p>
    <input type="text" name="first" size="20" /> 
  First name<br />
    <input type="text" name="last" size="20" />
    Last name<br />
    <input name="dob" type="text" size="20" id="dob" />
    Date of Birth<br />
    <input type="text" name="gender" size="20" id="gender" />
  Gender <br />
  <input type="text" name="ethnic" size="20" id="ethnic" />
  Ethnicity
  <br />
  <input type="text" name="country" size="20" id="country" />
  Country<br />
  <input type="text" name="state" size="20" id="state" />
  State<br />
    <input type="text" name="town" size="20" id="town" />
    Town<br />
<input type="text" name="zip" size="20" id="zip" />
  Zip Code<br />
  <br />
    <input type="text" name="email" size="40" id="email" />
    Email<br />
<textarea name="job" cols="40" id="job"></textarea>
  Job<br />
    <textarea name="business" cols="40" id="business"></textarea>
    Business<br />
    <input type="text" name="religion" size="60" id="religion" />
  Religion</p>
  <p><br />
    <textarea name="social" cols="100" id="social"></textarea>
    Social Groups<br />
    <textarea name="political" cols="100" id="political"></textarea>
    Political groups<br />
    <textarea name="affiliations" cols="100" id="affiliations"></textarea>
    Other Affiliations<br />
    <textarea name="buying" cols="100" id="buying"></textarea>
    Items I am buying<br />
    <textarea name="selling" cols="100" id="selling"></textarea>
    Items I am selling<br />
    <textarea name="likes" cols="100" id="likes"></textarea>
    My likes <br />
    <textarea name="dislikes" cols="100" id="dislikes"></textarea>
    My dislikes <br />
    <textarea name="links" cols="100" id="links"></textarea>
    My links <br />
    <input type="submit" name="submit" value="Store in database and search" />
    <input type="reset" value="Reset fields" />
  </p>
  </p>
</form> 

</body>
</html>

Link to comment
Share on other sites

You're trying to use a query result resource as a query string, which needless to say, won't work so well. Remove the query execution and just assign the value of the query string to $sql, then execute the query using the $sql variable.

 

$sql = "SELECT * FROM users WHERE 
dob  LIKE '%$dob%' 
OR gender LIKE '%$gender%' 
OR race  LIKE '%$ethnic%' 
OR job  LIKE '%$job%' 
OR country  LIKE '%$country%' 
OR state  LIKE '%$state%' 
OR town  LIKE '%$town%'  
OR zip  LIKE '%$zip%' 
OR hobbies  LIKE '%$hobbies%' 
OR business  LIKE '%$business%' 
OR religion LIKE '%$religion%' 
OR social_groups  LIKE '%$social%' 
OR political_groups  LIKE '%$political%' 
OR other_affiliations LIKE '%$affiliations%' 
OR  buying  LIKE '%$buying%' 
OR selling  LIKE '%$selling%' 
OR links  LIKE '%$links%' 
OR likes  LIKE '%$likes%' 
OR dislikes  LIKE '%$dislikes%'";

$result = mysql_query($sql) or die( "<br>Query: $sql<br>Failed with error: " . mysql_error() );

 

EDIT: Couldn't hurt to organize it a bit with some line breaks either . . .

Link to comment
Share on other sites

thank you . That was in my original version I accidentally deleted it because I am on like version 999 now. LMAO

 

I am getting an error now in the connection. I get this.

Could not successfully run query (Resource id #2) from DB: 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 'Resource id #2' at line 1

 

and here is my code, I am pretty sure it is wrong,

 

 
ini_set ("display_errors", "1");
error_reporting(E_ALL);

$host		= "";
$database 	= "";//this info is all correct I checked
$username 	= "";
$password 	= "";
$tbl_name   = "users";
$con=mysql_connect($host, $username, $password) or die("Could not connect: " . mysql_error());


$db_selected = mysql_select_db($database, $con);

if (!$db_selected)
  {
  die ("Can\'t use test_db : " . mysql_error());
  }

Link to comment
Share on other sites

I am starting to think it is my hosting provider. check this out. it looks perfect and all the info is correct but I get the error below.

 

 

error: failed to connect to database

Query: Resource id #2

Failed with 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 'Resource id #2' at line 1




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

$host        = "brendansite1.startlogicmysql.com";
$database     = "photo_artists";
$username     = "username"; //i took out the user and pass for security.
$password     = "password";
$tbl_name   = "users";


$connection = mysql_connect("brendansite1.startlogicmysql.com","username","password");
if($connection) {
    $db = mysql_select_db("photo_artists");
    if(!$db)
    {
    echo "Failed to select db";
    } else {
    echo "failed to connect to database";
    }
}
    

Link to comment
Share on other sites

man, now all I get is my form showing which was nice. but i get this.

 

 

failed to connect to database

 

LOL.

 

The file is the correct file, and it is in the correct location that I upload it to via fireftp. in public_html/website/ directory.

 

I am still using the code from the above post.

Link to comment
Share on other sites

The last logic you posted is nonsense. You will get the "failed to connect to database" message when you have connected to the database server and when you successfully selected the database. Why are you randomly changing the logic in your code. The logic in the first post in this thread would have told you if and why the database connection failed.

 

The query is actually succeeding, but the logic you have around the query statement is still not what Pikachu2000 already told you how to fix.

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.