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 echo
s 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
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.
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.