<?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);
/** * @return int */ public function count() { return sqlrcur_rowCount($this->_cursor); }
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_num_rows($result, $config) { if ($config[sql_debug] == 'true') { print "<b>DEBUG(SQL,SQLRELAY DRIVER): Query Result: Num rows:: " . @sqlrcur_rowCount($result[cur]) . "</b><br>\n"; } return sqlrcur_rowCount($result[cur]); return 0; }
/** * Log explain of each query in log files after truncating plan_table * Only if debugExplain wanted and Oracle type database used * * @access protected * @param query $sql * @see self::execute() * @link http://www.toutenligne.com/index.php?contenu=sql_explain&menu=sql */ function _logExplain($sql) { if ($this->_bDebugExplain == true && $this->isOracle()) { /** TRUNCATE IN ORDER TO CLEAN PLAN_TABLE CAN BE REMOVED IF NO_WAIT ERRORS **/ $sSQLExplain = "TRUNCATE TABLE PLAN_TABLE"; sqlrcur_sendQuery($this->curs_id, $sSQLExplain); /** In order to identify connexion file **/ if ($this->_sFileExplainId == '') { $this->_sFileExplainId = date('Y-m-d H:i:s') . '-' . (microtime() + mt_rand(0, 999)); } /** In order identify request in explain **/ $iTime = microtime() + mt_rand(0, 999); /** Calculate execution time of the request on the server **/ list($usec, $sec) = explode(" ", microtime()); $time_Start = (double) $usec + (double) $sec; /** EXPLAIN QUERY **/ $query = "EXPLAIN PLAN \r\n\t\t SET STATEMENT_ID = '" . $iTime . "' \r\n\t\t FOR " . $sql; sqlrcur_prepareQuery($this->curs_id, $query); sqlrcur_executeQuery($this->curs_id); list($usec, $sec) = explode(" ", microtime()); $time_end = (double) $usec + (double) $sec; $_elapsedTime += $time_end - $time_Start; /** GETTING BACK INFORMATIONS **/ $sSQLExplain = "SELECT ID, PARENT_ID, OPERATION, OPTIONS, OBJECT_NAME, OBJECT_TYPE, \r\n\t COST, CARDINALITY, BYTES, OPTIMIZER\r\n FROM PLAN_TABLE\r\n WHERE STATEMENT_ID = '" . $iTime . "'\r\n ORDER BY id, parent_id"; sqlrcur_prepareQuery($this->curs_id, $sSQLExplain); $iResult = sqlrcur_executeQuery($this->curs_id); $iNbFirstResult = sqlrcur_rowCount($this->curs_id); $i = 0; $aResult = array(); while ($i < $iNbFirstResult) { $aResult[] = sqlrcur_getRowAssoc($this->curs_id, $i); $i++; } /** Writing in log file **/ $filepath = BASEPATH . '/logs/SQL/Explain/' . $this->_sFileExplainId . '.log'; if (!($fp = @fopen($filepath, "a"))) { return FALSE; } fwrite($fp, str_replace("\n", ' ', $sql) . ' ( ' . round($_elapsedTime, 4) . " sec )\n"); foreach ($aResult as $aExplain) { $message = $aExplain['ID'] . '|' . $aExplain['PARENT_ID'] . '|' . $aExplain['OPERATION'] . '|' . $aExplain['OPTIONS'] . '|' . $aExplain['OBJECT_NAME'] . '|' . $aExplain['OBJECT_TYPE'] . '|' . $aExplain['COST'] . '|' . $aExplain['CARDINALITY'] . '|' . $aExplain['BYTES'] . '|' . $aExplain['OPTIMIZER'] . "\n"; flock($fp, LOCK_EX); fwrite($fp, $message); flock($fp, LOCK_UN); } fclose($fp); @chmod($filepath, 0666); } }
/** * Number of rows in the result set * * @access public * @return integer */ function num_rows() { return sqlrcur_rowCount($this->curs_id); }