Hey guys, sorry be reading up on a few things, got most of the issues fixed I think. Only is error that is coming up is this
Error displayed is this: added comments so you know that message is from two different outputs
Connected to database <--- Connection Good, This message is from Connection file script
There was a database error, (!$stmt->execute($params)) did not execute. Please contact the Webmaster of this site via our contact page if this problem persists
-------- From results.php page. page We are working on
if (!$stmt->execute($params)) {
$errors[] = 'There was a database error, (!$stmt->execute($params)) did not execute. Please contact the Webmaster of this site via our contact page if this problem persists';
I tested connection by running connection file nfacts.php by itself and seems to connect based on message displayed "Connected To Database".
[/code]
Here is the results.php page that is having the problem Full reworked code
#
#Script Ver 13 12/3/12
#
#
# I like collecting errors throughout the page to provide feedback
# Same with other feedback, although I keep them separate
$errors = array();
$feedback = array();
$search_result = 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) {
$errors[] = "<p>Search terms must be longer than 2 characters. Please try your search again. ";
} 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
include('includes/connections/nfacts.php');
// Display message if successfully connect, otherwise retains and outputs the potential error
try {
$conn = new PDO("mysql:host=$hostdb; dbname=$namedb", $userdb, $passdb);
echo 'Connected to database';
}
catch(PDOException $e) {
echo $e->getMessage();
}
$count_stmt = $conn->prepare('SELECT COUNT(*) AS rowcount FROM products WHERE keywords like :search');
# The query fetching the product links will also need "LIMIT offset, count"
$stmt = $conn->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
if (!$stmt->execute($params)) {
$errors[] = 'There was a database error, (!$stmt->execute($params)) did not execute. 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);
$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);
echo 'Database query passed however isset($search-result) was empty. Please look at that section of code';
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 />";
}
foreach ($feedback as $f) {
echo $f . "<br />";
}
// now you can display the results returned
foreach($search_result as $row) {
echo $row['link']. '<br />';
}
# followed by pagination
# echo "here..." ;
I know issue is here but I am missing it
include('includes/connections/nfacts.php');
$count_stmt = $conn->prepare('SELECT COUNT(*) AS rowcount FROM products WHERE keywords like :search');
# The query fetching the product links will also need "LIMIT offset, count"
$stmt = $conn->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
);
And Connection File
// Connection data (server_address, database, username, password)
$hostdb = 'mysite.com';
$namedb = 'mydatabase';
$userdb = 'myusername';
$passdb = 'mypassword';
// Display message if successfully connect, otherwise retains and outputs the potential error
try {
$conn = new PDO("mysql:host=$hostdb; dbname=$namedb", $userdb, $passdb);
echo 'Connected to database';
}
catch(PDOException $e) {
echo $e->getMessage();
}