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 23:01] – [Other Useful Functions] escape -> to avoid entities conversion 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 '' | + | 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 38: | Line 38: | ||
<code php> | <code php> | ||
// Section 1 | // Section 1 | ||
- | $db = mysql_connect(" | + | $db = mysqli_connect(" |
- | mysql_select_db(" | + | mysqli_select_db(" |
// Section 2 | // Section 2 | ||
- | $result = mysql_query(" | + | $result = mysqli_query(" |
if ($result === false) die(" | if ($result === false) die(" | ||
// Section 3 | // Section 3 | ||
- | while ($fields = mysql_fetch_row($result)) { | + | while ($fields = mysqli_fetch_row($result)) { |
for ($i=0, $max=sizeof($fields); | for ($i=0, $max=sizeof($fields); | ||
print $fields[$i].' | print $fields[$i].' | ||
Line 64: | Line 64: | ||
// Section 1 | // Section 1 | ||
include(" | include(" | ||
- | $db = NewADOConnection('mysql'); | + | $db = newADOConnection('mysqli'); |
- | $db->Connect(" | + | $db->connect(" |
// Section 2 | // Section 2 | ||
- | $result = $db->Execute(" | + | $result = $db->execute(" |
if ($result === false) die(" | if ($result === false) die(" | ||
// Section 3 | // Section 3 | ||
while (!$result-> | while (!$result-> | ||
- | for ($i=0, $max=$result-> | + | for ($i=0, $max=$result-> |
print $result-> | print $result-> | ||
} | } | ||
- | $result-> | + | $result-> |
print "< | print "< | ||
} | } | ||
</ | </ | ||
- | Now porting to Oracle is as simple as changing the second line to '' | + | Now porting to Oracle is as simple as changing the second line to '' |
==== Connecting to the Database ==== | ==== Connecting to the Database ==== | ||
Line 87: | Line 87: | ||
<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) die(" | if ($result === false) die(" | ||
</ | </ | ||
Line 116: | Line 116: | ||
while (!$result-> | while (!$result-> | ||
{ | { | ||
- | for ($i=0, $max=$result-> | + | for ($i=0, $max=$result-> |
print $result-> | print $result-> | ||
- | $result-> | + | $result-> |
print "< | print "< | ||
} | } | ||
Line 125: | 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 '' | + | 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 '' | + | As you can see in the above example, both recordsets store and use different fetch modes based on the '' |
===== ADOConnection ===== | ===== ADOConnection ===== | ||
Line 144: | Line 144: | ||
===== Other Useful Functions ===== | ===== Other Useful Functions ===== | ||
- | * '' | + | * '' |
- | '' | + | |
- | * $recordset%%-> | + | * '' |
- | * '' | + | * '' |
* '' | * '' | ||
Line 155: | Line 155: | ||
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 176: | Line 176: | ||
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 // | + | The next problem is that the // |
So how do we use the functions? Like this: | So how do we use the functions? Like this: | ||
Line 187: | 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 interchangeably) you are selecting by calling the recordset method '' | You can find out more information about each of the fields (I use the words fields and columns interchangeably) you are selecting by calling the recordset method '' | ||
Line 199: | Line 199: | ||
For example: | For example: | ||
<code php> | <code php> | ||
- | $recordset = $conn->Execute(" | + | $recordset = $conn->execute(" |
- | $f0 = $recordset-> | + | $f0 = $recordset-> |
</ | </ | ||
- | Then '' | + | 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 // | 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 // | ||
Line 220: | Line 220: | ||
<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 | ||
- | * '' | + | * '' |
- | * '' | + | * '' |
- | ===== 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, |
v5/userguide/mysql_tutorial.1455832880.txt.gz · Last modified: 2017/04/21 11:39 (external edit)