I'm following this tutorial, which includes:
include('db.php');
$db = new db();
In the tutorial does not show how new db() is created or the db.php file, however I do on my system already have config.local.php which contains the following:
$config['db_host'] = 'localhost';
$config['db_name'] = 'database_name';
$config['db_user'] = 'database_user';
$config['db_password'] = 'c@W)ukmd[0bm';
$config['database_backend'] = 'mysqli';
I'm really new to this. How can I use my current config file? I've already included "config.local.php" but need to of course use my config style and do the connection.
PHP (adjusted)
<?php
//if we got something through $_POST
if (isset($_POST['search'])) {
// here you would normally include some database connection
include('config.local.php');
$db = new db();
// never trust what user wrote! We must ALWAYS sanitize user input
$word = mysql_real_escape_string($_POST['postcode_q']);
$word = htmlentities($word);
// build your search query to the database
$sql = "SELECT description FROM cscart_postcode_location_descriptions WHERE cscart_postcode_location_descriptions LIKE '%" . $word . "%' ORDER BY cscart_postcode_location_descriptions LIMIT 1";
// get results
$row = $db->select_list($sql);
if(count($row)) {
$end_result = '';
foreach($row as $r) {
$result = $r['cscart_postcode_location_descriptions'];
// we will use this to bold the search word in result
$bold = '<span class="found">' . $word . '</span>';
$end_result .= '<li>' . str_ireplace($word, $bold, $result) . '</li>';
}
echo $end_result;
} else {
echo '<li>No results found</li>';
}
}
?>
MySQLi Attempt (needs review)
<?php
//if we got something through $_POST
if (isset($_POST['postcode_locator_search'])) {
// here you would normally include some database connection
include('config.local.php');
//Open a new connection to the MySQL server
$mysqli = new mysqli($config['db_host'],$config['db_user'],$config['db_password'],$config['db_name']);
//Output any connection error
if ($mysqli->connect_error) {
die('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error);
}
// never trust what user wrote! We must ALWAYS sanitize user input
$postcode_q = mysql_real_escape_string($_POST['postcode_q']);
$postcode_q = htmlentities($postcode_q);
//chained PHP functions
$sql = $mysqli->query("SELECT description FROM cscart_postcode_location_descriptions WHERE cscart_postcode_location_descriptions LIKE '%" . $postcode_q . "%' ORDER BY cscart_postcode_location_descriptions LIMIT 1");
if(count($sql)) {
$end_result = '';
foreach($sql as $r) {
$result = $r['cscart_postcode_location_descriptions'];
// we will use this to bold the search word in result
$bold = '<span class="found">' . $word . '</span>';
$end_result .= '<li>' . str_ireplace($word, $bold, $result) . '</li>';
}
echo $end_result;
} else {
echo '<li>No results found</li>';
}
$mysqli->close();
}
?>
jQuery I've done:
$(function() {
$(".postcode_locator_form .ty-btn-go").click(function() {
// getting the value that user typed
var searchString = $("#postcode_locator_search").val();
// forming the queryString
var data = 'postcode_locator_search='+ searchString;
// if searchString is not empty
if(searchString) {
// ajax call
$.ajax({
type: "POST",
url: "search_postcode.php",
data: data,
beforeSend: function(html) { // this happens before actual call
$("#results").html('');
$(".searched-postcode").html(searchString);
},
success: function(html){ // this happens after we get results
$("#results").show();
$("#results").append(html);
},
error: function(html){
$("#results").show();
$("#results").append(html);
}
});
}
return false;
});
});