PDO quote() werkt niet
Ik heb hier 2 verschillende klasse.
In mijn 2e class wil ik gebruik maken van een quote(), maar hij geeft een foutmelding.
Weer iemand hoe ik dit kan oplossen?
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
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
80
81
82
83
84
85
86
87
88
89
90
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
80
81
82
83
84
85
86
87
88
89
90
<?php
class database{
var $dbh;
function connect(){
$hostname = "";
$database = "";
$username = "";
$password = "";
try {
$this->dbh = new PDO("mysql:host=".$hostname.";dbname=".$database.";",$username, $password);
$this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
}
catch(PDOException $e){
echo $e->getMessage();
}
}
function dbselect($sql){
$result = $this->dbh->query($sql);
if($result->rowCount() <= 1)
{
$result = $result->fetch(PDO::FETCH_ASSOC);
}
elseif($result->rowCount() > 1)
{
$result = $result->fetchall(PDO::FETCH_ASSOC);
}
return $result;
}
function dbselect_row($sql){
$result = $this->dbh->query($sql)->rowCount();
return $result;
}
function dbquery($sql){
$result = $this->dbh->exec($sql);
}
}
class messaging extends database{
function check($check){ // titel beveiligen
$check = $this->dbh->quote($check);
return $check;
}
function addfriend($naam){
$member = $this->dbselect_row("SELECT id FROM members WHERE username='".$this->check($naam)."'");
if($member == 1){
$members = $this->dbselect("SELECT id FROM members WHERE username='".$this->check($naam)."'");
$this->dbquery("INSERT INTO vrienden (user_id1,user_id2,datum) VALUES (".$gebruiker['id'].",".$members['id'].",NOW())");
echo "<script type=\"text/javascript\">reports('".$text->addedfriend."','green');</script>";
}
else{
echo "<script type=\"text/javascript\">reports('".$text->usernotfound."','red');</script>";
}
}
function removefriend($naam){
$naam = $this->check($naam);
$member = $this->dbselect_row("SELECT id FROM members WHERE username='".$naam."'");
if($member == 1){
$members = $this->dbselect("SELECT id FROM members WHERE username='".$this->check($naam)."'");
$this->dbquery("DELETE FROM vrienden WHERE user_id1 = ".$gebruiker['id']." AND user_id2 = ".$members['id']." OR user_id2 = ".$gebruiker['id']." AND user_id1 = ".$members['id']."");
echo "<script type=\"text/javascript\">reports('".$text->removedfriend."','green');</script>";
}
else{
echo "<script type=\"text/javascript\">reports('".$text->usernotfound."','red');</script>";
}
}
}
?>
class database{
var $dbh;
function connect(){
$hostname = "";
$database = "";
$username = "";
$password = "";
try {
$this->dbh = new PDO("mysql:host=".$hostname.";dbname=".$database.";",$username, $password);
$this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
}
catch(PDOException $e){
echo $e->getMessage();
}
}
function dbselect($sql){
$result = $this->dbh->query($sql);
if($result->rowCount() <= 1)
{
$result = $result->fetch(PDO::FETCH_ASSOC);
}
elseif($result->rowCount() > 1)
{
$result = $result->fetchall(PDO::FETCH_ASSOC);
}
return $result;
}
function dbselect_row($sql){
$result = $this->dbh->query($sql)->rowCount();
return $result;
}
function dbquery($sql){
$result = $this->dbh->exec($sql);
}
}
class messaging extends database{
function check($check){ // titel beveiligen
$check = $this->dbh->quote($check);
return $check;
}
function addfriend($naam){
$member = $this->dbselect_row("SELECT id FROM members WHERE username='".$this->check($naam)."'");
if($member == 1){
$members = $this->dbselect("SELECT id FROM members WHERE username='".$this->check($naam)."'");
$this->dbquery("INSERT INTO vrienden (user_id1,user_id2,datum) VALUES (".$gebruiker['id'].",".$members['id'].",NOW())");
echo "<script type=\"text/javascript\">reports('".$text->addedfriend."','green');</script>";
}
else{
echo "<script type=\"text/javascript\">reports('".$text->usernotfound."','red');</script>";
}
}
function removefriend($naam){
$naam = $this->check($naam);
$member = $this->dbselect_row("SELECT id FROM members WHERE username='".$naam."'");
if($member == 1){
$members = $this->dbselect("SELECT id FROM members WHERE username='".$this->check($naam)."'");
$this->dbquery("DELETE FROM vrienden WHERE user_id1 = ".$gebruiker['id']." AND user_id2 = ".$members['id']." OR user_id2 = ".$gebruiker['id']." AND user_id1 = ".$members['id']."");
echo "<script type=\"text/javascript\">reports('".$text->removedfriend."','green');</script>";
}
else{
echo "<script type=\"text/javascript\">reports('".$text->usernotfound."','red');</script>";
}
}
}
?>
Quote:
Fatal error: Call to a member function quote() on a non-object in /home/www/xxx/class.messaging.php on line 59
regel 59 is de eerste regel in de functie check()
regel 59 is de eerste regel in de functie check()
Gewijzigd op 01/01/1970 01:00:00 door Yearupie Achternaamloos
SanThe.
SanThe schreef op 22.12.2009 17:18:
Haal de vraag niet weg. Nu is het topic totaal nutteloos geworden.
SanThe.
SanThe.
Ja sorry, ik heb nu al een andere vraag dus ik heb dit topic gebruikt.
PS: Als je objecten wilt gebruiken zoals ze bedoeld zijn, dan hoort je messaging class niet je database class over te erven. Je messaging class maakt gebruik van de database, het is niet een database. Dat is het verschil tussen objecten in properties te gebruiken (zoals je doet met PDO binnen database) en overerven. Overerven doe je als de nieuwe class een specifiekere versie is van de overgeërfde class. Compositie doe je wanneer de ene class gewoon gebruik wil maken van een andere class.
$db->connect()
$pm = new pm;
Ik gebruikte eerst bv $db->dbquery() in me messaging class,
maar dat herkende hij niet.
bij $pm roep je niet nog eerst connect() aan, waardoor $this->dbh leeg is, en $this->dbh->quote niet geldig is.
Maar ik krijg een error bij devolgende functie:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php
function removefriend($naam){
$naam = $this->check($naam);
$member = $this->dbselect_row("SELECT id FROM members WHERE username='".$naam."'");
if($member == 1){
$members = $this->dbselect("SELECT id FROM members WHERE username=".$this->check($naam));
$this->dbquery("DELETE FROM vrienden WHERE user_id1 = ".$gebruiker['id']." AND user_id2 = ".$members['id']." OR user_id2 = ".$gebruiker['id']." AND user_id1 = ".$members['id']);
echo "<script type=\"text/javascript\">reports('".$text->removedfriend."','green');</script>";
}
else{
echo "<script type=\"text/javascript\">reports('".$text->usernotfound."','red');</script>";
}
}
?>
function removefriend($naam){
$naam = $this->check($naam);
$member = $this->dbselect_row("SELECT id FROM members WHERE username='".$naam."'");
if($member == 1){
$members = $this->dbselect("SELECT id FROM members WHERE username=".$this->check($naam));
$this->dbquery("DELETE FROM vrienden WHERE user_id1 = ".$gebruiker['id']." AND user_id2 = ".$members['id']." OR user_id2 = ".$gebruiker['id']." AND user_id1 = ".$members['id']);
echo "<script type=\"text/javascript\">reports('".$text->removedfriend."','green');</script>";
}
else{
echo "<script type=\"text/javascript\">reports('".$text->usernotfound."','red');</script>";
}
}
?>
Gewijzigd op 01/01/1970 01:00:00 door Yearupie Achternaamloos
Je hebt $naam 2 keer door check() heen gehaald bij die dbquery() aanroep.
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php
function removefriend($naam){
$naam = $this->check($naam);
$member = $this->dbselect_row("SELECT id FROM members WHERE username='".$naam."'");
if($member == 1){
$members = $this->dbselect("SELECT id FROM members WHERE username=".$naam);
$this->dbquery("DELETE FROM vrienden WHERE user_id1 = ".$gebruiker['id']." AND user_id2 = ".$members['id']." OR user_id2 = ".$gebruiker['id']." AND user_id1 = ".$members['id']);
echo "<script type=\"text/javascript\">reports('".$text->removedfriend."','green');</script>";
}
else{
echo "<script type=\"text/javascript\">reports('".$text->usernotfound."','red');</script>";
}
}
?>
function removefriend($naam){
$naam = $this->check($naam);
$member = $this->dbselect_row("SELECT id FROM members WHERE username='".$naam."'");
if($member == 1){
$members = $this->dbselect("SELECT id FROM members WHERE username=".$naam);
$this->dbquery("DELETE FROM vrienden WHERE user_id1 = ".$gebruiker['id']." AND user_id2 = ".$members['id']." OR user_id2 = ".$gebruiker['id']." AND user_id1 = ".$members['id']);
echo "<script type=\"text/javascript\">reports('".$text->removedfriend."','green');</script>";
}
else{
echo "<script type=\"text/javascript\">reports('".$text->usernotfound."','red');</script>";
}
}
?>
Quote:
Warning: PDO::query() [function.PDO-query]: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'hansje''' at line 1 in /home/www/xxx/class.database.php on line 39
Fatal error: Call to a member function rowCount() on a non-object in /home/www/xxx/class.database.php on line 39
Fatal error: Call to a member function rowCount() on a non-object in /home/www/xxx/class.database.php on line 39
Gewijzigd op 01/01/1970 01:00:00 door Yearupie Achternaamloos
Hmm. Kan je eens controleren of quote() ook de begin- en eindquote die om een string heen staan erbij zet? Volgens mij wel. En als dat het geval is, dan moeten die enkele quotes bij je eerste query eruit.
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php
function removefriend($naam){
$naam = $this->check($naam);
$member = $this->dbselect_row("SELECT id FROM members WHERE username=".$naam);
if($member == 1){
$members = $this->dbselect("SELECT id FROM members WHERE username=".$naam);
$this->dbquery("DELETE FROM vrienden WHERE user_id1 = ".$this->gebruiker['id']." AND user_id2 = ".$members['id']." OR user_id2 = ".$this->gebruiker['id']." AND user_id1 = ".$members['id']);
echo "<script type=\"text/javascript\">reports('".$text->removedfriend."','green');</script>";
}
else{
echo "<script type=\"text/javascript\">reports('".$text->usernotfound."','red');</script>";
}
}
?>
function removefriend($naam){
$naam = $this->check($naam);
$member = $this->dbselect_row("SELECT id FROM members WHERE username=".$naam);
if($member == 1){
$members = $this->dbselect("SELECT id FROM members WHERE username=".$naam);
$this->dbquery("DELETE FROM vrienden WHERE user_id1 = ".$this->gebruiker['id']." AND user_id2 = ".$members['id']." OR user_id2 = ".$this->gebruiker['id']." AND user_id1 = ".$members['id']);
echo "<script type=\"text/javascript\">reports('".$text->removedfriend."','green');</script>";
}
else{
echo "<script type=\"text/javascript\">reports('".$text->usernotfound."','red');</script>";
}
}
?>
Ik heb nu dit, en nu werkt $gebruiker en $text niet.
Ze worden wel allebei voor de class gezet, maar dus wel erbuiten.
Hoe krijg ik deze erin?
Weet iemand de oplossing?
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php
class jouwKlasse {
private $gebruiker;
private $text;
public function __construct($gebruiker, $text) {
$this->gebruiker = $gebruiker;
$this->text = $text;
}
public function removeFriend($naam) {
// Gebruik nu $this->gebruiker en $this->text
}
}
// Procedurele code waarbij $gebruiker en $text waarden hebben:
$obj = new jouwKlasse($gebruiker, $text);
?>
class jouwKlasse {
private $gebruiker;
private $text;
public function __construct($gebruiker, $text) {
$this->gebruiker = $gebruiker;
$this->text = $text;
}
public function removeFriend($naam) {
// Gebruik nu $this->gebruiker en $this->text
}
}
// Procedurele code waarbij $gebruiker en $text waarden hebben:
$obj = new jouwKlasse($gebruiker, $text);
?>
Bedankt het werkt :)
Heb je al wat tutorials over OOP een PHP5 gelezen? De code die jij gebruikt is nog PHP4 en zou je dus moeten (willen) veranderen...