PHP, Postgres help using RETURNING - php

I think I understand how PostgreSQL and RETURNING works - I've found many, many resources. If I'm catching on, it would look something like
"INSERT INTO table (column2, column3) VALUES ('value1', 'value2') RETURNING id;"
However, I can't find anything that helps me access this via PHP. When I thought I figured it out, I tried
$new_id = pg_query($dbc, "INSERT INTO table (column2, column3) ".
"VALUES ('value1', 'value2') RETURNING id;");
return $new_id;
But it returns NULL. I also tried executing and declaring the variable separately with one query. After looking for hours for the solution, I've settled on a max(id) SELECT statement/function, but it's still bothering me. Any help is greatly appreciated.
I'm using Postgres 8.4 and PHP 5.3.

$new_id does not contain the id but it is a resource descriptor. You need to fetch the data from it as the query would be a SELECT, with pg_fetch_array($new_id) by example.
The RETURNING clause of PostgreSQL projects any fields of the inserted or modified rows ie INSERT|UPDATE … RETURNING id, field1, field2.

Related

How to get affected row count on WordPress query?

This code is to use in WordPress plugin.
The following is my code I am using to insert data from CSV file to database:
$sql="LOAD DATA LOCAL INFILE '".$fileurl."' INTO TABLE ".$table_name."
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\r\n' IGNORE 1 LINES (`first_name`,`last_name`, `email`, `mobile_phone`, `address_1`, `address_2`, `city`, `state`, `zip`, `about_us` );
";
$query = $wpdb->query($sql);
When I do this var_dump($query); it shows int(0), and data is successfully inserted in table. My question is how can I get number of inserted rows?
You can find the affected rows in your query using below.
$count = $wpdb->query($sql);
$count is your affected rows.
Very old question, I know, and perhaps this answer is trivially obvious, but posting in the hope it might be useful to someone who stumbled across it as I did.
In this particular case with LOAD DATA, one option might be to simply run an sql COUNT() before and after the LOAD DATA, and then take the difference:
$count_before = $wpdb->query("SELECT COUNT(*) FROM $table_name");
// LOAD DATA ...
$count_after = $wpdb->query("SELECT COUNT(*) FROM $table_name");
$new_rows = $count_after - $count_before;
I understand this may not work well if there is other simultaneous activity on the table; though you may be able to lock the table to prevent conflicting updates and to make the LOAD DATA even faster. You might be able to get this with $wpdb->affected_rows and that's also worth checking.
No doubt since it's 2 years ago since you asked this, you have any number of other working solutions; hopefully this is useful to anyone googling for a solution here.
Object $wpdb contains a lot of useful things. You can watch this by dump this object to see public properties.
One of them is what you search:
echo $wpdb->affected_rows;
You cannot get this information using a LOAD DATA statement with wpdb::query(). It doesn't understand the statement, so doesn't automatically fetch the number of affected rows. You can't get it yourself, either, because the necessary wpdb class properties (use_mysqli, dbh) are not public.

MySql query not running on the correct database after mysql_select_db

Firstly, I know that this system shouldn't be using mysql_ functions anymore, but it does.
The system is connecting to two databases. Before running a query, it is selecting the correct database on which to run the query using mysql_select_db(). The correct database is 'database2'. The connection to the database itself works, and the mysql_select_db function is returning true/1.
The code works locally but does not work on the server...
Here is an example of a failing query...
global $DBtwo;
mysql_select_db('database2', $DBtwo);
$sql = "INSERT INTO table
(Column1, Column2, Column3)
VALUES
('Value1', 'Value2', 'Value3')";
$result = mysql_query($sql, $DBtwo);
die(mysql_error());
This script returns the following output...
Table 'database1.table' doesn't exist
The error suggests that the query is being executed on database1, not database2. However database2 is being successfully selected before the query is run.
As mentioned, this error only occurs on the server. Running locally the queries work and the correct databases are used.
Any suggestions or pointers would be very welcome. Cheers.
The comment from Marc B solved my problem so credit goes to him.
The mysql_error function was returning misleading info as it was not being passed a resource.
The mysql_select_db was actually working. It gave the impression of not working via a combination of not passing the correct resource to mysql_error. On top of that a mysql_insert_id() was not working for the same reason, leading the function overall to return false.
I now feel stupid, but far less annoyed. Many thanks!

