0

Not sure if I have coded this incorrectly or if it is just because I have a very large database that I am trying to pull from.

I'm not receiving any errors but there is just nothing being displayed on the page. Is having a large database an issue which might prevent me from being able to find my result. I know that the postcode is within the database as I have found it in PHPMYADMIN SQL tab.

<?php

$location = $user['location'];

$postcode = DB::query("SELECT * FROM postcodes WHERE Postcode LIKE '%" . $location . "%'");

$longitude = $postcode['Longitude'];

echo $longitude;

?>

I'm getting the $user['location'] from a query which is already loaded in and echos on the page shows the postcode on the page already.

I'm very new to PHP and MYSQL so am trying to learn but when it isn't giving me any errors it makes it very hard to search for what I am looking for.

Thank you very much - Jonny Dommett

EDIT - Table Schema

id          int(11)         NO      PRI     NULL    auto_increment  
Postcode    varchar(8)      NO              NULL        
Latitude    decimal(9,6)    NO              NULL        
Longitude   decimal(9,6)    NO              NULL        

EDIT - PDO

    <?php
  class DB{

    private static function connect(){
      $pdo = new PDO('mysql:host=localhost;dbname=vapoural_wsc;charset=utf8','testing','testing123');
      $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
      return $pdo;

    }
    public static function query($query, $params = array()){
      $statement = self::connect()->prepare($query);
      $statement->execute($params);


   if (explode(' ', $query)[0] == 'SELECT') {
  $data = $statement-> fetchAll();
  return $data;
}

    }
  }

 ?>

EDIT - VAR DUMP

var_dump($postcode);

produced a result of

array(0) { }

EDIT - SQL WITHIN PHPMYADMIN

enter image description here

EDIT - The ISSUE = FIXED

The Issue was from within my database on where I had entered my data. Make sure to check your columns by doing VARDUMP to see what that results first.

Dimsquid
  • 480
  • 1
  • 7
  • 21
  • 2
    You `echo` `$longitude` but you set `$logitude` without the 'n'. I guess the error displaying is not enabled in your `php.ini`, otherwise it would tell you. – Anthony Aug 16 '18 at 09:25
  • @RonnieOosting It's result is NULL but the database has nothing set as NULL – Dimsquid Aug 16 '18 at 09:27
  • @AnthonyB I didn't notice that I have changed it but it did not change anything. I'm not to sure I will have a look at the php.ini now. – Dimsquid Aug 16 '18 at 09:31
  • Please update your question with the code without the typo, and provide us your table schema (column names). You've used `Logitude` in the array key too. – Anthony Aug 16 '18 at 09:33
  • @AnthonyB I have added the table schema and changed the errors in the spelling. – Dimsquid Aug 16 '18 at 09:41
  • Now could you provide us some more informations? A reproducible example would be great but there you are using `DB::query`, please write your code with a basic PDO/mysqli code in this example. A `var_dump($postcode)` would be useful too. – Anthony Aug 16 '18 at 09:45
  • @AnthonyB What do you mean a reproducible Example? The var dump produced array(0) { } Ok so that is not what I thought it would produce at all. – Dimsquid Aug 16 '18 at 09:47
  • By _reproducible example_ I mean a code I can test on my machine to help you. So please write your code with PDO or mysqli in order to have no dependencies. – Anthony Aug 16 '18 at 09:48
  • @AnthonyB I have now added my PDO class into the edit. – Dimsquid Aug 16 '18 at 09:53
  • I've tested it, and it works on my machine. I think your error was the $longitude/$logitude, and now your query doesn't find anything because your `$location` does not match. Try to set a static `$location`. – Anthony Aug 16 '18 at 09:59
  • Please add `try-catch` blocks inside your query and connect functions. This way you can catch if there's an error on your code, instead of looking at a blank page. Also put `error_reporting(E_ALL);` on top of your page so it will notify you if there's any error in it. – Cemal Aug 16 '18 at 10:02
  • @AnthonyB What do you mean by add static $location sorry I'm very new to PHP. – Dimsquid Aug 16 '18 at 10:07

1 Answers1

1

Your error was in because of your typo in the variables $longitude/$logitude and in the array key Logitude.

Here is a working code, tested on my machine.

<?php
class DB{
    private static function connect(){
      $pdo = new PDO('mysql:host=localhost;dbname=vapoural_wsc;charset=utf8','testing','testing123');
      $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
      return $pdo;

    }
    public static function query($query, $params = array()){
      $statement = self::connect()->prepare($query);
      $statement->execute($params);
      if (explode(' ', $query)[0] == 'SELECT') {
          $data = $statement-> fetchAll();
          return $data;
      }
    }
}


$location = '4200';
$query = "SELECT * FROM postcodes WHERE Postcode LIKE ?";
var_dump(DB::query($query, ['%'.$location.'%']));

Output :

array(1) {
[0]=>
array(8) {
  ["id"]=>
  string(1) "1"
  [0]=>
  string(1) "1"
  ["Postcode"]=>
  string(5) "42000"
  [1]=>
  string(5) "42000"
  ["Latitude"]=>
  string(4) "2344"
  [2]=>
  string(4) "2344"
  ["Longitude"]=>
  string(4) "2334"
  [3]=>
  string(4) "2334"
  }
}

The output is an example of random datas that I've inserted in my database.

Important note : this code use a prepared statement. You are using a variable in your query, so please use a prepared statement to avoid SQL injection.

In this code I've set $location to a static value to be sure the provided code works.

Anthony
  • 2,014
  • 2
  • 19
  • 29
  • 1
    You can always do `"SELECT * FROM postcodes WHERE Postcode LIKE concat('%',?,'%')"` if you're using a db framework, that uses `%` for sthg else. – Cemal Aug 16 '18 at 10:07
  • Thank you what was your result from the Var dump as mine still says array(0) { }. – Dimsquid Aug 16 '18 at 10:16
  • @JonnyGinaDommett I've added an output example. – Anthony Aug 16 '18 at 10:21
  • @AnthonyB I have just tested mine if I set `$location` to . `$location = $user['location']; ` the result is ` array(0) { } ` but if I set it to `$location = "BN11 3HB";` then I get what I want it to print out I have just checked the `$user['location'] ` and if I just echo that I get `"BN11 3HB"` it just doesn't work I am very confused now – Dimsquid Aug 16 '18 at 10:23
  • Thank you @AnthonyB The Issue was my fault there was a space in the database where I had inserted the postcode and added an extra space god I feel like a fool. Thank you very much tho. – Dimsquid Aug 16 '18 at 10:31