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
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] ;
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.