SELECT and INSERT not selecting

When I execute
INSERT INTO `survey`
(`id`,`name`,`date_start`,`date_end`)
values
(:id,:name,NULL,DATE_ADD(NOW(), INTERVAL 1 MINUTE))
on duplicate key UPDATE `name`=:name;
SELECT coalesce(:id,LAST_INSERT_ID()) as 'id'
it inserts a new data fine, but doesn't select the id (which is needed later on in my php code)
I've tried this suggestion
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
but this SQL throws errors (due to duplicate parameters)
SELECT ASCII(substr(`perm` FROM floor(:i/8)+1))&(1<<(:i%8))>0 as perm FROM `user` WHERE `id`=:id
I'm in a lose-lose situation, re-writing all my SQL code to not have duplicate parameters would be very messy, doing a separate select straight after inserting may not return the id I want. Any suggestions would be great
You cannot run two queries at the same time, only one at the time.
If you want to do the whole thing at once then create a stored procedure.
Same goes for complex queries, when it gets complicated you want to have your logic in the database.
Here is an example:
DELIMITER //
CREATE PROCEDURE sp_insert_survey(IN `p_id`,
IN `p_name`,
IN `p_date_start`,
IN `p_date_end`)
BEGIN
INSERT INTO `survey`(`id`,`name`,`date_start`,`date_end`)
VALUES (p_id, p_name, p_date_start, p_date_end);
SELECT `id`,`name`,`date_start`,`date_end`
FROM survey WHERE `id` =LAST_INSERT_ID();
END //
DELIMITER ;
Call the sp from PDO:
$stmt = $db->prepare('CALL sp_insert_survey(?, ?, ?, ?)');
then fetch the data as a SELECT query.
Upon typing this up, one of the similar questions that came up on the right getting last inserted row id with PDO (not suitable result) gave a suitable answer in the question itself, although I'm a little dubious considering the method is being questioned itself.
$db->lastInsertId();
Seems to work for me, but in the question linked it isn't working as desired for the questioner, so I'm not entirely settled with this answer. It does seem to be a bit of a hack.

How to insert data to several tables in MySQL?

I have only just begun learning about joining tables in MySQL. Now, I have a small project where I simply want to let the visitor insert data through a form. The data is then displayed in a HTML table with four rows, joining together two tables in my database. The "problem" is that the data should be submitted into those two different tables in my database.
I tried
$query = "INSERT INTO table1, table2 (col1, col2, col3, col4) VALUES ('value1', 'value2', 'value3', 'value4')";
but that doesn't seem to do it. What is the correct syntax for submitting form data to several database tables? Oh, and I read some similar threads mentioning using transactions. Is this necessary? My tables are run with MyISAM. Thanks!
You can read more about it from the MySQL Manual. In short, you cannot insert into multiple tables at once. This leaves you with three options:
Multiple INSERT statements
Triggers
Stored Procedures
The answer to this question: MySQL Insert into multiple tables? (Database normalization?) suggests using transactions, which will not work with MyISAM, but is a good FYI if you ever switch to InnoDB.
I really recommend you read up on Triggers. They can make your life a lot easier. But if you don't want to use them, look into PHP's mysqli_multi_query, which will allow you to execute two different queries at the same time, for example:
$query = "INSERT INTO table1 (col1,col2) VALUES ('$value1','$value2');";
$query = "INSERT INTO table2 (col3,col4) VALUES ('$value3','$value4');";
$result = mysqli_multi_query($dbcon, $query);
You can perform this by using MySQL Transactions By:
Try:
BEGIN
INSERT INTO table1 (col1, col2...ETC)
VALUES('value1', 'value2'...ETC)
INSERT INTO table2 (col1, col2...ETC)
VALUES('value1', 'value2'...ETC);
COMMIT;
With MyISM you will need to execute the query for each table you want to insert into, I do not believe that in a single query you can add to multiple tables.
In your case you can not use Transactions because they are not supported by your engine.
Your only solution is to use several separate queries, preferably within a transaction. Transactions are necessary if you want to make sure that the data from each query is inserted, in which case you COMMIT the transaction; should one of the queries fail, you can ROLLBACK.
P.S. Use InnoDB. It's better in pretty much any environment where INSERT queries make up at least 5% of all queries (sadly I cannot give the source as I had read it several months ago and no longer remember where).
I may be wrong, but you don't insert into multiple tables at the same time. You split it into two or more commands, each handling the specific insertion, whats the big deal, that one extra line of code (which makes everything clearer) too much of a hassle to type?
Look at it this way, if you write a large script, for instance a routine to process some data, the more you segment the code, the easier it is to debug, and, if necessary, inoculate instructions that are problematic, it will end up saving you time in the long run.
I have this problem before You can use multiple query function
$query = "INSERT INTO table1 (col1,col2) VALUES ('$value1','$value2')";
$query = "INSERT INTO table2 (col3,col4) VALUES ('$value3','$value4')";
$result = mysqli_multi_query($dbcon, $query);

MS Access: How does one insert NULL into DateTime field

I have an MS Access database (intolerably enough), and communicating with it through PHP (ODBC).
There is a DateTime field that I have to include in my INSERT statement. This field is NOT defined as "Required" in Access, meaning that it is indeed NULL-able, and in fact some of the rows in the Access database are already NULL.
The problem I'm having is simple: How to insert NULL through SQL? All the results I've found online have addressed it from something like Visual Basic or C#, whereas I'm using SQL through ODBC in PHP.
I have already tried the following:
INSERT INTO table_name (datetime_field) VALUES (NULL)
INSERT INTO table_name (datetime_field) VALUES (#NULL#)
INSERT INTO table_name (datetime_field) VALUES ('NULL')
INSERT INTO table_name (datetime_field) VALUES ('#NULL#')
INSERT INTO table_name (datetime_field) VALUES ('')
(There's about 30 other columns in my query.)
The exact error I get is 'Data type mismatch in criteria expression' when I try '' or NULL. The others return a parse error (understandably).
Please note that I have to include the field in the INSERT statement. The field has a default value, but in many cases the original data that I'm transporting has a NULL that must also be a NULL in the target database.
Thanks in advance!
Try the following. It works for me:
INSERT INTO sometable ( somedate, somethingelse )
SELECT Null AS Expr1, "foo" AS Expr2;
Basically, you are wrapping the null in the select query and letting SQL figure out how to represent it to the insert.
-- EDIT --
This SHOULD also work:
INSERT INTO sometable ( somedate, somethingelse )
values (Null , "foo");
But for some reason it doesn't with my default install.
On I hunch, I switched my DB from ANSI-89 to ANSI-92, and the VALUES method started working. I switched it back to ANSI-89, and it still works. Not only that, on ANY new database I create, it now also works. Weird... something in the installation must be getting changed, (and sticking) by the switching back and forth that's not just ANSI-89/92. This seems to be why we were getting different results.
You can switch the database ocwe by going to Office Logo->Access Options->OBJECT DESIGNERS->QUERY DESIGN. Change SQL Server Compatible Syntax (ANSI 92) - and checking "This database".
Ok, very odd.
I know you've already figured this out but there is also dbNull
INSERT INTO table_name (datetime_field) VALUES (DbNull.Value)
Try just leaving it blank
(values fld1,,fld2)
What are the libraries, you are using in order to talk to ODBC?
Could it be a problem with the syntax for null values, the way library interprets it?
See, if this page helps.
Note: I have not worked with PHP.
I have this type of error and
Try this.
INSERT INTO table_name (datetime_field) VALUES (DBNull.value)
It works fine for me.

Categories