View Full Version : Problems With PHP Search Script


Joseph Witchard
07-21-2009, 08:24 AM
<?php

/** Coded by: Jeffrey (Joseph Witchard)
** Created on: 07/20/09
** Last modified: 07/21/09
** Purpose: To search for specific
** Rebirth news and display
** it to the user. */

// strip BBCode

require('path_to_bb_function');

// set up the connection

require('path_to_connection_function');

$conn = @connect_function();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" dir="ltr">
<head>
<title>News Search Results -- Ultimate Hogwarts: The Rebirth</title>
<?php include('includes/meta_info.html'); ?>
<link href="/css/general.css" rel="stylesheet" type="text/css"/>
<link href="/css/homepage.css" rel="stylesheet" type="text/css"/>
<link href="/css/main_pages.css" rel="stylesheet" type="text/css"/>
<link href="/css/ticker.css" rel="stylesheet" type="text/css"/>
<link href="/favicon.ico" rel="shortcut icon"/>
<script type="text/javascript" src="/javascripts/scroll.js"></script>
</head>
<body>
<div id="wrapper">
<?php include('path_to_headers'); ?>
<div id="navigation">
<?php include('path_to_navigationl'); ?>
</div>
<div id="right">
<?php include('path_to_news_scroller.'); ?>
<div id="rightAd">
<?php include('path_to_google_adl'); ?>
</div>
</div>
<div align="center" id="center">
<h3>Search Results</h3>
<div id="news">
<?php

if (mysqli_connect_error())
{

// get ready to mail me the error

define('TO', 'josephwitchard@uhrebirth.com');
define('SUBJECT', 'Rebirth Database Connection Error');
$headers = "From: Rebirth Databases <rebirth_databases@uhrebirth.com> \r\n";
$headers .= "Reply-To: Rebirth Databases <rebirth_databases@uhrebirth.com> \r\n";
$message = "There was a connection error on " . DB_NAME . " at uhrebirth.com" . $_SERVER['PHP_SELF'] . ". The error returned was: " . mysqli_connect_error();

// send the message

mail(TO, SUBJECT, $message, $headers);

echo "<p class='warning'>There was an error when connecting to the database. The webmaster has been notified of this error. Please try again later.</p>";
exit;

}

if (array_key_exists('search', $_POST) && !empty($_POST['search']))
{

// set up required and expected fields

$required = array('searching');
$expected = array('searching');

// set up an empty array for missing fields

$missing = array();

// process the post variables

foreach ($_POST as $key => $value)
{

// assign to a temporary variable and strip whitespace if not an array

$temp = is_array($value) ? $value : trim($value);

if (empty($temp) && in_array($key, $required))
{

// add to missing

$missing[] = $key;

}

elseif (in_array($key, $expected))
{

// assign to a variable of the same name

${$key} = $temp;

}

}

// go ahead only if missing is empty

if (empty($missing))
{

// we no longer need missing

unset($missing);

// begin preparing the search term for searching

$searching = htmlentities($searching, ENT_QUOTES);

$searching = $conn->real_escape_string($searching);

$search_length = strlen($searching) + 2;

$searching = str_pad($searching, $search_length, '%', STR_PAD_BOTH);


// start the query

$query1 = "SELECT COUNT(*) FROM posts WHERE post_body LIKE '$searching'";

Joseph Witchard
07-21-2009, 08:32 AM
Sorry for all the double posts, but it's just that long of a script.

*sigh* Where do I begin?

1) I am not able to do an INNER JOIN without breaking the script. Could someone show me how to do that when using LIKE?

2) As you can see from the commented out code, I can't figure out how to see if I've gotten any data out or not. Apparently those two functions don't work on this type of query.

3) post_body is only returned with the search phrase instead of the whole post.

4) author_name is not displaying.

5) For some reason, even though I've got it set to the same divs and CSS as my homepage at www.uhrebirth.com, the news isn't centering like it does on the homepage.

THERE'S SUPPOSED TO BE A POST BETWEEN THIS POST AND THE OPENING POST. I DON'T KNOW IF THIS IS AN ERROR OR NOT, SINCE A MOD WAS ASKED TO APPROVE MY LAST POST.

Joseph Witchard
07-22-2009, 06:40 AM
I don't think the whole post ended up getting posted. But anyway, I'm posting again to show the solution in case someone else has the same trouble I was having.

I was using the following:



$searching // SEARCH STRING

$query = "query stuff WHERE posts.post_body LIKE '%?%'";

$stmt = $conn->prepare($query);

$stmt->bind_param('s', $searching);

No matter what, using that type of query messed it up when you bound the parameters. If you're using a query like the one above and keep getting an error message regarding $stmt->bind_param, try this instead:


$searching // SEARCH STRING

$query = "query stuff WHERE posts.post_body LIKE ?"; // JUST A QUESTION MARK

$stmt = $conn->prepare($query);

$stmt->bind_param('s', $search_string);

// PREPARE THE STRING FOR SEARCHING

$search_string = str_pad($searching, strlen($searching) + 2, '%', STR_PAD_BOTH);

In other other words, add the wildcards after the query has been written.

Lissa
07-23-2009, 05:30 PM
Thanks for posting the solution Joseph :) PHP is something I've never gotten into, but have always wanted to start.

Joseph Witchard
07-23-2009, 09:38 PM
No worries:) And you really should. You can do so much with it. The only thing that tends to worry me about it is when I make a career out of programming/web development, I hear most places want their server-side development done with ASP. No one is a bigger Microsoft fan than I am, but it would be easier on me since I've already worked a lot on PHP.

iGeek
07-23-2009, 10:45 PM
Ew, ASP. :P

I've been with PHP for... four years? I love it.