public function getAllFields($tablename) { $cur = sqlrcur_alloc($this->_conn); $success = sqlrcur_sendQuery($cur, SQLHelper::createSafeSQL("select * from :table", array(":table" => $tablename))); sqlrcon_endSession($cur); if (!$success) { throw new DatasetException(sqlrcur_errorMessage($cur)); } $fields = []; $colCount = sqlrcur_colCount($cur); for ($col = 0; $col < $colCount; $col++) { $fields[] = strtolower(sqlrcur_getColumnName($cur, $col)); } sqlrcur_free($cur); return $fields; }
<?php $con = sqlrcon_alloc('mysqlpool', 12000, '/tmp/mysqlpool.socket', 'root', '******', 0, 1); var_dump(sqlrcon_errorNumber($con)); var_dump(sqlrcon_dbHostName($con)); var_dump(sqlrcon_dbIpAddress($con)); $cur = sqlrcur_alloc($con); sqlrcur_sendQuery($cur, 'select * from test'); var_dump(sqlrcur_totalRows($cur)); for ($row = 0; $row < sqlrcur_rowCount($cur); $row++) { for ($col = 0; $col < sqlrcur_colCount($cur); $col++) { echo sqlrcur_getField($cur, $row, $col); } echo PHP_EOL; } sqlrcur_free($cur); sqlrcon_free($con);
function &serendipity_db_query($sql, $single = false, $result_type = "both", $reportErr = false, $assocKey = false, $assocVal = false, $expectError = false) { global $serendipity; $type_map = array('assoc' => sqlr_ASSOC, 'num' => sqlr_NUM, 'both' => sqlr_BOTH, 'true' => true, 'false' => false); static $benchmark = false; // highlight_string(var_export($sql, 1)); if (!is_resource($serendipity['dbConn'])) { return false; } $cur = sqlrcur_alloc($serendipity['dbConn']); $serendipity['dbCursor'] = $cur; if ($benchmark) { $start = microtime_float(); } if ($expectError) { $c = sqlrcur_sendQuery($cur, $sql); } else { $c = sqlrcur_sendQuery($cur, $sql); } if ($benchmark) { $end = microtime_float(); $cur = sqlrcur_alloc($serendipity['dbConn']); $sql_b = "INSERT INTO BLOGLOG (request, timestamp, sql, exec_time, ip) VALUES ('" . serendipity_db_escape_string($_SERVER['REQUEST_URI']) . "', NOW(), '" . serendipity_db_escape_string($sql) . "', '" . number_format($end - $start, 10) . "', '" . serendipity_db_escape_string($_SERVER['REMOTE_ADDR']) . "')"; $c = sqlrcur_sendQuery($cur, $sql_b); $psql = $sql; $psql = preg_replace('@[0-9]{10}@', 'TIMESTAMP', $psql); $sql_U = "UPDATE BLOGLOG_TOTAL SET counter = counter + 1 WHERE sql = '" . serendipity_db_escape_string($psql) . "'"; $c = sqlrcur_sendQuery($cur, $sql_U); if (sqlrcur_affectedRows() < 1) { $sql_i = "INSERT INTO BLOGLOG_TOTAL (sql, counter) VALUES ('" . serendipity_db_escape_string($psql) . "', 1)"; $c = sqlrcur_sendQuery($cur, $sql_i); } } if (!$expectError && sqlrcur_errorMessage($cur) != '') { $msg = '<pre>' . htmlspecialchars($sql) . '</pre> / ' . htmlspecialchars(sqlrcur_errorMessage($cur)); return $msg; } if (!$c) { if (!$expectError && !$serendipity['production']) { print '<pre>' . htmlspecialchars($sql) . '</pre> / ' . htmlspecialchars(sqlrcur_errorMessage($cur)); if (function_exists('debug_backtrace') && $reportErr == true) { highlight_string(var_export(debug_backtrace(), 1)); } } return $type_map['false']; } if ($c === true) { return $type_map['true']; } $result_type = $type_map[$result_type]; switch (sqlrcur_rowCount($cur)) { case 0: if ($single) { return $type_map['false']; } return $type_map['true']; case 1: if ($single) { $row = generate_resultset($cur, $result_type); if ($result_type != 'sqlr_ASSOC') { $row = $row[0]; } return $row; } default: if ($single) { return generate_resultset($cur, $result_type); } $row = generate_resultset($cur); $rows = array(); for ($idx = 0, $idxc = count($row); $idx < $idxc; $idx++) { if (!empty($assocKey)) { // You can fetch a key-associated array via the two function parameters assocKey and assocVal if (empty($assocVal)) { $rows[$assocKey] = $row[$idx][$assocKey]; } else { $rows[$row[$idx][$assocKey]] = $row[$idx][$assocVal]; } } else { $rows = $row; } } return $rows; } }
function da_sql_pconnect($config) { $retrytime = 0; if ($config[sql_use_http_credentials] == 'yes') { global $HTTP_SERVER_VARS; $SQL_user = $HTTP_SERVER_VARS["PHP_AUTH_USER"]; $SQL_passwd = $HTTP_SERVER_VARS["PHP_AUTH_PW"]; } else { $SQL_user = $config[sql_username]; $SQL_passwd = $config[sql_password]; } if ($config[sql_connect_timeout] != 0) { $retrytime = $config[sql_connect_timeout]; } if ($config[sql_debug] == 'true') { print "<b>DEBUG(SQL,SQLRELAY DRIVER): Connect: Host={$config['sql_server']},Port={$config['sql_port']},User={$SQL_user},Password={$SQL_passwd} </b><br>\n"; } $link[con] = sqlrcon_alloc($config[sql_server], $config[sql_port], '', $SQL_user, $SQL_passwd, $retrytime, 1); if ($link[con]) { sqlrcon_debugOn($link[con]); $link[cur] = sqlrcur_alloc($link[con]); if ($link[cur]) { sqlrcur_setResultSetBufferSize($link[cur], 4); sqlrcur_lowerCaseColumnNames($link[cur]); return $link; } else { return false; } } else { return false; } }
/** * Allocate a cursor * * @access protected * @see self::execute(); * @return cursor id */ function getCursor() { return $this->curs_id = sqlrcur_alloc($this->conn_id); }