MySQLi
Ik werk voor het eerst met OOP, en ik snap het al een beetje, maar met het volgende kom ik niet verder.
Ik wil dat als ik op een pagina $user->setOnline(); zet, hij de paginanaam, je ID en de tijd opslaat (update als je er al in staat, en insert als je er nog niet in staat).
En nu heb ik ergens gevonden dat als je een function uit een ándere class, in een class wilt gebruiken, je dat zo moet doen: classnaam::functionnaam();
Maar dit werkt niet bij MySQLi..
Hier zit verder geen class mysql { } etc. bij, deze is standaard dacht ik.
En dan heb ik verder deze class: (Er is nog een ander, maar die is hier niet bij nodig)
Code (php)
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
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
class User
{
public function loggedIn()
{
if ($_SESSION['loggedIn'] != true OR $_SESSION['IP'] != $_SERVER['REMOTE_ADDR'])
{
header('Location: ' . config::website() . '/logout');
}
}
public function loggedOut()
{
if ($_SESSION['loggedIn'] == true AND $_SESSION['IP'] == $_SERVER['REMOTE_ADDR'])
{
header('Location: ' . config::website() . '/me');
}
}
public function setOnline()
{
if ($_SESSION['loggedIn'] == true AND $_SESSION['IP'] == $_SERVER['REMOTE_ADDR'])
{
$select = 'SELECT * FROM user_online WHERE userID = ' . $_SESSION['userID'];
$query = mysqli::query($select);
if ($query->num_rows != 1)
{
$update = 'UPDATE user_online SET time = ' . time() . ', pageName = "' . $_SERVER['SCRIPT_NAME'] . '", hour = ' . date('H:i') . ' WHERE userID = ' . $_SESSION['userID'];
$query = mysqli::query($update);
}
else
{
$insert = 'INSERT INTO user_online (userID, pageName, time, hour) VALUES (' . $_SESSION['userID'] . ', "' . $_SERVER['SCRIPT_NAME'] . '", ' . time() . ', ' . date('H:i') . ') ';
$query = mysqli::query($insert);
}
}
}
}
{
public function loggedIn()
{
if ($_SESSION['loggedIn'] != true OR $_SESSION['IP'] != $_SERVER['REMOTE_ADDR'])
{
header('Location: ' . config::website() . '/logout');
}
}
public function loggedOut()
{
if ($_SESSION['loggedIn'] == true AND $_SESSION['IP'] == $_SERVER['REMOTE_ADDR'])
{
header('Location: ' . config::website() . '/me');
}
}
public function setOnline()
{
if ($_SESSION['loggedIn'] == true AND $_SESSION['IP'] == $_SERVER['REMOTE_ADDR'])
{
$select = 'SELECT * FROM user_online WHERE userID = ' . $_SESSION['userID'];
$query = mysqli::query($select);
if ($query->num_rows != 1)
{
$update = 'UPDATE user_online SET time = ' . time() . ', pageName = "' . $_SERVER['SCRIPT_NAME'] . '", hour = ' . date('H:i') . ' WHERE userID = ' . $_SESSION['userID'];
$query = mysqli::query($update);
}
else
{
$insert = 'INSERT INTO user_online (userID, pageName, time, hour) VALUES (' . $_SESSION['userID'] . ', "' . $_SERVER['SCRIPT_NAME'] . '", ' . time() . ', ' . date('H:i') . ') ';
$query = mysqli::query($insert);
}
}
}
}
In de eerste 2 functies staat config::website(), daarmee roep ik dus de functie website aan uit de class config.
Maar wanneer ik dat met MySQLi probeer, krijg ik deze error:
Non-static method mysqli::query() cannot be called statically, assuming $this from incompatible context in /include/classes.inc.php on line 62
Op welke manier moet ik dit oplossen zodat het werkt?
Je zal MySQLi in je class moeten laden (het liefst via de constructor) en vervolgens kun je die de hele tijd aanroepen:
Code (php)
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Tevens zou ik als ik jou was i.p.v. werken met MySQLi gaan werken met PDO, dit werkt stukken beter in OOP. Een goede tutorial: http://phptuts.nl/view/27/
Gewijzigd op 23/11/2011 07:56:19 door Wouter J
Ik heb nu het volgende:
Code (php)
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
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
<?php
class User
{
private $mysqli;
public function __construct( $mysqli )
{
$this->mysqli = $mysqli;
}
public function loggedIn()
{
if ($_SESSION['loggedIn'] != true OR $_SESSION['IP'] != $_SERVER['REMOTE_ADDR'])
{
header('Location: ' . config::website() . '/logout');
}
}
public function loggedOut()
{
if ($_SESSION['loggedIn'] == true AND $_SESSION['IP'] == $_SERVER['REMOTE_ADDR'])
{
header('Location: ' . config::website() . '/me');
}
}
public function setOnline()
{
if ($_SESSION['loggedIn'] == true AND $_SESSION['IP'] == $_SERVER['REMOTE_ADDR'])
{
$select = 'SELECT * FROM user_online WHERE userID = ' . $_SESSION['userID'];
$query = $this->mysqli->query($select);
if ($query->num_rows != 1)
{
$update = 'UPDATE user_online SET time = ' . time() . ', pageName = "' . $_SERVER['SCRIPT_NAME'] . '", hour = ' . date('H:i') . ' WHERE userID = ' . $_SESSION['userID'];
$query = $this->mysqli->query($update);
}
else
{
$insert = 'INSERT INTO user_online (userID, pageName, time, hour) VALUES (' . $_SESSION['userID'] . ', "' . $_SERVER['SCRIPT_NAME'] . '", ' . time() . ', ' . date('H:i') . ') ';
$query = $this->mysqli->query($insert);
}
}
}
}
?>
class User
{
private $mysqli;
public function __construct( $mysqli )
{
$this->mysqli = $mysqli;
}
public function loggedIn()
{
if ($_SESSION['loggedIn'] != true OR $_SESSION['IP'] != $_SERVER['REMOTE_ADDR'])
{
header('Location: ' . config::website() . '/logout');
}
}
public function loggedOut()
{
if ($_SESSION['loggedIn'] == true AND $_SESSION['IP'] == $_SERVER['REMOTE_ADDR'])
{
header('Location: ' . config::website() . '/me');
}
}
public function setOnline()
{
if ($_SESSION['loggedIn'] == true AND $_SESSION['IP'] == $_SERVER['REMOTE_ADDR'])
{
$select = 'SELECT * FROM user_online WHERE userID = ' . $_SESSION['userID'];
$query = $this->mysqli->query($select);
if ($query->num_rows != 1)
{
$update = 'UPDATE user_online SET time = ' . time() . ', pageName = "' . $_SERVER['SCRIPT_NAME'] . '", hour = ' . date('H:i') . ' WHERE userID = ' . $_SESSION['userID'];
$query = $this->mysqli->query($update);
}
else
{
$insert = 'INSERT INTO user_online (userID, pageName, time, hour) VALUES (' . $_SESSION['userID'] . ', "' . $_SERVER['SCRIPT_NAME'] . '", ' . time() . ', ' . date('H:i') . ') ';
$query = $this->mysqli->query($insert);
}
}
}
}
?>
Maar dan krijg ik deze error:
Warning: Missing argument 1 for User::__construct(), called in /include/classes.inc.php on line 86 and defined in /include/classes.inc.php on line 43
Fatal error: Call to a member function query() on a non-object in /include/classes.inc.php on line 69
Gewijzigd op 23/11/2011 08:54:40 door Lord Gaga
of een instantie die je al eerder gebruikte, bijvoorbeeld:
Code (php)
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
<?php
$mysqli = new mysqli('host', 'name', 'pass', 'database');
/*
* Andere code
*/
$user = new User($mysqli);
?>
$mysqli = new mysqli('host', 'name', 'pass', 'database');
/*
* Andere code
*/
$user = new User($mysqli);
?>
edit: puntkomma vergeten
Gewijzigd op 23/11/2011 09:11:17 door Victor -
Code (php)
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
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
<?php
class User
{
protected $mysqli;
public function __construct(mysqli $mysqli)
{
$this->mysqli = $mysqli;
}
public function loggedIn()
{
if ($_SESSION['loggedIn'] != true OR $_SESSION['IP'] != $_SERVER['REMOTE_ADDR'])
{
header('Location: ' . config::website() . '/logout');
}
}
public function loggedOut()
{
if ($_SESSION['loggedIn'] == true AND $_SESSION['IP'] == $_SERVER['REMOTE_ADDR'])
{
header('Location: ' . config::website() . '/me');
}
}
public function setOnline()
{
if ($_SESSION['loggedIn'] == true AND $_SESSION['IP'] == $_SERVER['REMOTE_ADDR'])
{
$select = 'SELECT * FROM user_online WHERE userID = ' . $_SESSION['userID'];
$query = $this->mysqli->query($select);
if ($query->num_rows == 1)
{
$update = 'UPDATE user_online SET time = ' . time() . ', pageName = "' . $_SERVER['SCRIPT_NAME'] . '", hour = ' . date('H:i') . ' WHERE userID = ' . $_SESSION['userID'];
$query = $this->mysqli->query($update);
}
else
{
$insert = 'INSERT INTO user_online (userID, pageName, time, hour) VALUES (' . $_SESSION['userID'] . ', "' . $_SERVER['SCRIPT_NAME'] . '", ' . time() . ', ' . date('H:i') . ') ';
$query = $this->mysqli->query($insert);
}
}
}
}
$config = new Config();
$user = new User($mysqli);
?>
class User
{
protected $mysqli;
public function __construct(mysqli $mysqli)
{
$this->mysqli = $mysqli;
}
public function loggedIn()
{
if ($_SESSION['loggedIn'] != true OR $_SESSION['IP'] != $_SERVER['REMOTE_ADDR'])
{
header('Location: ' . config::website() . '/logout');
}
}
public function loggedOut()
{
if ($_SESSION['loggedIn'] == true AND $_SESSION['IP'] == $_SERVER['REMOTE_ADDR'])
{
header('Location: ' . config::website() . '/me');
}
}
public function setOnline()
{
if ($_SESSION['loggedIn'] == true AND $_SESSION['IP'] == $_SERVER['REMOTE_ADDR'])
{
$select = 'SELECT * FROM user_online WHERE userID = ' . $_SESSION['userID'];
$query = $this->mysqli->query($select);
if ($query->num_rows == 1)
{
$update = 'UPDATE user_online SET time = ' . time() . ', pageName = "' . $_SERVER['SCRIPT_NAME'] . '", hour = ' . date('H:i') . ' WHERE userID = ' . $_SESSION['userID'];
$query = $this->mysqli->query($update);
}
else
{
$insert = 'INSERT INTO user_online (userID, pageName, time, hour) VALUES (' . $_SESSION['userID'] . ', "' . $_SERVER['SCRIPT_NAME'] . '", ' . time() . ', ' . date('H:i') . ') ';
$query = $this->mysqli->query($insert);
}
}
}
}
$config = new Config();
$user = new User($mysqli);
?>
$mysqli word op config.inc.php aangemaakt, en die staat samen met deze pagina geinclude op me.php
Gewijzigd op 23/11/2011 09:22:30 door Lord Gaga
Quote:
public function __construct(mysqli $mysqli)
Toevoeging op 23/11/2011 09:31:17:
verander dat eens in:
Toevoeging op 23/11/2011 09:31:17:
verander dat eens in:
krijg ik wel een juist resultaat (1 als ik iets in de tabel zet, en 0 als ik het er weer uithaal)
Aan de MySQLi verbinding ligt het dus niet meer denk ik..
Code (php)
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
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
<?php
class User
{
protected $mysqli;
public function __construct($mysqli)
{
$this->mysqli = $mysqli;
}
public function loggedIn()
{
if ($_SESSION['loggedIn'] != true OR $_SESSION['IP'] != $_SERVER['REMOTE_ADDR'])
{
header('Location: ' . config::website() . '/logout');
}
}
public function loggedOut()
{
if ($_SESSION['loggedIn'] == true AND $_SESSION['IP'] == $_SERVER['REMOTE_ADDR'])
{
header('Location: ' . config::website() . '/me');
}
}
public function setOnline()
{
if ($_SESSION['loggedIn'] == true AND $_SESSION['IP'] == $_SERVER['REMOTE_ADDR'])
{
$select = 'SELECT * FROM user_online WHERE userID = ' . $_SESSION['userID'];
$query = $this->mysqli->query($select);
if ($query->num_rows == 1)
{
$update = 'UPDATE user_online SET time = "' . time() . '", pageName = "' . $_SERVER['SCRIPT_NAME'] . '", hour = "' . date('H:i') . '" WHERE userID = ' . $_SESSION['userID'];
$this->mysqli->query($update);
}
else
{
$insert = 'INSERT INTO user_online (userID, pageName, time, hour) VALUES (' .$_SESSION['userID'] . ', "' . $_SERVER['SCRIPT_NAME'] . '", "' . time() . '", "' . date('H:i') . '") ';
$this->mysqli->query($insert);
}
}
}
}
$config = new Config();
$user = new User($mysqli);
?>
class User
{
protected $mysqli;
public function __construct($mysqli)
{
$this->mysqli = $mysqli;
}
public function loggedIn()
{
if ($_SESSION['loggedIn'] != true OR $_SESSION['IP'] != $_SERVER['REMOTE_ADDR'])
{
header('Location: ' . config::website() . '/logout');
}
}
public function loggedOut()
{
if ($_SESSION['loggedIn'] == true AND $_SESSION['IP'] == $_SERVER['REMOTE_ADDR'])
{
header('Location: ' . config::website() . '/me');
}
}
public function setOnline()
{
if ($_SESSION['loggedIn'] == true AND $_SESSION['IP'] == $_SERVER['REMOTE_ADDR'])
{
$select = 'SELECT * FROM user_online WHERE userID = ' . $_SESSION['userID'];
$query = $this->mysqli->query($select);
if ($query->num_rows == 1)
{
$update = 'UPDATE user_online SET time = "' . time() . '", pageName = "' . $_SERVER['SCRIPT_NAME'] . '", hour = "' . date('H:i') . '" WHERE userID = ' . $_SESSION['userID'];
$this->mysqli->query($update);
}
else
{
$insert = 'INSERT INTO user_online (userID, pageName, time, hour) VALUES (' .$_SESSION['userID'] . ', "' . $_SERVER['SCRIPT_NAME'] . '", "' . time() . '", "' . date('H:i') . '") ';
$this->mysqli->query($insert);
}
}
}
}
$config = new Config();
$user = new User($mysqli);
?>
Victor - op 23/11/2011 09:30:50:
Dit is juist goed. Hiermee geef je aan de $mysqli alleen een instance mag zijn van de class MySQLi. En dat moet ook, want als je de constructor een pdo instance meegeeft werkt het niet. Dus forceer altijd welk database type je wilt hebben in een class en hiermee zorg je dat je een fout krijgt bij een andere instance.