2012-02-08 23:14:28 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class Sphinx {
|
|
|
|
private $link;
|
|
|
|
private $prefix;
|
|
|
|
|
|
|
|
public function __construct($hostname, $username, $password, $database, $prefix = NULL) {
|
|
|
|
|
2012-08-07 22:20:07 +02:00
|
|
|
list($host, $port) = explode(":", $hostname);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$this->link = new PDO("mysql:host=$host;port=$port;dbname=$database;charset=utf8", $username, $password);
|
|
|
|
}
|
|
|
|
catch(PDOException $exception) {
|
|
|
|
exit('Error: ' . $exception->getMessage() . " on database: $database<br />");
|
2012-02-08 23:14:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-07 22:20:07 +02:00
|
|
|
$this->affected = 0;
|
2012-02-08 23:14:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-07 22:20:07 +02:00
|
|
|
public function select_db($database) { }
|
|
|
|
|
|
|
|
|
|
|
|
public function query($sql, $arr = array()) {
|
2012-02-08 23:14:28 +01:00
|
|
|
$query = new stdClass();
|
|
|
|
|
2012-08-07 22:20:07 +02:00
|
|
|
$query->error = 1;
|
|
|
|
$query->errmsg = "Error";
|
2012-02-08 23:14:28 +01:00
|
|
|
$query->query = $sql;
|
2013-12-17 15:26:43 +01:00
|
|
|
$query->total_found = 0;
|
2012-02-08 23:14:28 +01:00
|
|
|
|
|
|
|
$time_start = microtime(true);
|
|
|
|
|
2012-08-07 22:20:07 +02:00
|
|
|
$i = 0;
|
|
|
|
$data = array();
|
2012-02-08 23:14:28 +01:00
|
|
|
|
2012-08-07 22:20:07 +02:00
|
|
|
$s = $this->link->prepare($sql);
|
|
|
|
if(!$s) { return $query; }
|
2012-02-08 23:14:28 +01:00
|
|
|
|
2012-08-07 22:20:07 +02:00
|
|
|
$s->execute($arr);
|
2012-02-08 23:14:28 +01:00
|
|
|
|
2012-08-07 22:20:07 +02:00
|
|
|
$this->affected = $s->rowCount();
|
2012-02-08 23:14:28 +01:00
|
|
|
|
2012-08-07 22:20:07 +02:00
|
|
|
$R = $s->fetchAll();
|
2012-02-08 23:14:28 +01:00
|
|
|
|
2012-08-07 22:20:07 +02:00
|
|
|
while(list ($k, $v) = each($R)){
|
|
|
|
$data[$i] = $v;
|
|
|
|
$i++;
|
|
|
|
}
|
2012-02-08 23:14:28 +01:00
|
|
|
|
2012-08-07 22:20:07 +02:00
|
|
|
$query->row = isset($data[0]) ? $data[0] : array();
|
|
|
|
$query->rows = $data;
|
|
|
|
$query->num_rows = $i;
|
2012-02-08 23:14:28 +01:00
|
|
|
|
2012-08-07 22:20:07 +02:00
|
|
|
$query->error = 0;
|
|
|
|
$query->errmsg = "";
|
2012-02-08 23:14:28 +01:00
|
|
|
|
2012-08-07 22:20:07 +02:00
|
|
|
unset($data);
|
2012-02-08 23:14:28 +01:00
|
|
|
|
2012-08-07 22:20:07 +02:00
|
|
|
$time_end = microtime(true);
|
2012-02-08 23:14:28 +01:00
|
|
|
|
2012-08-07 22:20:07 +02:00
|
|
|
$query->exec_time = $time_end - $time_start;
|
2012-02-08 23:14:28 +01:00
|
|
|
|
2015-08-11 15:16:13 +02:00
|
|
|
$meta = $this->link->prepare("SHOW META LIKE 'total_found'");
|
2013-12-17 15:26:43 +01:00
|
|
|
$meta->execute();
|
2013-12-25 16:26:09 +01:00
|
|
|
$R = $meta->fetchAll();
|
|
|
|
while(list ($k, $v) = each($R)){
|
2015-08-11 15:16:13 +02:00
|
|
|
if($v[0] == "total_found") { $query->total_found = $v[1]; }
|
2013-12-25 16:26:09 +01:00
|
|
|
}
|
2013-12-17 15:26:43 +01:00
|
|
|
|
2015-08-11 15:16:13 +02:00
|
|
|
if(ENABLE_SYSLOG == 1) { syslog(LOG_INFO, sprintf("sphinx query: '%s' in %.2f s, %d hits, %d total found", $query->query, $query->exec_time, $query->num_rows, $query->total_found)); }
|
|
|
|
|
2012-08-07 22:20:07 +02:00
|
|
|
return $query;
|
2012-02-08 23:14:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function countAffected() {
|
|
|
|
return mysql_affected_rows($this->link);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function getLastId() {
|
|
|
|
return mysql_insert_id($this->link);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-07 22:20:07 +02:00
|
|
|
public function __destruct() { }
|
2012-02-08 23:14:28 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
?>
|