Welcome to the Browser-Base Game Zone forums!
You are writing everything in PHP. PHP is the language you write it in. And without doubt, PHP is the core of your writing (you can replace "PHP" with "Ruby of Rails" if you happen to hate PHP ). That will set you in the proper state of mind.You don't use HTML. You use echo();. HTML is just a parameter passed to echo function. And that's all.
You minimize use of SQL since it is very, very, very expensive processing time wise. Precompute what you can on PHP, then pass the minimum to SQL.PHP good, SQL bad
db.query("SELECT * FROM Items WHERE id='".$itemID."' AND type='".$type."';");
As for structure take a look at C/C++. C is industry standard for a reason. PHP is just dumbed down C (I like to call it "C for kindergardeners" Cheesy) so almost everything aplies here as well. Check how the big brother of PHP did it.
Honestly, if you're just starting out, don't worry about it so much. Figure out a way that suits your needs for the present and explore other options.Eventually, most professionals opt for a template engine that's responsible for the presentation / output of your program. That way, your PHP code doesn't really care if it's outputting HTML, JS, CSS, XML, etc.. And usually an object is developed that controls your interaction with the database.But, again, for the time being, just do it however it seems to work and then review your process afterward and tweak it. Once you start doing maintenance on a particular program, you'll start learning better ways to structure it: "Oh, man! I should have put X over here in the xyz.php file!"
My problem with SQL has been having to use these ridiculous constructs. (I do all my PHP in HaXe now, so I may have the syntax wrong.)Code: [Select]db.query("SELECT * FROM Items WHERE id='".$itemID."' AND type='".$type."';");I had thought that prepared statements would have made the code look cleaner. Is there a better way to do it?
SELECT * FROM Items WHERE id=? && type=?;
$query = sprintf('SELECT * FROM Items WHERE id=\'%s\' && type=\'%s\';', $itemID, $type);
However, not every query needs to be done using a prepared statement. Prepared statements actually have a higher resource (albeit a slight one) than do standard queries. The exception here is if you're using prepared statements to execute a batch of statements where only the parameters change. This is because, to prepare the query, you have to make one more communication with the SQL server. It's not a huge performance cost, but each query increases that cost. So, just be aware that it's a place to potentially optimize your code if you don't really need the security there.
As another suggestion, you could use the sprintf() function to make the code look a little cleaner if that's the only objective you really need to accomplish. I feel this is a good time to tell you to beware of just blindly trusting input though. With no boilerplate surrounding this, it looks wide open to SQL injection.Code: (php) [Select]$query = sprintf('SELECT * FROM Items WHERE id=\'%s\' && type=\'%s\';', $itemID, $type);
$query = sprintf("SELECT * FROM Items WHERE id='%s' && type='%s';", $itemID, $type);
This can be trivially cleaned up a bit more by using double quotes around the format string:Code: (php) [Select]$query = sprintf("SELECT * FROM Items WHERE id='%s' && type='%s';", $itemID, $type);
But, really, use the prepared statement. It'll give you absolute protection from SQL injection attacks without the overhead (cognitive or processing) of having to escape data before giving it to the database.
(...which brings up something that hasn't occurred to me before. How does the performance impact of doing separate prepare-and-execute of a query that only gets run once compare to the performance impact of escaping user inputs? There's a non-zero cost for calling mysql_real_escape_string, after all, so it's not a case of "escape-and-interpolate is free, prepare-and-execute isn't".)
I disagree with both JGadrow and dsheroh Do not use prepared statements.
At least at the beginning. Get the hang of pure raw SQL syntax first.
"SELECT a, b, c FROM my_table WHERE d = ?"
"SELECT a, b, c FROM my_table WHERE d = '" . $some_var . "'"
Quote from: Chris on October 04, 2010, 06:59:04 AMI disagree with both JGadrow and dsheroh Do not use prepared statements.I was waiting for that...
I'm inclined to prefer prepared statements in terms of aesthetics and readability.
I have one more question while I'm here. How do you handle timed events? Do you just check if the timer's up whenever the user does, or do you have a program running in the background that monitors timers?
JGadrow- Makes sense. What kind of script would the cron event trigger?
Quote from: Topazan on October 04, 2010, 03:54:22 PMI'm inclined to prefer prepared statements in terms of aesthetics and readability. Traitor
As an example from the world of business logic, Drupal just has a single page that is called at the time of cron (/cron.php). The way the process works is that modules wanting to do something whenever the cron job is run register their intent by creating a function that's name ends with _cron and having either no parameters or all parameters are optional.
QuoteQuote from: Topazan on October 04, 2010, 03:54:22 PMI'm inclined to prefer prepared statements in terms of aesthetics and readability. Traitor
Quote from: Topazan on October 04, 2010, 03:54:22 PMI'm inclined to prefer prepared statements in terms of aesthetics and readability.
QuoteQuote from: Topazan on October 04, 2010, 03:54:22 PMI'm inclined to prefer prepared statements in terms of aesthetics and readability. Traitor Hey, talk about performance all you want, but you don't actually think those concatenated string statements look good, do you?
One more question. Does anyone have an example of an SQL injection attack that can bypass mysql_real_escape_string(), or is it just theoretical?
EDIT: One more question. Does anyone have an example of an SQL injection attack that can bypass mysql_real_escape_string(), or is it just theoretical?
placeholders = vasectomy (do it once and you never have to think about it again; naked data stored without modification)
Quote from: dsheroh on October 05, 2010, 07:24:34 AMplaceholders = vasectomy (do it once and you never have to think about it again; naked data stored without modification)Technically speaking, with a vasectomy, your "naked data" has been modified.