0

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;
    });
});
halfer
  • 19,824
  • 17
  • 99
  • 186
James
  • 1,668
  • 19
  • 50
  • 3
    If you can, you should [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) [statements](http://php.net/manual/en/pdo.prepared-statements.php) instead, and consider using PDO, [it's really not hard](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Aug 18 '15 at 21:53
  • 2
    stop,find a new tutorial, that one uses a depreciated mysql api. better to start right –  Aug 18 '15 at 21:53
  • I would rather use mysqli by all means! does anyone know of a tutorial for what i need? as need to get this sorted quickly. Need to simply search 1 field in a database and return if a result was found or else was not found. Im pretty new to PHP / jQuery so any help would be great guys! Thank you! – James Aug 18 '15 at 22:01
  • 2
    The PHP manual has enough examples to get you started – rjdown Aug 18 '15 at 22:02
  • Maybe but does it do the jquery ajax as well? sorry ive just got like 40 mins to get this done and into production thats all. already 11pm :( – James Aug 18 '15 at 22:04
  • Ive edited with something ive tried to put together.... do you see any issues? im sure there are some but im trying :) – James Aug 18 '15 at 22:21
  • The code, looks ok, aside from mysql_* as you know, but what is the error exactly? Also, you might want to remove that password in your example, looks like it might be the real one? – Michael Hommé Aug 18 '15 at 22:21
  • with the new mysqli one i added to question, i do a search and returns access denied.... have i do the calls coorectly to get from the config file... the connection details part? – James Aug 18 '15 at 22:24
  • Ok, what's inside db.php? – Michael Hommé Aug 18 '15 at 22:24
  • I added jQuery as well, which seems to work i think... as its coming back as a response on the #results dynamically. @mikehomme im not using db.php but my own one which is in question... $config['db_host'] etc... but not sure if im calling it right as might explain "access denied" – James Aug 18 '15 at 22:27
  • With your new MySQLi Attempt are you still getting access denied? – Michael Hommé Aug 18 '15 at 22:27
  • Your MySQLi version, the way it's coded, should work, as long as your username and password are in fact correct and you're actually connected. – Michael Hommé Aug 18 '15 at 22:30
  • Yep getting access denied, and details are correct as its the config file thats used on the main ecommerce site which runs fine so im thinking maybe im setting the connection variables wrong? like... new mysqli($config['db_host'],$config['db_user'] is that the right way to get them from whats set in the config.local.php file? – James Aug 18 '15 at 22:31
  • I tried to just place the database details in manually but just get back access denied still...... got 20 mins to deploy this :( – James Aug 18 '15 at 22:41
  • you are mixing mysql and mysqli in your revision, particularly with mysql_real_escape_string – nomistic Aug 18 '15 at 23:04
  • You are replacing 'database_user' and 'database_name' with a user that actually exists in your local MySQL server? Can you connect to the server from a cmd prompt, for example > mysql -u username -p ? – Michael Hommé Aug 18 '15 at 23:04
  • yep can connect fine, i placed details directly into the connection query just to make sure – James Aug 18 '15 at 23:05
  • there could be a server security setting that's not allowing you to connect from your current IP. can you connect anything at all from your current location using a php script? – nomistic Aug 18 '15 at 23:15

0 Answers0