Jump to content

Accessing only part of a Database value


MortimerJazz

Recommended Posts

Hi there,

I'm currently writing a form where a user will input the first three characters of a postcode.

In my database I have registered users full postcodes, but when I run my query I only want to match the from the form to the first three letters of the database value.

I'm currently using the following query:
[code]$sql_postcode_check = mysql_query("SELECT username, postcode
  FROM users
  WHERE left(cast(postcode as string), 3) = '$postcode'");[/code]

But I'm getting an error message saying that the supplied argument is not a valid MySQL result resource.

Can anyone tell me what I'm doing wrong?

Thanks
Link to comment
https://forums.phpfreaks.com/topic/34405-accessing-only-part-of-a-database-value/
Share on other sites

add an or die at the end of the query and find out why the result resource is not there

[code]
$sql_postcode_check = mysql_query("SELECT username, postcode FROM users WHERE left(cast(postcode as string), 3) = '$postcode'") or die ('Error in Query ' . mysql_error())
[/code] ;
it seems it does not like your WHERE statement - try this one then, its worth a go(tested the substring() function on PHPMyAdmin and it works on another table I have with postcodes in it
[code]
$sql_postcode_check = mysql_query("SELECT username, postcode FROM users WHERE SUBSTRING(postcode,1,3) = '$postcode'") or die ('Error in Query ' . mysql_error())
[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.