Home » Php » How to insert data into array in PHP prepare statment?

How to insert data into array in PHP prepare statment?

Posted by: admin October 20, 2018 Leave a comment

Questions:

I am trying to insert data into array before using it using prepare statement pdo.

Here is what I have done :

$stmt = $pdo->prepare("SELECT * FROM settings"); 
$stmt->execute(array());
$row = $stmt->fetch(PDO::FETCH_ASSOC);
                    htmlspecialchars($row['site_name']);
                    htmlspecialchars($row['site_desc']);
                    htmlspecialchars($row['site_url']);
unset($stmt);

Usaqe :

$row['site_name'];

It works fine, but I feel like I am missing an importand thing.

Here is my question:
Do I have to declare all values in $stmt->execute(array());
Like so:$stmt->execute(array(':site_name'=>$site_name, ':site_name'=>$site_name,)); before using it ?

And how to do like this ?

 $a['site_name'] = htmlspecialchars($row['site_name']);
 $a['site_desc'] = htmlspecialchars($row['site_desc']);
 $a['site_url'] = htmlspecialchars($row['site_url']);

Usaqe :

$a['site_name'];

I wached videos and tutorials but couldnt make second example works.

Answers: