简体   繁体   中英

Using PHP-PEAR, packages MDB2#mysql

  • I'm using: a local WampServer Version 2.2 PHP5.4.3, PEAR 1.9.4, MDB2 2.4.1, MDB2_Driver_mysql 1.4.1
  • Executing example_php5.php:
  • errors are not recognized by 'PEAR::isError' (eg wrong password for database)
  • the whole script is executed
  • correct password: table is created with 2 rows, but I can't display the right values of the rows (see "foreach-statement")
  • if parameter 'MDB2_BufferedIterator' isn't commented out in the select-request, there is displayed "Maximum execution time of 30 seconds exceeded"
  • I have modified some functions in MDB2.php,PEAR.php by specifying these as 'static' and preventing "Strict standards: Non-static method ... should not be called statically..."
  • How to avoid the warnings ?
  • How to realize a correct error-handling and how to get values of a SQL-select ?

Many thanks in advance for some tips !

My example example_php5.php:

<pre>
<?php

/**************************************/
/* a nice php5 only show case of MDB2 */
/**************************************/

require 'MDB2.php';

// the database needs to be created manually beforehand
$dsn = array(
    'phptype'  => 'mysql',
    'username' => 'root',
    'password' => '',
    'hostspec' => 'localhost',
    'database' => 'wahlen',
);

// create MDB2 instance
$mdb2 = MDB2::factory($dsn);
if (PEAR::isError($mdb2)) {
    die($mdb2->getMessage());
}

// set the default fetchmode
//$mdb2->setFetchMode(MDB2_FETCHMODE_ASSOC);

$fields = array(
    'id' => array(
        'type'     => 'integer',
        'unsigned' => true,
        'autoincrement'  => true,
    ),
    'somename' => array(
        'type'     => 'text',
        'length'   => 12,
    ),
    'somedate'  => array(
        'type'     => 'date',
    ),
);
$table = 'sometable';
$mdb2->mgDropTable($table); 
// create a table
// since we are on php5 we can use the magic __call() method to:
// - load the manager module: $mdb2->loadModule('Manager', null, true);
// - redirect the method call to the manager module: $mdb2->manager->createTable('sometable', $fields);
$mdb2->mgCreateTable($table, $fields);

$query = "INSERT INTO $table (somename, somedate) VALUES (:name, :date)";
// parameters:
// 1) the query (notice we are using named parameters, but we could also use ? instead
// 2) types of the placeholders (either keyed numerically in order or by name)
// 3) MDB2_PREPARE_MANIP denotes a DML statement
$stmt = $mdb2->prepare($query, array('text', 'date'), MDB2_PREPARE_MANIP);
if (PEAR::isError($stmt)) {
    die($stmt->getMessage());
}

// load Date helper class
MDB2::loadFile('Date');

$stmt->execute(array('name' => 'hello', 'date' => MDB2_Date::mdbToday()));
// get the last inserted id
echo 'last insert id: ';
var_dump($mdb2->lastInsertId($table, 'id'));
$stmt->execute(array('name' => 'world', 'date' => '2005-11-11'));
// get the last inserted id
echo 'last insert id: ';
var_dump($mdb2->lastInsertId($table, 'id'));

// load Iterator implementations
MDB2::loadFile('Iterator');

$query = 'SELECT * FROM '.$table;
// parameters:
// 1) the query
// 2) true means MDB2 tries to determine the result set type automatically
// 3) true is the default and means that internally a MDB2_Result instance should be created
// 4) 'MDB2_BufferedIterator' means the MDB2_Result should be wrapped inside an SeekableIterator
$result = $mdb2->query($query, true, true);//, 'MDB2_BufferedIterator');

// iterate over the result set
foreach ($result as $row) {
    echo 'output row:<br>';
    var_dump($row);
}

// call drop table, since dropTable is not implemented in our instance
// but inside the loaded Manager module __call() will find it there and
// will redirect the call accordingly
// we could also have done:
//  $mdb2->manager->dropTable($table); or
//  $mdb2->mgDropTable($table);
//$mdb2->dropTable($table);

echo "<br/>Ende<hr/>";
?>
</pre>

and here is the listing:

( ! ) Strict standards: Declaration of MDB2_Driver_Common::raiseError() should be compatible with & PEAR::raiseError($message = NULL, $code = NULL, $mode = NULL, $options = NULL, $userinfo = NULL, $error_class = NULL, $skipmsg = false) in C:\wamp\bin\php\php5.4.3\pear\MDB2.php on line 990
Call Stack
#   Time    Memory  Function    Location
1   0.0008  263544  {main}( )   ..\example_php5.php:0
2   0.0061  932976  require( 'C:\wamp\bin\php\php5.4.3\pear\MDB2.php' ) ..\example_php5.php:8



( ! ) Strict standards: Non-static method MDB2_Date::mdbToday() should not be called statically in C:\wamp\www\peartest\example_php5.php on line 66
Call Stack
#   Time    Memory  Function    Location
1   0.0008  263544  {main}( )   ..\example_php5.php:0

last insert id: 

null

last insert id: 

null

output row:

object(MDB2_Driver_mysql)[1]
  public 'string_quoting' => 
    array (size=4)
      'start' => string ''' (length=1)
      'end' => string ''' (length=1)
      'escape' => string '\' (length=1)
      'escape_pattern' => string '\' (length=1)
  public 'identifier_quoting' => 
    array (size=3)
      'start' => string '`' (length=1)
      'end' => string '`' (length=1)
      'escape' => string '`' (length=1)
  public 'sql_comments' => 
    array (size=3)
      0 => 
        array (size=3)
          'start' => string '-- ' (length=3)
          'end' => string '
' (length=1)
          'escape' => boolean false
      1 => 
        array (size=3)
          'start' => string '#' (length=1)
          'end' => string '
' (length=1)
          'escape' => boolean false
      2 => 
        array (size=3)
          'start' => string '/*' (length=2)
          'end' => string '*/' (length=2)
          'escape' => boolean false
  public 'start_transaction' => boolean true
  public 'varchar_max_length' => int 65532
  public 'db_index' => int 1
  public 'dsn' => 
    array (size=10)
      'phptype' => string 'mysql' (length=5)
      'dbsyntax' => string 'mysql' (length=5)
      'username' => string 'root' (length=4)
      'password' => string '' (length=0)
      'protocol' => boolean false
      'hostspec' => string 'localhost' (length=9)
      'port' => boolean false
      'socket' => boolean false
      'database' => boolean false
      'mode' => boolean false
  public 'connected_dsn' => 
    array (size=10)
      'phptype' => string 'mysql' (length=5)
      'dbsyntax' => string 'mysql' (length=5)
      'username' => string 'root' (length=4)
      'password' => string '' (length=0)
      'protocol' => boolean false
      'hostspec' => string 'localhost' (length=9)
      'port' => boolean false
      'socket' => boolean false
      'database' => boolean false
      'mode' => boolean false
  public 'connection' => resource(9, mysql link)
  public 'opened_persistent' => boolean false
  public 'database_name' => string 'wahlen' (length=6)
  public 'connected_database_name' => string 'wahlen' (length=6)
  public 'connected_server_info' => string '5.5.24-log' (length=10)
  public 'supported' => 
    array (size=19)
      'sequences' => string 'emulated' (length=8)
      'indexes' => boolean true
      'affected_rows' => boolean true
      'summary_functions' => boolean true
      'order_by_text' => boolean true
      'transactions' => boolean true
      'savepoints' => boolean true
      'current_id' => string 'emulated' (length=8)
      'limit_queries' => boolean true
      'LOBs' => boolean true
      'replace' => boolean true
      'sub_selects' => boolean true
      'auto_increment' => boolean true
      'primary_key' => boolean true
      'result_introspection' => boolean true
      'prepared_statements' => boolean true
      'identifier_quoting' => boolean true
      'pattern_escaping' => boolean true
      'new_link' => boolean true
  public 'options' => 
    array (size=30)
      'ssl' => boolean false
      'field_case' => int 0
      'disable_query' => boolean false
      'result_class' => string 'MDB2_Result_%s' (length=14)
      'buffered_result_class' => string 'MDB2_BufferedResult_%s' (length=22)
      'result_wrap_class' => boolean false
      'result_buffering' => boolean true
      'fetch_class' => string 'stdClass' (length=8)
      'persistent' => boolean false
      'debug' => int 0
      'debug_handler' => string 'MDB2_defaultDebugOutput' (length=23)
      'debug_expanded_output' => boolean false
      'default_text_field_length' => int 4096
      'lob_buffer_length' => int 8192
      'log_line_break' => string '
