-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathquery.class.php
More file actions
79 lines (64 loc) · 2.02 KB
/
query.class.php
File metadata and controls
79 lines (64 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
namespace PluSQL;
use EmptySetException,Exception,mysqli;
class Query
{
private $query;
private $link;
public function __construct($sql,$link)
{
if($link instanceof mysqli)
$query = $link->query($sql);
else
$query = mysql_query($sql,$link);
if(!$query)
{
if($link instanceof mysqli)
$error = $link->error;
else
$error = mysql_error();
throw new SqlErrorException($error);
}
$this->query = $query;
$this->link = $link;
}
public function __get($name)
{
return new QueryIterator($this,$name);
}
public function nextRow()
{
if($this->link instanceof mysqli)
$row = $this->query->fetch_assoc();
else
$row = mysql_fetch_assoc($this->query);
return $row;
}
public function rowAtIndex($index)
{
if($this->link instanceof mysqli)
$num_rows = $this->query->num_rows;
else
$num_rows = mysql_num_rows($this->query);
if(!$num_rows)
throw new EmptySetException('You have passed me a query that returns no information');
if($num_rows <= $index)
throw new InvalidQueryRowException('Out of range');
if($this->link instanceof mysqli)
{
$this->query->data_seek($index);
$ret = $this->query->fetch_assoc();
}
else
{
mysql_data_seek($this->query,$index);
$ret = mysql_fetch_assoc($this->query);
}
return $ret;
}
public function link()
{
return $this->link;
}
}
class SqlErrorException extends Exception{}