list($localhost, $database, $charset, $user, $pass) = ["localhost", "world", "utf8", "root", ""];
$pdo = new pdo("mysql:host=$localhost;dbname=$database;charset=$charset", $user, $pass);
#ERROR
for ($i = 1; $i < 20; $i++) {
$full_name = ['a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l'];
$sex = ['m', 'f'];
$useradd =
[
'full_name' => $full_name[rand(0, 8)],
"sex"=>$sex[rand(14, 40)],
'age' =>rand(14, 40)
];
$insert_data = "INSERT INTO people (full_name,sex,age ) VALUES (?,?,?)";
$stmt = $pdo->prepare($insert_data);
$stmt ->bindParam("ssi",$useradd['full_name'], $useradd["sex"], $useradd['age']);
if ($stat->execute()) {
echo "insert new user";
}
}
"$m1 = $stmt ->bindParam("ssi",$useradd['full_name'], $useradd["sex"], $useradd['age']);"
"$m2 = $stat->execute([$useradd['full_name'], $useradd["sex"], $useradd['age']]);"
"$m1==$m2 ????"
for = error;
###############################
function I($Ygg)
{
foreach ($Ygg as $value) {
echo (implode("_",(array)$value)).PHP_EOL;
}
}
function WE($G)
{
foreach ($G as $value) {
I($value);
}
}
$select= 'SELECT * FROM PEOPLE WHERE id = id';
$stmt=$pdo->prepare($select);
$stmt -> execute();
#ERROR ERROR ERROR ERROR ERROR ERROR ERROR ERROR ERROR ERROR ERROR ERROR ERROR ERROR ERROR ERROR ERROR?
WE($stmt ->fetch());
#OK OK OK OK OK OK OK OK OK OK OK OK OK OK OK OK OK OK OK OK OK OK OK OK OK OK OK OK OK OK OK OK OK OK?
we($stmt ->fetchAll(PDO::FETCH_BOTH));
سلام استاد
در قسمت اول سوال ک از حلقه for استفاده کردم
بعد از دادن پارامترها به bindparam کد راحت اجرا شد
ولی وقتی همینکار رو بجای
Bindparam
با
Exceute
انجام دادم به error
خوردم به این شکل
$stat->execute([$useradd['full_name'], $useradd["sex"], $useradd['age']])
ولی استاد آوند در توضیحاتشون
از Exceute
بجای bindparam
استفاده کردند
و به ارور نخوردن
در بخش دوم سوالم
دوتا فانکشن ساختم برای دستور select ولی وقتی
WE($stmt ->fetch());
استفاده میکنم به ارور میخورم
we($stmt ->fetchAll(PDO::FETCH_BOTH));
در صورتیکه برای این اینطور نیست