I'm teaching myself using the text PHP7+MariaDB/MySQL Masterbook.
Coding a sample program in text results in a heading error.
<?PHP
// Keep users, passwords, and hosts secret.Configuring separately.
$db_user="xxxxxxxxxx";
$db_pass="xxxxxxxxxx";
$db_host="xxxxxxxxxx";
$db_name = "phptx";
$db_type="mysql";
$dsn="$db_type:host=$db_host;db_name=$db_name; charset=utf8";
try{
$pdo = new PDO($dsn, $db_user, $db_pass);
$pdo->setattribute(pdo::ATTR_ERRMODE,
pdo::ERRMODE_EXCEPTION);
$pdo->setattribute(pdo::ATTR_EMULATE_PREPARES, false);
print "Connected";
} catch(PDOexception$Exception){
die('Error 1:'.$exception->getMessage());
}
try{
$pdo->beginTransaction();
$sql = "insert into member(last_name, first_name, age) values(:last_name,:first_name,:age)";
$stmh = $pdo->prepare($sql);
$stmh->bindvalue(':last_name',$_post['last_name'],pdo::param_str);
$stmh->bindvalue(':first_name',$_post['first_name'],pdo::param_str);
$stmh->bindvalue(':age', $_post['age'], pdo::param_int);
$pdo->execute();
$pdo->commit();
print "Inserted ".$stmh->rowcount()" data.<br>";
} catch(PDOException$exception){
$pdo->rollback();
print "Error 2:".$exception->getMessage();
}
?>
The connection to MySQL seems to be working, but DB has not been selected.
We have verified that DB (Schema) phptx and table member exist on MySQL.
The results of the file are
Connected Error 2: SQLSTATE [3D000]: Invalid catalog name: 1046 No database selected
is the case.I would appreciate it if you could let me know.
php mysql
$dsn="$db_type:host=$db_host;db_name=$db_name; charset=utf8";
↓
$dsn="$db_type:host=$db_host;dbname=$db_name; charset=utf8";
© 2024 OneMinuteCode. All rights reserved.