ok got rid of all error being throw but on and it does not make sense.
$search_result not defined , but it is
Error
An error occurred in script '/home/content/84/8829884/html/test/results.php' on line 301:
<br />Undefined variable: search_result
<br />Date/Time: 11-30-2012 09:42:06
<br /><pre>Array
(
[GLOBALS] => Array
RECURSION
[_POST] => Array
(
)
[_GET] => Array ( [SearchTerms] => rolls [submit] => Search ) [_COOKIE] => Array ( [__utma] => 107246494.1524893141.1353092377.1354224105.1354293727.26 [__utmz] => 107246494.1353095668.2.2.utmcsr=mysite.com|utmccn=(referral)|utmcmd=referral|utmcct=/ [_pk_id_1_67e2] => 22a9ccccd5c8c264.1353340941.17.1354293727.1354224658. [__utmb] => 107246494.1.10.1354293727 [__utmc] => 107246494 [_pk_ses_1_67e2] => * ) [_FILES] => Array ( ) [live] => 1 [email] => error@my.com [errors] => Array ( [0] => There was a database error. Please contact the Webmaster of this site via our contact page if this problem persists, ) [feedback] => Array ( ) [s] => rolls [word_check] => Array ( [0] => bread [1] => white [2] => wheat [3] => rye ) [limit] => 10 [host] => mysite.com [dbname] => nfacts [dsn] => mysql:dbname=nfacts;host=mysite.com [db] => PDO Object ( ) [count_stmt] => PDOStatement Object ( [queryString] => SELECT COUNT(*) AS rowcount FROM products WHERE keywords like :search ) [stmt] => PDOStatement Object ( [queryString] => SELECT link, keywords FROM products WHERE keywords like :search ORDER BY keywords LIMIT :offset, :count ) [count_params] => Array ( [search] => rolls ) [params] => Array ( [search] => rolls [offset] => 10 [count] => 20 ) [err] => There was a database error. Please contact the Webmaster of this site via our contact page if this problem persists,
)
</pre>
<br />
<?php
# I like collecting errors throughout the page to provide feedback
# Same with other feedback, although I keep them separate
$errors = array();
$feedback = array();
/* First off, I'd try to get as many checks as possible into one single
* For example, instead of checking both "no input" and "less than 3 characters" and informing
* user of each one separately, just check string length and inform user: has to be at least 3 chars
*
* Also note that $_GET['SearchTerms'] will always be set if the form was posted properly,
* even if the field was left empty. Thus, in the original case of !isset($_GET['SearchTerm'])
* it would have been better with !empty($_GET['SearchTerm'])
*/
# To make sure the array element is set, using the ternary operator ?:
# Works like this
# (is this value true) ? (then return this value) : (else return this value)
$_GET['SearchTerms'] = !isset($_GET['SearchTerms']) ? '' : trim($_GET['SearchTerms']);
# Now that we've trimmed the search, check for string length using mb_strlen (you are using utf-8 aren't you?)
if (mb_strlen($_GET['SearchTerms']) < 2) {
$feedback[] = "<p>Search terms must be longer than 2 characters. Please try your search again. ";
# Generally, you don't want to use exit. Rather construct the code so that it can output the entire page
# normally. The difference will be wether they get search results, "nothing found" or one or more errors
# in the content part of the page
}
# This will run for pretty much the rest of the file, no matter how long it might become
# That is, this section deals with having a valid search
else {
$s = $_GET['SearchTerms'];
// Checking SearchTerm for disallowed search terms
# Easier way to check for these values in a more condensed form
$word_check = array(
'bread',
'white',
'wheat',
'rye'
);
if (in_array($s, $word_check)) {
$feedback[] = "<p>Your search term is too broad. Please enter a more specific search term. ";
}
$limit = 10; // rows to return
// Build SQL Query
# Connecting to the DB using PDO
$host = 'mysite.com';
$dbname = 'nfacts';
$dsn = "mysql:dbname=$dbname;host=$host";
$db = new PDO($dsn, 'user', 'password');
$count_stmt = $db->prepare('SELECT COUNT(*) AS rowcount FROM products WHERE keywords like :search');
# The query fetching the product links will also need "LIMIT offset, count"
$stmt= $db->prepare('SELECT link, keywords FROM products WHERE keywords like :search ORDER BY keywords LIMIT :offset, :count');
# search here will replace :search in the query
$count_params = array(
'search' => $s
);
# I put in fixed values for offset and count, leaving for you to deal with the pagination :)
$params = array(
'search' => $s,
'offset' => 10,
'count' => 20
);
# Then you use matching name, other_name (with or without :) as array keys to pass the data <----- Fails Here
if (!$stmt->execute($params)) {
$errors[] = 'There was a database error. Please contact the Webmaster of this site via our contact page if this problem persists,';
} else {
# This returns all selected rows into an associative array
$search_result = $stmt->fetchAll(PDO::FETCH_ASSOC); <--------------------------------------- $search_result is defined here? I dont't get it
$count_stmt->execute($params);
# Whereas fetchAll fetches all rows at once, fetch fetches one row at the time
# and here we only have one row anyway
$row = $count_stmt->fetch(PDO::FETCH_ASSOC);
$rowcount = $row['rowcount'];
# Add search result number independently if that was 0 or any other number
$feedback[] = sprintf('Your search found %d results', $numrows);
if ($numrows == 0) {
$feedback[] = sprintf('Sorry, your search: "%s" returned zero results. Please try again. If you need help please <a href="%s">click here.</a></p>', $s, BASE . HELPDIR . 'search-help.php')
}
}
}
# But before you get to actual content, let's give the user errors and feedback, if any
# Which ought to start with errors
foreach ($errors as $err) {
echo $err . "<br />"; // assuming HTML output, otherwise substitute a newline character for whatever OS you are using
}
foreach ($feedback as $f) {
echo $f . "<br />"; // assuming HTML output, otherwise substitute a newline character for whatever OS you are using
}
# and all that remains to do is output the search results
// now you can display the results returned
while ($result = $search_result) {
echo "$row[link] <br>";
}
# followed by pagination
# echo "here..." ;