0

what I'm trying to do is:- when someone enters a postcode from a HTML form, the query will check the first half of the post code and return all the entries that match.

<?php
$link = mysqli_connect("localhost", "root", "mypass123", "carpooling");

$location = mysqli_real_escape_string($link, $_REQUEST['location']);

$query="SELECT * FROM journeys WHERE substring(location='$location', 1, 4)";
$results = mysqli_query($link, $query);
print_r($results);
?>

I've tried to use half of the substring because the postcode will be 8 characters long anyway, and I just need the first 4 to find out what area they are in. I just get a white page whenever I run it, I know it won't display the results as a list but I just need it to return properly.

EDIT: Now I'm getting:-

mysqli_result Object ( [current_field] => 0 [field_count] => 7 [lengths] => [num_rows] => 1 [type] => 0 )

as the output, there should be 3 rows that match this, so it's not actually finding the values, any ideas?

Thank you!

Pedwar
  • 1
  • 1
  • [PHP white screen of death](https://stackoverflow.com/questions/1475297/phps-white-screen-of-death) – Spoody Apr 09 '18 at 22:03
  • 2
    `like %'search_for_string'%`, quotes should be outside percent sign `'%search_for_string%'` – Felippe Duarte Apr 09 '18 at 22:03
  • Haha thank you Felippe, I would never have spotted that. Cheers. – Pedwar Apr 09 '18 at 22:06
  • Have you tried `result * 2` ?, if they are 1/2 the input, then multiplying the result by 2 should make them the whole input, no? {sarchasm}, seriously wouldn't `substring(location='$location', 1, 5)` be better? what if the postcode is only the first 5, or is this not US postcode `55555-4444`? – ArtisticPhoenix Apr 09 '18 at 22:12
  • Also these are not your real credentials I hope `"localhost", "root", "mypass123", "carpooling"` Because I wouldn't suggest advertising them. – ArtisticPhoenix Apr 09 '18 at 22:18
  • I've just tried your method substring(location='$location', 1, 4) and it still doesn't return any rows, it's a UK postcode so it's XXXX-XXX. Thanks though! and no they aren't don't worry haha – Pedwar Apr 09 '18 at 22:22
  • All you have is `WHERE X` but that needs to be `=` to something. So try `WHERE substring(location='$location', 1, 4) = '1234'` or something similar. – WOUNDEDStevenJones Apr 09 '18 at 22:32
  • I need it to be = to the first 4 characters of the in putted $location though, so it needs to work for whatever the first 4 chars could be, so I can't have a static value as the =. Thank you though! – Pedwar Apr 09 '18 at 22:41

0 Answers0