I want to INSERT into MySQL database using PDO connection with PHP7.4, but no data is added.
I tried to find out the cause, but I couldn't understand the cause without displaying any errors.
$err = [PDO :: ATTR_ERRMODE =>PDO :: ERRMODE_EXCEPTION, PDO :: ATTR_EMULATE_PREPARES =>false];
// Error display setting: Display all except notification
error_reporting (E_ALL&~ E_NOTICE);
try {
// Connect to DB
$dbh = new PDO ('mysql:'. DB_NAME. ';'. DB_HOST, DB_USER, DB_PASSWORD, $err);
if ($dbh == null) {
print ('Connection failed.');
}
else {
print ('Connected');
}
$a1 = 'abcdef';
$a2 = 'ghijklm';
$a3 = 'nopqrst';
// Create SQL
$sql = "INSERT INTO sample.data (one, two, three)
VALUES ('". $A1."', '". $A2."', '". $A3."');";
// SQL execution)
$res = $dbh->query ($sql);
print_r ($res);
}
catch (PDOException $e) {
print ('ERROR:'. $e->getMessage ());
exit;
}
Execution result (print_r output contents)
Connected. PDOStatement Object
(
[queryString] =>INSERT INTO sample.data (one, two, three)
VALUES ('abcdef', 'ghijklm', 'nopqrst');
)
First of all, I checked the if statement to see if an error occurred at the database connection stage. The message "Connected" was displayed, so I think that the connection was successful.
Next, the result obtained by displaying the contents of $dbh->query ($sql) (INSERT INTO sample.data (one, two, three)
VALUES ('abcdef', 'ghijklm', 'nopqrst');) was inserted when logging in mysql and copying.
PHP7.4
MySQL8.0
FreeBSD12.1
Assuming that the IP address on the side running PHP is 192.168.1.100 and the IP address of the connection destination (MySQL server to be connected) is 192.168.1.101.
echo 'mysql:'. DB_NAME. ';'. DB_HOST;results in
mysql: sample;It was 192.168.1.101.
As a result of checking again, there was a table named data in the database named sample at 192.168.1.100, but it seems that the INSERTs so far have been on this side. The database on 192.168.1.101 is not INSERTed as usual.
-
Answer # 1
-
Answer # 2
Does table sample.data really exist?
The mysql system seems to enclose identifiers in back quotes, but try it.
Try it like// Create SQL $sql = "INSERT INTO` sample.data` (`one`,` two`, `three`) VALUES (: one,: two,: three);"; // SQL execution) $sth = $dbh->prepare ($sql); $res = $sth->execute ([ ': one' =>$a1, ': two' =>$a2, ': three' =>$a3, ]); print_r ($res);
.
Related articles
- php: there is an error in the insert into line and it cannot be resolved
- automatic numbering insert cannot be done from php to postgressql
- i want to save data in phpmyadmin using insert syntax
- php - cannot install with composer require
- php - payment using square i want to register a memo or product name in payment
- php - list is displayed by select, but insert is not possible
- php - about passing value to form using tag
- php - about the version when developing using the framework
- javascript - i want to insert the start tag and end tag of html tags individually using jquery
- php - wordpress: tab switching using custom field items
- cannot post html form on local web server using python
- php - variables cannot be passed in laravel
- about encryption at the time of output in php using form input tyle = "password"
- php - in a test using mockey for sns authentication, provider :: stateless () does not exist on this mock object is displayed
- php - cannot load javascript on individual page of custom post
- php - unix time cannot be converted
- php - how to convert from excel to pdf using libreoffice on ec2
- creating an associative array using php and variable names as keys
- php - cannot save even if re-executed with sudo authority
- i'm using phplaravel and the screen gives an error
- How does PDO in PHP find a database?
- php : Why is only one product displayed via PDO?
- Long connection to the database (PHP)
- How to choose the right database in PHP PDO
- php : How to execute a request from Union All from two identical tables with sorting
- php : MySQL goes down with PDO connection only.
- RedBeanPHP problem
- How to find out if the data returned the prepared PDO PHP request?
- javascript : Google Charts Annotations to Bars
- php : Parser returns a string, and an array needs
The cause seems to have been a mistake when creating an instance of PDO.
mysql: '. DB_NAME.';'. DB_HOST
mysql: dbname = testdb;host = localhost;
Try adjusting so that the output looks like this.
Reference:
Knowledge necessary for MySQL connection with PHP (minimum version)