MySQL "variable" not recognized in array

OK what am I doing wrong:

try
{date_default_timezone_set ("Pacific/Honolulu");
$daynum=array ("s","m","t","w","h","f","a");
$weekday = strftime("%w");
$weekday = $daynum [$weekday];
//$weekday = "w";
$weekday = '%' . $weekday . '%';
//$weekday = "%w%";
echo "$weekday";

$sql = "SELECT id FROM message_instance
            WHERE day LIKE :weekday";

        $s = $pdo->prepare($sql);
        $s->bindValue(':weekday', "$weekday");
        $s->execute();

$result = $s->fetch();

foreach ($result as $row)
{
    $messageids = array(
     'id' => $row['id']); //THIS IS THE ERROR LINE
}

print_r ($messageids);
exit()

I get back:

Warning: Illegal string offset 'id' in C:\\xampp\\htdocs\\...\\gonogo.php on line 28
Warning: Illegal string offset 'id' in C:\\xampp\\htdocs\\...\\gonogo.php on line 28
Array ( [id] => 1 )

The array should contain 1, 4 and 5.

I have used this fetch/foreach array combo in other places without problem (lifted directly from “PHP & MySQL e5”). I have used this in lots of other places without problems. Cannot see any difference but I am doing something wrong. Just cannot see where this is going wrong.

TIA
Steve
PS Also posted at grumpy overflow.

Try var_dump on $row inside the foreach loop. Is that an array? I think the error means PHP is trying to look for an index that isn’t there (after looking it up).

PDO’s fetch method will only grab a single row of the result set each time it’s called. You might want to consider fetchAll() if you want to fetch all of the result set

You might want to build your id array like this.

$messageids = array();
foreach ($result as $row)
{
    $messageids[] = $row['id'];
}

Thank you all - finally to there. TWO errors/confusions.

1)The fetch/fetchall screwed up the $s variable I think and

  1. more basically $messageids needed the otherwise it only stores the last value of the foreach loop. Final code is:

include $_SERVER['DOCUMENT_ROOT'] . '/vvvxxx/includes/helpers.inc.php'; 
include $_SERVER['DOCUMENT_ROOT'] . '/vvvxxx/includes/db.inc.php'; 

try {
date_default_timezone_set ("Pacific/Honolulu");
$daynum=array ("s","m","t","w","h","f","a");
$weekday = strftime("%w");
$weekday = $daynum [$weekday];
$weekday = '%' . $weekday . '%';
echo "$weekday"; 

$sql = "SELECT id FROM message_instance
                WHERE day LIKE :weekday";    
                
            $s = $pdo->prepare($sql);
            $s->bindValue(':weekday', "$weekday");
            $s->execute();

    foreach ($s as $row)
    {
        $messageids [] = array(
         "id" => $row['id']);
     }

  print_r ($messageids);

echo "adios";
exit()

That was an awful lot of work for one building block and I am STILL a little confused on the operation of fetch and fetchall but I HAVE GOT THERE HOORAY and a million thanks.

You should be able to use WHILE to loop through those results.

$messageids = array();
while($row = $s->fetch(PDO::FETCH_ASSOC))
{
    $messageids['id'][] = $row['id'];
}

Or simply without the ‘id’ key.

$messageids = array();
while($row = $s->fetch(PDO::FETCH_ASSOC))
{
    $messageids[] = $row['id'];
}

Depending on what you need to do with the array.

Drummin thanks for that. Have gone back to basics and am spending a few hours on starting PHP arrays right from square ONE! Always a good place to start :slight_smile: