ADOdb

Database Abstraction Layer for PHP

User Tools

Site Tools


v5:userguide:learn_abstraction:key_value_pairs

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
v5:userguide:learn_abstraction:key_value_pairs [2016/03/17 01:20] mnewnhamv5:userguide:learn_abstraction:key_value_pairs [2021/02/21 13:45] (current) – typo, httpheader for json, param names for mysqli example peterdd
Line 1: Line 1:
 <- v5:userguide:learn_abstraction:basic_query|A Basic Query ^ v5:userguide:learn_abstraction:start_lesson|Start Of Lesson ^ v5:userguide:learn_abstraction:using_execute|Using The Execute Method -> <- v5:userguide:learn_abstraction:basic_query|A Basic Query ^ v5:userguide:learn_abstraction:start_lesson|Start Of Lesson ^ v5:userguide:learn_abstraction:using_execute|Using The Execute Method ->
 +~~NOTOC~~
 ====== Key/Value Pairs ====== ====== Key/Value Pairs ======
 +===== An Associative Array =====
 An often-used SQL construction is to read a table for a key/value pair. This can be used in creating Select Boxes, Radio groups etc. ADOdb contains a shortcut command [[v5:reference:connection:getassoc|getAssoc()]] to assist in the task. An often-used SQL construction is to read a table for a key/value pair. This can be used in creating Select Boxes, Radio groups etc. ADOdb contains a shortcut command [[v5:reference:connection:getassoc|getAssoc()]] to assist in the task.
  
Line 42: Line 44:
 * Program adodbExample.php * Program adodbExample.php
 */ */
-include 'adodb_dir/adodb.inc.php';+include_once 'adodb_dir/adodb.inc.php';
  
 /* /*
Line 50: Line 52:
 class jsonObject class jsonObject
 { {
 +   /*
 +    * The jquery plugin requires each pair of data be a json object,
 +    * with the key called 'id' and the description called 'text'
 +    */
     public $id;     public $id;
     public $text;     public $text;
Line 57: Line 63:
  
 $db = newAdoConnection('mysqli'); $db = newAdoConnection('mysqli');
-$db->connect('parameter1','parameter2','etc','etc');+#$db->debug=true; 
 +$db->connect('dbhost','dbuser','dbpassword','dbname');
  
 $sql = "SELECT state_code,name  $sql = "SELECT state_code,name 
          FROM us_states          FROM us_states
      ORDER BY state_code";      ORDER BY state_code";
-$result = $db->execute($SQL);+$result = $db->execute($sql);
 while ($r = $result->fetchRow()) while ($r = $result->fetchRow())
 { {
Line 74: Line 81:
 * Return the data to the calling progam * Return the data to the calling progam
 */ */
 +if(!$db->debug){
 +    header('Content-Type: application/json');
 +}
 print json_encode($rows); print json_encode($rows);
 </code> </code>
Line 80: Line 90:
 The method [[v5:reference:recordset:fetchnextobj|fetchNextObject()]] returns data as a PHP object which can be easily JSON encoded. The method [[v5:reference:recordset:fetchnextobj|fetchNextObject()]] returns data as a PHP object which can be easily JSON encoded.
  
-{{htmlmetatags>metatag-keywords:(php, programming, json, javascript, jquery, adodb, select2, plugin, ajax)}} +{{htmlmetatags>metatag-keywords=(php, programming, json, javascript, jquery, adodb, select2, plugin, ajax)}} 
-{{htmlmetatags>metatag-description:(An example of how to use ADOdb to serve a jquery plugin using ajax )}}+{{htmlmetatags>metatag-description=(An example of how to use ADOdb to serve a jquery plugin using ajax )}}
  
  
v5/userguide/learn_abstraction/key_value_pairs.1458174031.txt.gz · Last modified: 2017/04/21 11:40 (external edit)