jeremy
jeremy

Reputation: 653

validation against a SQL table

I've always done validation by pulling the entirety of a SQL table into an array, and searching it for the value I was validating. (Is Joe Smith already in my list of users?)

Is it more effective to do a more specific SQL query looking for that value, and returning true or false?

If so, I would like to see a code example, please.

I've been attempting to do this, but keep running into "Unknown column 'variablename' in 'where clause'"

(edit:) For example sake, here is some code. I'm attempting to query a SQL table for a specific variable (a user). I then just need to know if it did or did not find the user in the table.

//$value = "Joe Smith"

$result = mysql_query("SELECT * FROM usernames WHERE fullname= $value",$db);

$rowcheck = mysql_num_rows($result);
if ($rowcheck > '0') {
// The value was found in the table.
}

Upvotes: 1

Views: 317

Answers (1)

Jacob
Jacob

Reputation: 43309

Pulling the entire table is a bad idea. If you happen to have a few million rows in your database, good luck validating this in code. Always narrow down the data transfered with a WHERE clause. If you have the correct indexes, the statement will be fast and your frontend code will be simpler.

SELECT id FROM users WHERE name='Joe Smith'

Check if this returns any rows. Concerning your error, I guess you used the wrong quotes (single quotes).

Edit: You need quotes around string literals

mysql_query("SELECT * FROM usernames WHERE fullname='$value'",$db);

Upvotes: 1

Related Questions