|
|||||||
New to SitePoint Forums? Register here for free!
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
SitePoint Wizard
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Oct 2001
Posts: 2,714
|
PHP and MySQL coding tips
The great "PHP and MySQL coding tips" thread has been restructured. The posts from the old thread have been sorted into four categories and are quoted below, with a link to the original post. Not all posts have been quoted.
Before you create a new tip, please make sure it's not already in here. Please do not create fluff posts. Instead of a thank you post here, send the person a Private Message (PM). It is Falesh that has done the heavy work here pulling the original thread apart. We would like to say a really big thank you for doing it. Great work! Send him a PM and say thanks! Last edited by longneck; Jun 27, 2008 at 11:55.. |
|
|
|
|
#2 | ||||||||
|
SitePoint Wizard
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Oct 2001
Posts: 2,714
|
Strings
Quote:
Quote:
Quote:
Quote:
Quote:
Quote:
Quote:
Quote:
|
||||||||
|
|
|
|
#3 | ||||||||||||||||||||||
|
SitePoint Wizard
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Oct 2001
Posts: 2,714
|
MySQL
Quote:
Quote:
Quote:
Quote:
Quote:
Quote:
Quote:
Quote:
Quote:
Quote:
Quote:
Quote:
Quote:
|
||||||||||||||||||||||
|
|
|
|
#4 | |||||||||||||
|
SitePoint Wizard
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Oct 2001
Posts: 2,714
|
GLOBALS
Quote:
Quote:
Quote:
Quote:
Quote:
Quote:
Quote:
Quote:
Quote:
Quote:
Quote:
|
|||||||||||||
|
|
|
|
#5 | |||||||||||||||||||
|
SitePoint Wizard
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Oct 2001
Posts: 2,714
|
Misc.
Quote:
Quote:
Quote:
Quote:
Quote:
Quote:
Quote:
Quote:
Quote:
Quote:
Quote:
Quote:
Quote:
Quote:
Quote:
Quote:
Quote:
Quote:
Quote:
|
|||||||||||||||||||
|
|
|
|
#6 |
|
SitePoint Evangelist
![]() ![]() ![]() ![]() Join Date: Feb 2007
Location: UK
Posts: 594
|
My tip (I noticed nobody ever wrote this in any articles anywhere which gave me lots of problems until I found it out my self):
When using mysql_real_escape_string() or addslashes() it changes ' and " to \' and \" which we all know, right? But when adding the variables to the mysql database it doesnt actually put the backslashes into the table. You DONT need to use strip slashes when bringing data out of the table because the backslashes arent added! Probably sounds like I'm stupid, but there is not one article that I learned from which told me "the backslashes arent actually added to the database". |
|
|
|
|
#7 |
|
SitePoint Enthusiast
![]() Join Date: Jun 2007
Posts: 26
|
Well, the name of the thread is "PHP and MySQL coding tips" but I see many people submitting code that can be useful. And now the debug class I'm using in conjunction with the above db class and the q() function:
PHP Code:
dbgWriteDb() -> This one obviously writes the input value to a table including two columns : an id and a message of type text. And the showMysqlError(). You will need the function mysql_error() frequently in case of errors. And there can be times that it is impossible to output the error to the screen. One example is a custom class that uses the database as the session storage instead of the default file system. Since some session job have to happen before any output sent, if there is a database error, you cannot see it on your screen. Instead, you can direct it to a database table. This function writes the error either to the screen (default) or to the database. Examples: PHP Code:
PHP Code:
Last edited by halil; Aug 19, 2007 at 13:12.. Reason: Some more formatting and correction |
|
|
|
|
#8 |
|
masquerading
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2003
Location: East Coast
Posts: 2,227
|
This might have been mentioned before, but I didn't see it. Anyways, it's a small performance increase trick - unnoticeable in 99% of the cases, but could make a bit of difference in very large apps. echo actually takes parameters, unlike print, which means if you are using it to print out strings and variables, you don't have to concatenate, you can do it like this:
PHP Code:
[construed from the php manual]
__________________
█ Nick . all that we see or seem, is but a dream within a dream █ Show someone you care, send them a virtual flower. █ Good deals on men's watches
|
|
|
|
|
#9 |
|
SitePoint Zealot
![]() ![]() Join Date: Feb 2003
Location: Norway
Posts: 110
|
if you have a simple statement with something that needs to happen if some criteria is set, you *could* do this:
PHP Code:
PHP Code:
__________________
If pigs could fly, the prize of bacon would reach the sky. www.dosspirit.net - Norwegian reviews of DOS games |
|
|
|
|
#10 |
|
SitePoint Guru
![]() ![]() ![]() ![]() ![]() Join Date: Feb 2008
Location: end($world)
Posts: 834
|
Hi guys. Here is a simple function I often use to format text from textarea into nice XHTML.
What it basically does - it removes newlines and inserts HTML paragraph tags (<p>, </p>) and breaks (<br />). PHP Code:
PHP Code:
![]() Last edited by risoknop; Sep 10, 2008 at 04:44.. |
|
|
|
|
#11 |
|
SitePoint Guru
![]() ![]() ![]() ![]() ![]() Join Date: Feb 2008
Location: end($world)
Posts: 834
|
Here's how to insert an UNIX timestamp into MySQL. I learned it yesterday:
PHP Code:
In addition, here is how to select an UNIX timestamp from a database: PHP Code:
Last edited by risoknop; Sep 10, 2008 at 04:43.. |
|
|
|
|
#12 |
|
SitePoint Evangelist
![]() ![]() ![]() ![]() Join Date: Apr 2006
Location: South London
Posts: 579
|
The most important tip any PHP beginner must have is: to turn the error reporting on when developing script. Putting this single line
PHP Code:
When error reporting is not on, PHP masks many warnings that could potentially bring down your application, and when that happens, it is hard to find the problems which are often misspelt variables and variables that have not been set or not available in that part of the code. Equally impotant is that you take the error reporting off when you put your code in a live website. Hope that helps somebody. Rageh
__________________
|
|
|
|
|
#13 |
|
SPF Thingymajig
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jan 2004
Location: uk, Leeds (area)
Posts: 1,079
|
Email Attachment Coding Tip
Just recently I've been trying to send an email with an attachment (an image file) using php. I know how to send an email with no attachment so this was new to me and after a few days work and looking at other scripts I've came up with the code below. I had a look at Phpmailer after so many attempts and failing. The script worked a treat but I did wondered how? After looking through the script it became clear that more than one random hash code was needed. This does not mean two different codes had to be randomly made but (as you will see below) before the codes different names can be used to split them both apart. In this case "b1_" and "b2_" are used. PHP Code:
The content type can be changed to suit the file being attached. spence |
|
|
|
|
#14 |
|
SPF Thingymajig
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jan 2004
Location: uk, Leeds (area)
Posts: 1,079
|
Email() & Hotmail
If you are having trouble with sending email with mail() to hotmail, then please try the following:
|
|
|
|
|
#15 |
|
Unobtrusively zen
![]() ![]() Join Date: Jan 2007
Location: Christchurch, New Zealand
Posts: 7,854
|
Here are the results of combing through the top ten search results for php code conventions, as they apply to quoting strings.
1. Zend Framework - Coding Style When a string is literal (contains no variable substitutions), the apostrophe or "single quote" should always be used to demarcate the string 2. evolt.org PHP coding guidelines Strings in PHP can either be quoted with single quotes ('') or double quotes (""). The difference between the two is that the parser will use variable-interpolation in double-quoted strings, but not with single-quoted strings. So if your string contains no variables, use single quotes and save the parser the trouble of attempting to interpolate the string for variables 3. cartoweb .org Code Convention Coding Style - Developers should use the PEAR coding standards as the coding style reference in CartoWeb [which state] Things like "single quotes vs double quotes" are features of PHP itself to make programming easier and there are no reasons not use one way in preference to another. Such best practices are left solely on developer to decide. 4. Valio - PHP Code Standard and Name Conventions We are going to follow the code standard and name conventions created by the Zend Framework. [as in #1] 5. Drupal Coding Standards Drupal does not have a hard standard for the use of single quotes vs. double quotes. Where possible, keep consistency within each module, and respect the personal style of other developers. With that caveat in mind: single quote strings are known to be faster because the parser doesn't have to look for in-line variables. Their use is recommended except in two cases:
6. OrangeHRM PHP Coding Standards When a string is literal (contains no variable substitutions), the apostrophe or "single quote" should always be used to demarcate the string 7. Stack Overflow - Which coding conventions to follow for PHP For PHP, i'd suggest to follow Zends suggestions [as in #1] 8. TuVinh PHP Coding Convention [unstated] 9. Wikipedia - Programming Style PHP::PEAR: PHP::PEAR Coding Standards [as in #3] 10. PHP Chapter Conventions [unrelated] |
|
|
![]() |
| Bookmarks |
«
Previous Thread
|
Next Thread
»
| Thread Tools | |
| Display Modes | |
|
|
|
All times are GMT -7. The time now is 05:32.








when you do a SHOW INDEX FROM table, it shows 3 indexes.
i too thought it might ignore the KEY and UNIQUE.
The PHP developers seem to have realized that register_globals was a bad idea, though. They discuss 







Linear Mode
