This question already has an answer here:
I need help with prevent from "Notice: Array to string conversion in "
$stmt = $DBH->prepare("SELECT * FROM users WHERE id = :emailid");
$stmt->bindParam(':emailid', $emailid, PDO::PARAM_STR);
$stmt->execute();
$result = $stmt->fetchAll();
foreach($result as $row)
{
echo $row['id'].'<br />';
echo $row['emailid'].'<br />';
echo $row['password'];
}
Everythink its OK, index.php?id=5
but when i put index.php?id[]=5
Return me error
Notice: Array to string conversion in
What is the best way to check for arrays in this case; error_reporting(0)
is not a solution I want to find optimized method.
</div>
Not sure in which direction you want this question to go. But let's assume you want - for some reason - make it work either way: ?id=...
and ?id[]=...&id[]=...
if ( !is_array($_GET['id']) ) {
// just make it an array with one element
$ids = array( $_GET['id'] );
// so having a simple type string id is just a special case of the
// array case
}
else {
$ids = $_GET['id'];
}
$stmt = $DBH->prepare("SELECT * FROM users WHERE id = :emailid");
$stmt->bindParam(':emailid', $emailid, PDO::PARAM_STR);
foreach( $ids as $emailid ) {
$stmt->execute();
$result = $stmt->fetchAll();
foreach($result as $row) {
...
}
}