Web Development with PHP and MySQLDynamic, database-driven small business web sites done right.
|
| Web Developer :: phpMyEdit :: MySQL Cheat Sheet :: Clients |
|
|
MySQL Cheat SheetPortions of the following are quoted from the MySQL web site. Observations and opinions expressed below may be useful to MySQL™ newbies (use this information at your own risk). Use the underscore character in your column names, e.g. first_name. Subsequently in your PHP scripts the underscore can be easily replaced with a space. Use only lower case letters to ensure portability to other system configurations. Avoid using hyphens in the names of databases, tables, and columns. Situations can arise where the schema is not portable to another system configuration if upper case letters and/or hyphens are used. Hyphens appearing in a double-quoted line of PHP code may be interpreted as a minus sign (crashing the script). Usage of SET and ENUM fields is tricky when writing PHP scripts, but once mastered, works really well. MySQL™ will auto-generate sequential record number id's for auto_increment (integer) primary key fields. IMHO, always make an "id" field as the first column (MySQL™ column 0) and make it an auto_increment primary key field (MySQL™ utilities often expect the first column to be an auto_increment field). Vary the name of the field so that you don't end up with a field named "id" in each table (using "id" in every table may cause confusion when working with SQL queries involving more than one table). Conserve MySQL™ resources and optimize your table schema. The smallest suitable column type should be employed whenever possible. Do not use a TINYINT column as the auto_increment primary key field unless you're sure the table will contain fewer than 128 records. Do not use an INT column as the auto_increment primary key field unless the table is actually going to hold a billion records. Using SMALLINT whenever possible will help reduce the amount of storage space required. Refer to the chart on this page that reflects the impact of defining a column as UNSIGNED. I am guessing that most auto_increment primary key fields should be SMALLINT() UNSIGNED NOT NULL DEFAULT '0' ... or possibly MEDIUMINT() UNSIGNED NOT NULL DEFAULT '0'. Assign the field attribute NOT NULL unless you have a specific reason for inserting NULL's. If the table being defined does not contain BLOB fields, use CHAR instead of VARCHAR. CHAR is supposedly faster than VARCHAR if the data length is about the same for all records. Note however that you can't mix CHAR & VARCHAR in the same table. MySQL™ will silently convert CHAR fields to VARCHAR fields under certain conditions (such as if a BLOB field is added to the table).
Note that the formatting of TIMESTAMP columns is handled differently across different versions of MySQL™, with respect to hyphens and colons used to separate elements.
MySQL UtilitiesPopular utilities for working with MySQL™ include MySQL Administrator, phpMyAdmin, and phpMyEdit. For those of you who intend to create a development environment on a Windows PC, WAMP5 will quickly install Apache 2, MySQL™ 5, and PHP 5 (users can optionally select a document root that matches your web server). Before you leave this site, please check out phpMyEdit, MySQL Table Editor. Search and Replace Text in MySQL
UPDATE `tableName`
Example of an ALIAS queryThe following is an example of aliasing multiple tables, to query site names begining with "$ltr" and associating related articles and authors. // Name the tables using $A, $B, $C, $D The following is an example of aliasing multiple tables, to query site names begining with "$ltr" to locate records that DO NOT have related articles and authors. $qry2 = "SELECT $A.site_id, $A.site_name " Miscellaneous PHP ExamplesphpMyAdmin or MySQL Administrator are probably the most reliable utilities for exporting MySQL data. The following examples create a batch file that can be used with mysqldump.exe on a Windows PC to export data and move the .SQL file into a .GZ archive. Script 1 creates a batch file used to export MySQL data. Script 2 moves the exported .SQL file to a .GZ archive. Sample output from the batch file created using Script 1. Old example: How to use mysqldump.exe with a batch file in order to backup a MySQL database on Windows. PHP Script Example - FULLTEXT Search of MySQL Database Table Related MySQL info can be found on this web site under Reserved Words, MySQL Schema I, MySQL Schema II, and MySQL Image Tables - Inserting images into a MySQL database table. Upgrade headache with MySQL v3 or v4.0 --> v4.1 or v5The TIMESTAMP column is handled differently between version 4.0 and 4.1 or 5.0, leaving me to conclude that as regards portability, storing PHP's TIME() value in a VARCHAR(10) column makes more sense than using TIMESTAMP columns. Hyphens are used to separate elements of the TIMESTAMP beginning with 4.1. Because I have MySQL™ v4.0 on my PC and my web host upgraded to 4.1x, certain queries are returning unexpected results when run on the web. You might upgrade your MySQL™ version to 4.1 or 5.x on your Windows PC, and when you try to connect to the database you are greeted with Client does not support authentication protocol requested by server; consider upgrading MySQL™ client. Be sure to clear your browser cache after following the instruction in the prior link. Sometimes Firefox won't let go of old content. It may be that all you need to do is add "old-passwords" to your my.ini file and restart MySQL™. [mysqld] See also this page. If you have MySQL™ 3.x or MySQL™ 4.0.x on your PC's development environment and you want to upgrade, be aware there is no upgrade available. You have to back up all your databases, uninstall the lesser version, install the new version, and then import the data that you backed up. You should check out your backup files before you uninstall the old version of MySQL™. If using UTF-8, LATIN2 (ISO-8859-2), or CP1250 (WINDOWS-1250) character sets, after connecting to MySQL and selecting database but before querying a table you may need to execute the appropriate query below. // UTF8 connection // ISO-8859-2 connection // WINDOWS-1250 connection Example script for uploading images to MySQLUpdate: 16 July 2006 - mysql schema and upload form to insert images into mysql The above script should give you something like this:
Connecting To Multiple MySQL Databases Or HostsIf using multiple databases, "db." or "db_" may be a good choice for the prefix applied to the name of your scripts and/or web forms. Before including your db connection file, you might add a new variable:
switch($opts['data_source'])
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
copyright © 2010 - hockinson.com - all rights reserved -
home
- top
web designer - web designer - colorado web developer - web hosting - mysql form maker |