' (length=1)
      'idxname_format' => string '%s_idx' (length=6)
      'seqname_format' => string '%s_seq' (length=6)
      'savepoint_format' => string 'MDB2_SAVEPOINT_%s' (length=17)
      'statement_format' => string 'MDB2_STATEMENT_%1$s_%2$s' (length=24)
      'seqcol_name' => string 'sequence' (length=8)
      'quote_identifier' => boolean false
      'use_transactions' => boolean true
      'decimal_places' => int 2
      'portability' => int 127
      'modules' => 
        array (size=6)
          'ex' => string 'Extended' (length=8)
          'dt' => string 'Datatype' (length=8)
          'mg' => string 'Manager' (length=7)
          'rv' => string 'Reverse' (length=7)
          'na' => string 'Native' (length=6)
          'fc' => string 'Function' (length=8)
      'emulate_prepared' => boolean false
      'datatype_map' => 
        array (size=0)
          empty
      'datatype_map_callback' => 
        array (size=0)
          empty
      'nativetype_map_callback' => 
        array (size=0)
          empty
      'default_table_type' => string '' (length=0)
  public 'wildcards' => 
    array (size=2)
      0 => string '%' (length=1)
      1 => string '_' (length=1)
  public 'as_keyword' => string ' AS ' (length=4)
  public 'warnings' => 
    array (size=0)
      empty
  public 'debug_output' => string '' (length=0)
  public 'in_transaction' => null
  public 'nested_transaction_counter' => null
  public 'has_transaction_error' => boolean false
  public 'offset' => int 0
  public 'limit' => int 0
  public 'phptype' => string 'mysql' (length=5)
  public 'dbsyntax' => string 'mysql' (length=5)
  public 'last_query' => string 'SELECT * FROM sometable' (length=23)
  public 'fetchmode' => int 2
  public 'modules' => 
    array (size=3)
      'Manager' => &
        object(MDB2_Driver_Manager_mysql)[2]
          public 'db_index' => int 1
      'Datatype' => &
        object(MDB2_Driver_Datatype_mysql)[3]
          public 'valid_default_values' => 
            array (size=10)
              ...
          public 'lobs' => 
            array (size=0)
              ...
          public 'db_index' => int 1
      'Reverse' => &
        object(MDB2_Driver_Reverse_mysql)[5]
          public 'db_index' => int 1
  public 'destructor_registered' => boolean true
  public '_debug' => boolean false
  public '_default_error_mode' => null
  public '_default_error_options' => null
  public '_default_error_handler' => string '' (length=0)
  public '_error_class' => string 'PEAR_Error' (length=10)
  public '_expected_errors' => 
    array (size=0)
      empty
  public 'manager' => &
    object(MDB2_Driver_Manager_mysql)[2]
      public 'db_index' => int 1
  public 'loaded_version_modules' => 
    array (size=3)
      0 => string 'manager' (length=7)
      1 => string 'datatype' (length=8)
      2 => string 'reverse' (length=7)
  public 'datatype' => &
    object(MDB2_Driver_Datatype_mysql)[3]
      public 'valid_default_values' => 
        array (size=10)
          'text' => string '' (length=0)
          'boolean' => boolean true
          'integer' => int 0
          'decimal' => float 0
          'float' => float 0
          'timestamp' => string '1970-01-01 00:00:00' (length=19)
          'time' => string '00:00:00' (length=8)
          'date' => string '1970-01-01' (length=10)
          'clob' => string '' (length=0)
          'blob' => string '' (length=0)
      public 'lobs' => 
        array (size=0)
          empty
      public 'db_index' => int 1
  public 'reverse' => &
    object(MDB2_Driver_Reverse_mysql)[5]
      public 'db_index' => int 1

