Welcome to the Browser-Base Game Zone forums!
SELECT * from tablea WHERE number=5 && text='txt' LIMIT 1;
mysql_query("SELECT * FROM tabela WHERE number=5 AND text='txt' LIMIT 1");
for '' people say DON'T use it if you have numerical entry - but WHY?
Notice that I utilized the ampersands in place of "AND". This reduces the query by a single character which optimizes it that much further.
Nice tip If I hadn't had my habits already I would switch to &&/||.
Because it is a bit slower (conversion from string to int required). BUT, sometimes it is good to make num='$num' if you haven't made int conversion/check before to avoid SQL injection.
my @res = $dbh->selectrow_array('SELECT * FROM tabela WHERE number=? AND text=? LIMIT ?', undef, 5, 'txt', 1);
my $sth = $dbh->prepare('SELECT * FROM tabela WHERE number=? AND text=? LIMIT ?');my @res = $sth->fetchrow_array(5, 'txt', 1);
as far as about SQL injection and/or XSS, I use addslashes() for every string entry and round() for every numerical entry, so I guess I escape potentional SQL injection and XSS attack
Me staying in a corner with a big transparent "No to sql placeholders!"
Quote from: Chris on April 08, 2010, 01:40:03 PMMe staying in a corner with a big transparent "No to sql placeholders!" Why's that? Just a matter of portability (in the PHP world, at least) or is there some other aspect you don't like about them?
Quote from: dsheroh on April 09, 2010, 06:11:38 AMQuote from: Chris on April 08, 2010, 01:40:03 PMMe staying in a corner with a big transparent "No to sql placeholders!" Why's that? Just a matter of portability (in the PHP world, at least) or is there some other aspect you don't like about them? Performance.
Out of curiosity, what makes them so bad performance wise?
Quote from: Chris on April 09, 2010, 10:41:11 AMQuote from: dsheroh on April 09, 2010, 06:11:38 AMQuote from: Chris on April 08, 2010, 01:40:03 PMMe staying in a corner with a big transparent "No to sql placeholders!" Why's that? Just a matter of portability (in the PHP world, at least) or is there some other aspect you don't like about them? Performance.Out of curiosity, what makes them so bad performance wise?
1. If you compare a string column to a number literal (ie, WHERE varchar=5), then MySQl cannot use any index that may exist on the string column. The same rule applies if comparing a string column to a number column (ie, WHERE varchar=integer) with no literal involved. So, if you decide to be speedy and write your queries to meticulously avoid quoting all numbers, just step back and make sure you don't do it in this case because you'll get painful table scans happening.
2. Whenever two values with different data types are compared (string=number, essentially) MySQL converts both values to floating point (real) numbers first. Floating point comparison is a little slower than integer comparison which can hurt on large datasets. It's also inexact, if that matters to your app (probably no big deal for games, but definitely a consideration in business apps or anything dealing with real money).
You mean "... WHERE id='5' " will not use the id field index?
$sql .= ' WHERE ' . mysql_real_escape_string($field) . ' = ' . (is_numeric($value) ? $value : '"' . mysql_real_escape_string($value) .'"');
What about signed int and unsigned int comparison?
Floating point comparison is a little slower than integer comparison which can hurt on large datasets. It's also inexact, if that matters to your app (probably no big deal for games, but definitely a consideration in business apps or anything dealing with real money).
Quote from: Chris on April 26, 2010, 04:08:09 AMWhat about signed int and unsigned int comparison?No worries there. Integers of any size or sign can be compared and an index can be used.
Am I correct to assume that this inexactness is solely an issue in case where the limitations of the underlying IEEE floating-point notation manifest themselves (in which case the same problems would appear with floating-point data types anyhow)?
How exactly is it done? There must be some casting done, right?