Vraagje van een PDO newbie
Ik probeer mijn oude scripts om te zetten naar PDO.
Hiervoor wel wat oefenen en direct gaat het fout.
Kan iemend mij vertellen wat ik in onderstaande code fout doe:
Ik krijg deze foutmeldingen:
Notice: Undefined variable: stmt in /home/harry-arends.nl/public_html/event/SetCompNum.php on line 25
Fatal error: Call to a member function rowCount() on a non-object in /home/harry-arends.nl/public_html/event/SetCompNum.php on line 25
Hiervoor wel wat oefenen en direct gaat het fout.
Kan iemend mij vertellen wat ik in onderstaande code fout doe:
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
function getAllData($db) {
$stmt = $db->query("
SELECT person.*, horse.*, 2010Combination.*
FROM FEIPerson AS person
INNER JOIN 2010Combination ON person.fei_id = 2010Combination.personFEIid
INNER JOIN FEIHorse horse ON horse.fei_id = 2010Combination.horseFEIid
ORDER BY person.competing_for_country, horse.complete_name ASC
");
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
$db = new PDO('mysql:host=localhost;dbname='.$MySqlDatabase.';charset=utf8', $MySqlUsername, $MySqlPassword);
try {
getAllData($db);
} catch(PDOException $ex) {
echo "An Error occurred!"; //user friendly message
}
$row_count = $stmt->rowCount();
echo $row_count.' rows selected';
?>
$stmt = $db->query("
SELECT person.*, horse.*, 2010Combination.*
FROM FEIPerson AS person
INNER JOIN 2010Combination ON person.fei_id = 2010Combination.personFEIid
INNER JOIN FEIHorse horse ON horse.fei_id = 2010Combination.horseFEIid
ORDER BY person.competing_for_country, horse.complete_name ASC
");
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
$db = new PDO('mysql:host=localhost;dbname='.$MySqlDatabase.';charset=utf8', $MySqlUsername, $MySqlPassword);
try {
getAllData($db);
} catch(PDOException $ex) {
echo "An Error occurred!"; //user friendly message
}
$row_count = $stmt->rowCount();
echo $row_count.' rows selected';
?>
Ik krijg deze foutmeldingen:
Notice: Undefined variable: stmt in /home/harry-arends.nl/public_html/event/SetCompNum.php on line 25
Fatal error: Call to a member function rowCount() on a non-object in /home/harry-arends.nl/public_html/event/SetCompNum.php on line 25
Op regel 2 declareer je de variabele $stmt en kent aan deze een waarde toe. Dit doe je binnen een functie. Alle variabelen die binnen een functie gedeclareerd worden zijn alleen zichtbaar/bruikbaar binnen diezelfde functie.
Op regel 21 gebruik je wederom de variabele $stmt buiten de functie om en dat gaat niet ...
Toevoeging op 17/09/2014 17:46:40:
Overigens is de variabele-naam $stmt binnen de functie totaal niet logisch. Noem dit gewoon $result.
(Je noemt een auto toch ook geen kachelpook?)
Toevoeging op 17/09/2014 17:54:54:
Op regel 16 vergeet je de array die getAllData() teruggeeft aan een variabele toe te kennen.
Op regel 21 kun je gewoon count() gebruiken omdat fetchAll een array teruggeeft TENZIJ er fout optreedt. Dan geeft FetchAll false terug
Toevoeging op 17/09/2014 17:57:53:
Op regel 21 gebruik je wederom de variabele $stmt buiten de functie om en dat gaat niet ...
Toevoeging op 17/09/2014 17:46:40:
Overigens is de variabele-naam $stmt binnen de functie totaal niet logisch. Noem dit gewoon $result.
(Je noemt een auto toch ook geen kachelpook?)
Toevoeging op 17/09/2014 17:54:54:
Op regel 16 vergeet je de array die getAllData() teruggeeft aan een variabele toe te kennen.
Op regel 21 kun je gewoon count() gebruiken omdat fetchAll een array teruggeeft TENZIJ er fout optreedt. Dan geeft FetchAll false terug
Toevoeging op 17/09/2014 17:57:53:
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
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
<?php
function getAllData($db) {
$result = $db->query("
SELECT person.*, horse.*, 2010Combination.*
FROM FEIPerson AS person
INNER JOIN 2010Combination ON person.fei_id = 2010Combination.personFEIid
INNER JOIN FEIHorse horse ON horse.fei_id = 2010Combination.horseFEIid
ORDER BY person.competing_for_country, horse.complete_name ASC
");
return $result->fetchAll(PDO::FETCH_ASSOC);
}
$db = new PDO('mysql:host=localhost;dbname='.$MySqlDatabase.';charset=utf8', $MySqlUsername, $MySqlPassword);
try {
$arr = getAllData($db);
} catch(PDOException $ex) {
echo "An Error occurred!"; //user friendly message
}
$row_count = count($arr);
echo $row_count.' rows selected';
echo 'Inhoud van arr:<pre>';
print_r($arr);
echo '</pre>';
?>
function getAllData($db) {
$result = $db->query("
SELECT person.*, horse.*, 2010Combination.*
FROM FEIPerson AS person
INNER JOIN 2010Combination ON person.fei_id = 2010Combination.personFEIid
INNER JOIN FEIHorse horse ON horse.fei_id = 2010Combination.horseFEIid
ORDER BY person.competing_for_country, horse.complete_name ASC
");
return $result->fetchAll(PDO::FETCH_ASSOC);
}
$db = new PDO('mysql:host=localhost;dbname='.$MySqlDatabase.';charset=utf8', $MySqlUsername, $MySqlPassword);
try {
$arr = getAllData($db);
} catch(PDOException $ex) {
echo "An Error occurred!"; //user friendly message
}
$row_count = count($arr);
echo $row_count.' rows selected';
echo 'Inhoud van arr:<pre>';
print_r($arr);
echo '</pre>';
?>
Gewijzigd op 17/09/2014 17:44:03 door Frank Nietbelangrijk
Bedankt Frank, maar is er geen eenvoudigere wijze om het aantal records te tellen.
Of moet ik deze weg bewandelen en dan direct de array te verwijderen?
Of moet ik deze weg bewandelen en dan direct de array te verwijderen?
Harry als je enkel wilt weten hoeveel records er in je tabel zitten dan gebruik je onderstaande query:
Toevoeging op 17/09/2014 21:53:11:
Met PDO:
Toevoeging op 17/09/2014 21:53:11:
Met PDO:
Code (php)
1
2
3
4
5
6
7
2
3
4
5
6
7
<?php
$sql = "SELECT count(*) FROM tablename";
$result = $db->prepare($sql);
$result->execute();
$number_of_rows = $result->fetchColumn();
echo $number_of_rows;
?>
$sql = "SELECT count(*) FROM tablename";
$result = $db->prepare($sql);
$result->execute();
$number_of_rows = $result->fetchColumn();
echo $number_of_rows;
?>
Gewijzigd op 17/09/2014 21:53:56 door Frank Nietbelangrijk
Ik heb jouw code iets aangepast naar wat ik nodig ben:
maar dit levert niets op.
Zie ik iets over het hoofd?
Code (php)
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
<?php
$sqlNC = "
SELECT count(*)
FROM `2010Combination`
WHERE `is_nc` LIKE 'Yes'
");
$resultNC = $db->prepare($sqlNC);
$resultNC->execute();
$numberNC = $resultNC->fetchColumn();
echo $numberNC.' combinations Non Competing<BR/>';
?>
$sqlNC = "
SELECT count(*)
FROM `2010Combination`
WHERE `is_nc` LIKE 'Yes'
");
$resultNC = $db->prepare($sqlNC);
$resultNC->execute();
$numberNC = $resultNC->fetchColumn();
echo $numberNC.' combinations Non Competing<BR/>';
?>
maar dit levert niets op.
Zie ik iets over het hoofd?
Gewijzigd op 17/09/2014 23:50:05 door Harry H Arends
Code (php)
1
2
3
2
3
<?php
$db = new PDO('mysql:host=localhost;dbname='.$MySqlDatabase.';charset=utf8', $MySqlUsername, $MySqlPassword);
?>
$db = new PDO('mysql:host=localhost;dbname='.$MySqlDatabase.';charset=utf8', $MySqlUsername, $MySqlPassword);
?>
moet er nog wel even bij natuurlijk en de variabelen in die regel moet je dan dus ook aangemaakt hebben.
Krijg je geen foutmeldingen te zien?
Gewijzigd op 18/09/2014 00:05:47 door Frank Nietbelangrijk
Ik krijg geen foutmelding maar ook niets op het scherm.
Hieronder de voledige code:
Hieronder de voledige code:
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
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
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
function getAllCombinations($db) {
$result = $db->query("
SELECT person.*, horse.*, 2010Combination.*
FROM FEIPerson AS person
INNER JOIN 2010Combination ON person.fei_id = 2010Combination.personFEIid
INNER JOIN FEIHorse horse ON horse.fei_id = 2010Combination.horseFEIid
WHERE `is_nc` LIKE 'no'
AND `is_waitingList` LIKE 'no'
ORDER BY person.competing_for_country, horse.complete_name ASC
");
return $result->fetchAll(PDO::FETCH_ASSOC);
}
function getAllWL($db) {
$result = $db->query("
SELECT *
FROM `2010Combination`
WHERE `is_waitingList` LIKE 'Yes'
");
return $result->fetchAll(PDO::FETCH_ASSOC);
}
$db = new PDO('mysql:host=localhost;dbname='.$MySqlDatabase.';charset=utf8', $MySqlUsername, $MySqlPassword);
try {
$arrAll = getAllCombinations($db);
} catch(PDOException $ex) {
echo '<pre>';
echo 'Foutmelding: '.$e->getMessage();
echo '</pre>';
}
$row_count_all = count($arrAll);
echo $row_count_all.' combinations on file<BR/>';
try {
$arrWL = getAllWL($db);
} catch(PDOException $ex) {
echo '<pre>';
echo 'Foutmelding: '.$e->getMessage();
echo '</pre>';
}
$row_count_wl = count($arrWL);
echo $row_count_wl.' combinations on Waiting-list<BR/>';
try {
$sqlNC = " SELECT count(*) FROM `2010Combination` WHERE `is_nc` LIKE 'Yes' ");
$resultNC = $db->prepare($sqlNC);
$resultNC->execute();
$numberNC = $resultNC->fetchColumn();
}
catch(PDOException $e)
{
echo '<pre>';
echo 'Foutmelding: '.$e->getMessage();
echo '</pre>';
}
echo $numberNC.' combinations Non Competing<BR/>';
?>
Haal ik het laatste stuk er uit worden de eerste twee wel uitgevoerd.
<?php
try {
// $sqlNC = "SELECT count(*) FROM `2010Combination` WHERE `is_nc` LIKE 'Yes' ");
// $resultNC = $db->prepare($sqlNC);
// $resultNC->execute();
// $numberNC = $resultNC->fetchColumn();
}
catch(PDOException $e)
{
echo '<pre>';
echo 'Foutmelding: '.$e->getMessage();
echo '</pre>';
}
echo $numberNC.' combinations Non Competing<BR/>';
?>
Krijg dan een foutmelding dat $numberNC ontbreekt.
Op het moment dat ik $sqlNC vul gaat het fout, vreemd (voor mij dan).
error_reporting(E_ALL);
ini_set("display_errors", 1);
function getAllCombinations($db) {
$result = $db->query("
SELECT person.*, horse.*, 2010Combination.*
FROM FEIPerson AS person
INNER JOIN 2010Combination ON person.fei_id = 2010Combination.personFEIid
INNER JOIN FEIHorse horse ON horse.fei_id = 2010Combination.horseFEIid
WHERE `is_nc` LIKE 'no'
AND `is_waitingList` LIKE 'no'
ORDER BY person.competing_for_country, horse.complete_name ASC
");
return $result->fetchAll(PDO::FETCH_ASSOC);
}
function getAllWL($db) {
$result = $db->query("
SELECT *
FROM `2010Combination`
WHERE `is_waitingList` LIKE 'Yes'
");
return $result->fetchAll(PDO::FETCH_ASSOC);
}
$db = new PDO('mysql:host=localhost;dbname='.$MySqlDatabase.';charset=utf8', $MySqlUsername, $MySqlPassword);
try {
$arrAll = getAllCombinations($db);
} catch(PDOException $ex) {
echo '<pre>';
echo 'Foutmelding: '.$e->getMessage();
echo '</pre>';
}
$row_count_all = count($arrAll);
echo $row_count_all.' combinations on file<BR/>';
try {
$arrWL = getAllWL($db);
} catch(PDOException $ex) {
echo '<pre>';
echo 'Foutmelding: '.$e->getMessage();
echo '</pre>';
}
$row_count_wl = count($arrWL);
echo $row_count_wl.' combinations on Waiting-list<BR/>';
try {
$sqlNC = " SELECT count(*) FROM `2010Combination` WHERE `is_nc` LIKE 'Yes' ");
$resultNC = $db->prepare($sqlNC);
$resultNC->execute();
$numberNC = $resultNC->fetchColumn();
}
catch(PDOException $e)
{
echo '<pre>';
echo 'Foutmelding: '.$e->getMessage();
echo '</pre>';
}
echo $numberNC.' combinations Non Competing<BR/>';
?>
Haal ik het laatste stuk er uit worden de eerste twee wel uitgevoerd.
<?php
try {
// $sqlNC = "SELECT count(*) FROM `2010Combination` WHERE `is_nc` LIKE 'Yes' ");
// $resultNC = $db->prepare($sqlNC);
// $resultNC->execute();
// $numberNC = $resultNC->fetchColumn();
}
catch(PDOException $e)
{
echo '<pre>';
echo 'Foutmelding: '.$e->getMessage();
echo '</pre>';
}
echo $numberNC.' combinations Non Competing<BR/>';
?>
Krijg dan een foutmelding dat $numberNC ontbreekt.
Op het moment dat ik $sqlNC vul gaat het fout, vreemd (voor mij dan).
Gewijzigd op 18/09/2014 12:20:49 door Harry H Arends
Is deze SQL goed?
Omdat je LIKE gebruikt zou ik iets verwachten als LIKE '%Yes%'.
Omdat je LIKE gebruikt zou ik iets verwachten als LIKE '%Yes%'.
Rechtstreeks uitgevoerd op de server met PHPMyAdim werkt het goed.
LIKE mag gewoon = zijn
@Ivo: dat kan niet want ik wil Yes en yes afvangen.
Heb even wat verder gekeken in de lunchpauze en dit werkt wel:
Voor mij als newbie heb ik hier geen verklaring voor.
Heb even wat verder gekeken in de lunchpauze en dit werkt wel:
Code (php)
Voor mij als newbie heb ik hier geen verklaring voor.
Als je is_nc een character column is dan zou UPPER(is_nc) = "YES" voor Yes, YES, yes, yEs enz werken.
Als het een binary is, dan moet je wat extras doen.
Wil je echt op Yes of yes testen dan kan je dit gebruiken: is_nc in ("Yes", "yes")
Als het een binary is, dan moet je wat extras doen.
Wil je echt op Yes of yes testen dan kan je dit gebruiken: is_nc in ("Yes", "yes")
vraag is, hoe je dan ooit die varianten in hooDFletTers voor elkaar hebt gekregen.
Wel een argument om dus voortaan gewoon een 0 of 1 te gebruiken voor dit soort situaties (of een boolean.)
Wel een argument om dus voortaan gewoon een 0 of 1 te gebruiken voor dit soort situaties (of een boolean.)
Maar dat is NIET het probleem waar ik het over heb.
Verder bedankt voor de input
Verder bedankt voor de input