output row:

object(MDB2_BufferedResult_mysql)[6]
  public 'db' => 
    object(MDB2_Driver_mysql)[1]
      public 'string_quoting' => 
        array (size=4)
          'start' => string ''' (length=1)
          'end' => string ''' (length=1)
          'escape' => string '\' (length=1)
          'escape_pattern' => string '\' (length=1)
      public 'identifier_quoting' => 
        array (size=3)
          'start' => string '`' (length=1)
          'end' => string '`' (length=1)
          'escape' => string '`' (length=1)
      public 'sql_comments' => 
        array (size=3)
          0 => 
            array (size=3)
              ...
          1 => 
            array (size=3)
              ...
          2 => 
            array (size=3)
              ...
      public 'start_transaction' => boolean true
      public 'varchar_max_length' => int 65532
      public 'db_index' => int 1
      public 'dsn' => 
        array (size=10)
          'phptype' => string 'mysql' (length=5)
          'dbsyntax' => string 'mysql' (length=5)
          'username' => string 'root' (length=4)
          'password' => string '' (length=0)
          'protocol' => boolean false
          'hostspec' => string 'localhost' (length=9)
          'port' => boolean false
          'socket' => boolean false
          'database' => boolean false
          'mode' => boolean false
      public 'connected_dsn' => 
        array (size=10)
          'phptype' => string 'mysql' (length=5)
          'dbsyntax' => string 'mysql' (length=5)
          'username' => string 'root' (length=4)
          'password' => string '' (length=0)
          'protocol' => boolean false
          'hostspec' => string 'localhost' (length=9)
          'port' => boolean false
          'socket' => boolean false
          'database' => boolean false
          'mode' => boolean false
      public 'connection' => resource(9, mysql link)
      public 'opened_persistent' => boolean false
      public 'database_name' => string 'wahlen' (length=6)
      public 'connected_database_name' => string 'wahlen' (length=6)
      public 'connected_server_info' => string '5.5.24-log' (length=10)
      public 'supported' => 
        array (size=19)
          'sequences' => string 'emulated' (length=8)
          'indexes' => boolean true
          'affected_rows' => boolean true
          'summary_functions' => boolean true
          'order_by_text' => boolean true
          'transactions' => boolean true
          'savepoints' => boolean true
          'current_id' => string 'emulated' (length=8)
          'limit_queries' => boolean true
          'LOBs' => boolean true
          'replace' => boolean true
          'sub_selects' => boolean true
          'auto_increment' => boolean true
          'primary_key' => boolean true
          'result_introspection' => boolean true
          'prepared_statements' => boolean true
          'identifier_quoting' => boolean true
          'pattern_escaping' => boolean true
          'new_link' => boolean true
      public 'options' => 
        array (size=30)
          'ssl' => boolean false
          'field_case' => int 0
          'disable_query' => boolean false
          'result_class' => string 'MDB2_Result_%s' (length=14)
          'buffered_result_class' => string 'MDB2_BufferedResult_%s' (length=22)
          'result_wrap_class' => boolean false
          'result_buffering' => boolean true
          'fetch_class' => string 'stdClass' (length=8)
          'persistent' => boolean false
          'debug' => int 0
          'debug_handler' => string 'MDB2_defaultDebugOutput' (length=23)
          'debug_expanded_output' => boolean false
          'default_text_field_length' => int 4096
          'lob_buffer_length' => int 8192
          'log_line_break' => string '
