Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
152 views
in Technique[技术] by (71.8m points)

php - pg_insert(): Accepts only string key for values

I've got postgres table with an auto-incremental column, and attempting to insert data from PHP with pg_insert returns a string key error. How would I go about modifying this for the different datatypes?

$username = $_SESSION['username'];

$data_timestamp = array(round(microtime(true) * 1000));
$data_username = array($username);

array_splice($data_to_store, 0, 0, $data_timestamp);
array_splice($data_to_store, 3, 0, $data_username);

var_dump($data_to_store);

$last_id = pg_insert($getPgInstance, 'app.features',  $data_to_store);

Currently returns the following error:

Warning: pg_insert(): Accepts only string key for values in /var/www/html/admin/add_table.php on line 24
insert failed: Array ( [0] => 1609941177697 [1] => username [2] => real_name [3] => staff_account [4] => dummydatareason [5] => accounttype )

A var_dump returns:

array(6) { [0]=> float(1609941177697) ["username"]=> string(4) "user" ["real_name"]=> string(4) "disp" [1]=> string(15) "dummydatareasontext" ["reason"]=> string(14) "sdfdsfpassssss" ["Type"]=> string(13) "accounttype" }

My Postgres table is made up of:

id integer NOT NULL DEFAULT nextval('app.features_id_seq'::regclass),
"timestamp" bigint,
username character varying(20) COLLATE pg_catalog."default",
real_name character varying(20) COLLATE pg_catalog."default",
staff_name character varying(100) COLLATE pg_catalog."default",
reason character varying(500) COLLATE pg_catalog."default",
type character varying(100) COLLATE pg_catalog."default",
CONSTRAINT features_pkey PRIMARY KEY (id)
question from:https://stackoverflow.com/questions/65598568/pg-insert-accepts-only-string-key-for-values

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

From the PHP manual:

pg_insert() inserts the values of assoc_array into the table specified by table_name.

That means you can't use a numeric array, like you do over here: [0]=> float(1609941177697)

I wouldn't use this function anyway, please check pg_query_params(), pg_query() and pg_prepare/pg_execute


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...