我想用php连接到mysql数据库。代码是:
index.php
<button>
<a href='insert.php'>insert.php geçiş yap</a>
</button>
<?php
require_once 'baglan.php';
if(!isset($_GET['sayfa'])){
$_GET['sayfa'] = 'index';
}
switch ($_GET['sayfa']) {
case 'insert':
require_once 'insert.php';
break;
}
?>
baglan.php
<?php
try{
$db = new PDO('mysql:host=localhost;dbname=ornek_veritabani','root','');
}catch(PDOException $e){
echo $e -> getMessage();
};
?>
insert.php
<?php
echo 'insert.php';
$sorgu = $db->prepare('INSERT INTO deneme SET
ad = ?,
soyad = ?,
yas = ?,
memleket = ?');
$ekle = $sorgu->execute([
'php ad','php soyad', 10, 'php memleket'
]);
if($ekle){
echo 'verileriniz eklendi...';
} else{
print_r($sorgu->errorInfo());
}
?>
insert.php文件中的问题。返回错误。它给出以下错误:
insert.php 注意:未定义的变量:第3行的C:\ xampp \ htdocs \ PHP \ insert.php中的db
致命错误:未捕获错误:在> C:\ xampp \ htdocs \ PHP \ insert.php:3中对null调用成员函数prepare()堆栈跟踪:#0 {main}在> C:\ xampp中引发第3行上的\ htdocs \ PHP \ insert.php