' (length=1)
          'idxname_format' => string '%s_idx' (length=6)
          'seqname_format' => string '%s_seq' (length=6)
          'savepoint_format' => string 'MDB2_SAVEPOINT_%s' (length=17)
          'statement_format' => string 'MDB2_STATEMENT_%1$s_%2$s' (length=24)
          'seqcol_name' => string 'sequence' (length=8)
          'quote_identifier' => boolean false
          'use_transactions' => boolean true
          'decimal_places' => int 2
          'portability' => int 127
          'modules' => 
            array (size=6)
              ...
          'emulate_prepared' => boolean false
          'datatype_map' => 
            array (size=0)
              ...
          'datatype_map_callback' => 
            array (size=0)
              ...
          'nativetype_map_callback' => 
            array (size=0)
              ...
          'default_table_type' => string '' (length=0)
      public 'wildcards' => 
        array (size=2)
          0 => string '%' (length=1)
          1 => string '_' (length=1)
      public 'as_keyword' => string ' AS ' (length=4)
      public 'warnings' => 
        array (size=0)
          empty
      public 'debug_output' => string '' (length=0)
      public 'in_transaction' => null
      public 'nested_transaction_counter' => null
      public 'has_transaction_error' => boolean false
      public 'offset' => int 0
      public 'limit' => int 0
      public 'phptype' => string 'mysql' (length=5)
      public 'dbsyntax' => string 'mysql' (length=5)
      public 'last_query' => string 'SELECT * FROM sometable' (length=23)
      public 'fetchmode' => int 2
      public 'modules' => 
        array (size=3)
          'Manager' => &
            object(MDB2_Driver_Manager_mysql)[2]
              ...
          'Datatype' => &
            object(MDB2_Driver_Datatype_mysql)[3]
              ...
          'Reverse' => &
            object(MDB2_Driver_Reverse_mysql)[5]
              ...
      public 'destructor_registered' => boolean true
      public '_debug' => boolean false
      public '_default_error_mode' => null
      public '_default_error_options' => null
      public '_default_error_handler' => string '' (length=0)
      public '_error_class' => string 'PEAR_Error' (length=10)
      public '_expected_errors' => 
        array (size=0)
          empty
      public 'manager' => &
        object(MDB2_Driver_Manager_mysql)[2]
          public 'db_index' => int 1
      public 'loaded_version_modules' => 
        array (size=3)
          0 => string 'manager' (length=7)
          1 => string 'datatype' (length=8)
          2 => string 'reverse' (length=7)
      public 'datatype' => &
        object(MDB2_Driver_Datatype_mysql)[3]
          public 'valid_default_values' => 
            array (size=10)
              ...
          public 'lobs' => 
            array (size=0)
              ...
          public 'db_index' => int 1
      public 'reverse' => &
        object(MDB2_Driver_Reverse_mysql)[5]
          public 'db_index' => int 1
  public 'result' => 
    &object(MDB2_BufferedResult_mysql)[6]
  public 'rownum' => int -1
  public 'types' => 
    array (size=3)
      0 => string 'integer' (length=7)
      1 => string 'text' (length=4)
      2 => string 'date' (length=4)
  public 'values' => 
    array (size=0)
      empty
  public 'offset' => int 0
  public 'offset_count' => int 0
  public 'limit' => int 0
  public 'column_names' => null

output row:

int -1

output row:

array (size=3)
  0 => string 'integer' (length=7)
  1 => string 'text' (length=4)
  2 => string 'date' (length=4)

output row:

array (size=0)
  empty

output row:

int 0

output row:

int 0

output row:

int 0

output row:

null


Ende
now I have found a solution for my problem, here is what I did:
- cd c:\wamp\bin\php\php5.4.3
- pear uninstall mdb2_driver_mysql //uninstall stable release
- pear uninstall mdb2              //also mdb2
- pear install mdb2-beta           //install the beta release
- pear install mdb2_driver_mysql-beta //also mysql-driver
- pear list

if you receive a message like "No valid packages found install failed"
then make:
  pear clear-cache
before repeating your command.
Now I have adapted the example-script "example_php5.php" located in
"docs\Mdb2\docs\examples\" (regarding to the above directory), concerning
database and password.
After that the script was running by the wampserver without any problems.
Conclusion: use the beta release instead of stable version.

Regards Wicki

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM