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:

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';
?>

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 ...

[size=xsmall]Toevoeging op 17/09/2014 17:46:40:[/size]

Overigens is de variabele-naam $stmt binnen de functie totaal niet logisch. Noem dit gewoon $result.
(Je noemt een auto toch ook geen kachelpook?)

[size=xsmall]Toevoeging op 17/09/2014 17:54:54:[/size]

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

[size=xsmall]Toevoeging op 17/09/2014 17:57:53:[/size]

<?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>';
?>
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?
Harry als je enkel wilt weten hoeveel records er in je tabel zitten dan gebruik je onderstaande query:

SELECT COUNT(*) FROM tablename


[size=xsmall]Toevoeging op 17/09/2014 21:53:11:[/size]

Met PDO:
<?php
$sql = "SELECT count(*) FROM tablename";
$result = $db->prepare($sql);
$result->execute();
$number_of_rows = $result->fetchColumn();
echo $number_of_rows;
?>
Ik heb jouw code iets aangepast naar wat ik nodig ben:
<?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/>';
?>

maar dit levert niets op.
Zie ik iets over het hoofd?
<?php
$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?
Ik krijg geen foutmelding maar ook niets op het scherm.
Hieronder de voledige code:
[code]
<?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).
Is deze SQL goed?
SELECT count(*) FROM `2010Combination` WHERE `is_nc` 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:

<?php
try {
$numberNC = $db->query('SELECT count(*) FROM 2010Combination WHERE is_nc LIKE "yes"')->fetchColumn(); 
}
catch(PDOException $e)
{
    echo '<pre>';
    echo 'Foutmelding: '.$e->getMessage();
    echo '</pre>';
}
echo $numberNC.' combinations Non Competing<BR/>';
?>

Voor mij als newbie heb ik hier geen verklaring voor.

Reageren