v5:userguide:mysql_tutorial
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
v5:userguide:mysql_tutorial [2016/02/18 22:32] – ADODB -> ADOdb dregad | v5:userguide:mysql_tutorial [2018/06/27 16:09] (current) – [Moving from MySQL to ADOdb] increase title level dregad | ||
---|---|---|---|
Line 3: | Line 3: | ||
</ | </ | ||
- | ====== Tutorial ====== | + | |
- | ===== Moving from MySQL to ADOdb ===== | + | ====== Moving from MySQL to ADOdb ====== |
<WRAP indent> | <WRAP indent> | ||
Line 24: | Line 24: | ||
PHP is all about creating dynamic web-sites with the least fuss and the most fun. To create these websites we need to use databases to retrieve login information, | PHP is all about creating dynamic web-sites with the least fuss and the most fun. To create these websites we need to use databases to retrieve login information, | ||
- | Unfortunately in PHP every database is accessed slightly differently. To connect to MySQL, you would use mysql_connect(); when you decide to upgrade to Oracle or Microsoft SQL Server, you would use ocilogon() or mssql_connect() respectively. What is worse is that the parameters you use for the different connect functions are different also.. One database says po-tato, the other database says pota-to. Oh-oh. | + | Unfortunately in PHP every database is accessed slightly differently. To connect to MySQL, you would use '' |
===== Let's NOT call the whole thing off ===== | ===== Let's NOT call the whole thing off ===== | ||
Line 34: | Line 34: | ||
===== MySQL Example ===== | ===== MySQL Example ===== | ||
- | The most common database used with PHP is MySQL, so I guess you should be familiar with the following code. It connects to a MySQL server at localhost, database mydb, and executes an SQL select statement. The results are printed, one line per row. | + | The most common database used with PHP is MySQL, so I guess you should be familiar with the following code. It connects to a MySQL server at //localhost//, database |
<code php> | <code php> | ||
- | $db = mysql_connect(" | + | // Section 1 |
- | mysql_select_db(" | + | $db = mysqli_connect(" |
- | $result = mysql_query(" | + | mysqli_select_db(" |
+ | |||
+ | // Section 2 | ||
+ | $result = mysqli_query(" | ||
if ($result === false) die(" | if ($result === false) die(" | ||
- | while ($fields = mysql_fetch_row($result)) { | + | |
- | for ($i=0, $max=sizeof($fields); | + | // Section 3 |
- | print $fields[$i].' | + | while ($fields = mysqli_fetch_row($result)) { |
- | } | + | for ($i=0, $max=sizeof($fields); |
- | print "< | + | print $fields[$i].' |
+ | } | ||
+ | print "< | ||
} | } | ||
</ | </ | ||
- | The above code has been color-coded by section. | + | The above code has been split in 3 sections: |
+ | | ||
+ | - The execution of the SQL, and | ||
+ | - Displaying | ||
Here is the equivalent code in ADOdb | Here is the equivalent code in ADOdb | ||
<code php> | <code php> | ||
+ | // Section 1 | ||
include(" | include(" | ||
- | $db = NewADOConnection('mysql'); | + | $db = newADOConnection('mysqli'); |
- | $db->Connect(" | + | $db->connect(" |
- | $result = $db->Execute(" | + | |
+ | // Section 2 | ||
+ | $result = $db->execute(" | ||
if ($result === false) die(" | if ($result === false) die(" | ||
- | while (!$result-> | + | |
- | { | + | // Section 3 |
- | for ($i=0, $max=$result-> | + | while (!$result-> |
- | | + | for ($i=0, $max=$result-> |
- | $result-> | + | print $result-> |
+ | } | ||
+ | $result-> | ||
print "< | print "< | ||
} | } | ||
- | |||
</ | </ | ||
- | Now porting to Oracle is as simple as changing the second line to NewADOConnection(' | + | Now porting to Oracle is as simple as changing the second line to '' |
- | ===== Connecting to the Database | + | ==== Connecting to the Database ==== |
<code php> | <code php> | ||
include(" | include(" | ||
- | $db = NewADOConnection(' | + | $db = newADOConnection(' |
- | $db->Connect(" | + | $db->connect(" |
</ | </ | ||
- | The connection code is a bit more sophisticated than MySQL' | + | The connection code is a bit more sophisticated than MySQL' |
- | To conserve memory, we only load the PHP code specific to the database you are connecting to. We do this by calling NewADOConnection(databasedriver). Legal database drivers include mysql, mssql, oracle, oci8, postgres, sybase, vfp, access, ibase and many others. | + | To conserve memory, we only load the PHP code specific to the database you are connecting to. We do this by calling |
- | Then we create a new instance of the connection class by calling NewADOConnection(). Finally we connect to the database using $db-> | + | Then we create a new instance of the connection class by calling |
- | ===== Executing the SQL ===== | + | ==== Executing the SQL ==== |
<code php> | <code php> | ||
- | $result = $db->Execute(" | + | $result = $db->execute(" |
- | if ($result === false) | + | if ($result === false) die(" |
- | | + | |
</ | </ | ||
- | Sending the SQL statement to the server is straight forward. Execute() will return a recordset object on successful execution. You should check $result as we do above. | + | Sending the SQL statement to the server is straight forward. |
An issue that confuses beginners is the fact that we have two types of objects in ADOdb, the connection object and the recordset object. When do we use each? | An issue that confuses beginners is the fact that we have two types of objects in ADOdb, the connection object and the recordset object. When do we use each? | ||
Line 102: | Line 112: | ||
The only thing I need to add is that ADOdb provides several helper functions for making INSERT and UPDATE statements easier, which we will cover in the Advanced section. | The only thing I need to add is that ADOdb provides several helper functions for making INSERT and UPDATE statements easier, which we will cover in the Advanced section. | ||
- | ===== Retrieving the Data ===== | + | ==== Retrieving the Data ==== |
<code php> | <code php> | ||
while (!$result-> | while (!$result-> | ||
{ | { | ||
- | for ($i=0, $max=$result-> | + | |
- | print $result-> | + | print $result-> |
- | | + | $result-> |
- | | + | print "< |
} | } | ||
</ | </ | ||
Line 115: | Line 125: | ||
The paradigm for getting the data is that it's like reading a file. For every line, we check first whether we have reached the end-of-file (EOF). While not end-of-file, | The paradigm for getting the data is that it's like reading a file. For every line, we check first whether we have reached the end-of-file (EOF). While not end-of-file, | ||
- | The $result-> | + | The '' |
<code php> | <code php> | ||
$ADODB_FETCH_MODE = ADODB_FETCH_NUM; | $ADODB_FETCH_MODE = ADODB_FETCH_NUM; | ||
- | $rs1 = $db->Execute(' | + | $rs1 = $db->execute(' |
$ADODB_FETCH_MODE = ADODB_FETCH_ASSOC; | $ADODB_FETCH_MODE = ADODB_FETCH_ASSOC; | ||
- | $rs2 = $db->Execute(' | + | $rs2 = $db->execute(' |
print_r($rs1-> | print_r($rs1-> | ||
print_r($rs2-> | print_r($rs2-> | ||
</ | </ | ||
- | As you can see in the above example, both recordsets store and use different fetch modes based on the $ADODB_FETCH_MODE setting when the recordset was created by Execute(). | + | As you can see in the above example, both recordsets store and use different fetch modes based on the '' |
===== ADOConnection ===== | ===== ADOConnection ===== | ||
Line 134: | Line 144: | ||
===== Other Useful Functions ===== | ===== Other Useful Functions ===== | ||
- | $recordset-> | + | * '' |
- | $recordset-> | + | * '' |
- | $recordset-> | + | * '' |
- | rs2html($recordset) is a function that is generates a HTML table based on the $recordset passed to it. An example with the relevant lines in bold: | + | * '' |
<code php> | <code php> | ||
- | |||
include(' | include(' | ||
include(' | include(' | ||
- | $conn = ADONewConnection('mysql'); | + | $conn = newADOConnection('mysqli'); |
- | $conn->PConnect(' | + | $conn->pConnect(' |
- | $rs = $conn->Execute(' | + | $rs = $conn->execute(' |
rs2html($rs); | rs2html($rs); | ||
</ | </ | ||
Line 159: | Line 168: | ||
Let's say you want to insert the following data into a database. | Let's say you want to insert the following data into a database. | ||
- | | + | < |
- | TheDate=mktime(0, | + | ID = 3 |
- | Note= sugar why don't we call it off | + | TheDate=mktime(0, |
+ | Note= sugar why don't we call it off | ||
+ | </ | ||
When you move to another database, your insert might no longer work. | When you move to another database, your insert might no longer work. | ||
- | The first problem is that each database has a different default date format. MySQL expects YYYY-MM-DD format, while other databases have different defaults. ADOdb has a function called [[v5: | + | The first problem is that each database has a different default date format. MySQL expects YYYY-MM-DD format, while other databases have different defaults. ADOdb has a function called [[v5: |
- | The next problem is that the don't in the Note needs to be quoted. In MySQL, we use don\'t but in some other databases (Sybase, Access, Microsoft SQL Server) we use don'' | + | The next problem is that the //don't// in the Note needs to be quoted. In MySQL, we use '' |
So how do we use the functions? Like this: | So how do we use the functions? Like this: | ||
Line 176: | Line 187: | ||
. $db-> | . $db-> | ||
. $db-> | . $db-> | ||
- | $db->Execute($sql); | + | $db->execute($sql); |
</ | </ | ||
- | ADOdb also supports [[v5: | + | ADOdb also supports |
- | ===== MetaTypes | + | ==== MetaTypes ==== |
- | You can find out more information about each of the fields (I use the words fields and columns | + | You can find out more information about each of the fields (I use the words fields and columns |
For example: | For example: | ||
<code php> | <code php> | ||
- | $recordset = $conn->Execute(" | + | $recordset = $conn->execute(" |
- | $f0 = $recordset-> | + | $f0 = $recordset-> |
</ | </ | ||
- | Then $f0-> | + | Then '' |
- | One problem with handling different databases is that each database often calls the same type by a different name. For example a timestamp type is called datetime in one database and time in another. So ADOdb has a special MetaType($type, | + | One problem with handling different databases is that each database often calls the same type by a different name. For example a timestamp type is called |
- | | + | ^ MetaType ^ Description ^ |
- | X: text or long character (eg. more than 255 bytes wide). | + | | C | character and varchar types | |
- | B: blob or binary image | + | | X | text or long character (eg. more than 255 bytes wide). |
- | D: date | + | | B | blob or binary image | |
- | T: timestamp | + | | D | date | |
- | L: logical (boolean) | + | | T | timestamp |
- | I: integer | + | | L | logical (boolean) |
- | N: numeric (float, double, money) | + | | I | integer |
+ | | N | numeric (float, double, money) | ||
In the above date example, | In the above date example, | ||
<code php> | <code php> | ||
- | $recordset = $conn->Execute(" | + | $recordset = $conn->execute(" |
- | $f0 = $recordset-> | + | $f0 = $recordset-> |
- | $type = $recordset-> | + | $type = $recordset-> |
print $type; /* should print ' | print $type; /* should print ' | ||
</ | </ | ||
- | ===== Select Limit and Top Support | + | ==== Select Limit and Top Support ==== |
- | ADOdb has a function called [[v5: | + | ADOdb has a function called |
- | ===== Caching Support | + | ==== Caching Support ==== |
ADOdb allows you to cache recordsets in your file system, and only requery the database server after a certain timeout period with | ADOdb allows you to cache recordsets in your file system, and only requery the database server after a certain timeout period with | ||
- | * $connection-> | + | * '' |
- | * $connection-> | + | * '' |
- | ===== Session Handler Support | + | ==== Session Handler Support ==== |
ADOdb also supports session handlers. You can store your session variables in a database for true scalability using ADOdb. For further information, | ADOdb also supports session handlers. You can store your session variables in a database for true scalability using ADOdb. For further information, | ||
Line 235: | Line 247: | ||
===== Conclusion ===== | ===== Conclusion ===== | ||
- | As a thank you for finishing this article, here are the complete lyrics for let's call the whole thing off. | + | As a thank you for finishing this article, here are the complete lyrics for //let's call the whole thing off//. |
- | Refrain | + | <WRAP indent> |
- | You say eether and I say eyether, | + | // |
- | You say neether and I say nyther; | + | Refrain |
- | Eether, eyether, neether, nyther - | + | You say eether and I say eyether, |
- | Let's call the whole thing off ! | + | You say neether and I say nyther; |
- | + | Eether, eyether, neether, nyther - \\ | |
- | You like potato and I like po-tah-to, | + | Let's call the whole thing off ! \\ |
- | You like tomato and I like to-mah-to; | + | \\ |
- | Potato, po-tah-to, tomato, to-mah-to - | + | You like potato and I like po-tah-to, |
- | Let's call the whole thing off ! | + | You like tomato and I like to-mah-to; |
- | + | Potato, po-tah-to, tomato, to-mah-to - \\ | |
- | + | Let's call the whole thing off ! \\ | |
- | | + | \\ |
- | And oh, if we ever part, then that might break my heart. | + | But oh, if we call the whole thing off, then we must part. \\ |
- | + | And oh, if we ever part, then that might break my heart. | |
- | So, if you like pajamas and I like pa-jah-mas, | + | \\ |
- | I'll wear pajamas and give up pa-jah-mas. | + | So, if you like pajamas and I like pa-jah-mas, |
- | For we know we | + | I'll wear pajamas and give up pa-jah-mas. |
- | Need each other, so we | + | For we know we \\ |
- | Better call the calling off off. | + | Need each other, so we \\ |
- | Let's call the whole thing off ! | + | Better call the calling off off. \\ |
- | + | Let's call the whole thing off ! \\ | |
- | Second Refrain | + | \\ |
- | + | Second Refrain | |
- | You say laughter and I say lawfter, | + | \\ |
- | You say after and I say awfter; | + | You say laughter and I say lawfter, |
- | Laughter, lawfter, after, awfter - | + | You say after and I say awfter; |
- | Let's call the whole thing off ! | + | Laughter, lawfter, after, awfter - \\ |
- | + | Let's call the whole thing off ! \\ | |
- | You like vanilla and I like vanella, | + | \\ |
- | You, sa' | + | You like vanilla and I like vanella, |
- | Vanilla, vanella, choc' | + | You, sa' |
- | Let's call the whole thing off ! | + | Vanilla, vanella, choc' |
- | + | Let's call the whole thing off ! \\ | |
- | But oh, if we call the whole thing off, then we must part. | + | \\ |
- | And oh, if we ever part, then that might break my heart. | + | But oh, if we call the whole thing off, then we must part. \\ |
+ | And oh, if we ever part, then that might break my heart. | ||
+ | \\ | ||
+ | So, if you go for oysters and I go for ersters, \\ | ||
+ | I'll order oysters and cancel the ersters. \\ | ||
+ | For we know we \\ | ||
+ | Need each other, so we \\ | ||
+ | Better call the calling off off. \\ | ||
+ | Let's call the whole thing off ! | ||
+ | // | ||
+ | </ | ||
- | So, if you go for oysters and I go for ersters, | ||
- | I'll order oysters and cancel the ersters. | ||
- | For we know we | ||
- | Need each other, so we | ||
- | Better call the calling off off. | ||
- | Let's call the whole thing off ! | ||
- | | ||
Song and lyrics by George and Ira Gershwin, introduced by Fred Astaire and Ginger Rogers in the film "Shall We Dance?" | Song and lyrics by George and Ira Gershwin, introduced by Fred Astaire and Ginger Rogers in the film "Shall We Dance?" | ||
(c) 2001-2014 John Lim. | (c) 2001-2014 John Lim. |
v5/userguide/mysql_tutorial.1455831127.txt.gz · Last modified: 2017/04/21 11:39 (external edit)