0

I have a problem I cannot separate the array by commas if I put the implode it only shows the first number and not the second in database i need insert in database 500,756

global $wpdb;  $arg=array(500,756);

$var = implode(",", $arg);   --> return in db 500 ->I can not separate by commas, num 500 is only show in db 
$var = implode($arg);        --> if i put this instead of the other return in db shows 500756

     $wpdb->insert('number',array( 
          'num' => $var,   
        ));  
CREATE TABLE `number` (
  `id` int(255) NOT NULL,
  `num` varchar(255) NOT NULL,
) ENGINE=MyISAM DEFAULT CHARSET=latin1;


ALTER TABLE `number`
  ADD PRIMARY KEY (`id`);

ALTER TABLE `number`
  MODIFY `id` int(255) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=39;
3
  • 1
    What type is the num column? If it's an integer column of any kind then you can't insert comma separated values. Jul 14, 2020 at 5:21
  • 2
    are you trying to insert 1 row with the value "500,756" or are you trying to insert two rows with one : 500 and the other 765? Jul 14, 2020 at 8:53
  • Data type is varchar but only saves 500
    – Stymark
    Jul 14, 2020 at 18:09

1 Answer 1

0

implode(",", [500,756]) will definitely return "500,756", therefore you have some other problem.

If the field type of the 'num' field is a numeric type such as MySQL's INT, then MySQL or PHP might try and convert this string to a number and so "500,756" could become 500.

Check the data type of the 'num' field in the 'number' table. To store the string you want to store it needs to be a string type such as VARCHAR. Or if you're trying to store a number with stuff after the decimal point make sure it's e.g. a FLOAT / DECIMAL, etc. and add the value using a decimal point, e.g. 500.756.

If you need further help, show the CREATE TABLE output for your table

2
  • I have put the sql code in the publication
    – Stymark
    Jul 14, 2020 at 18:07
  • looks fine, perhaps you're not looking at the right row when looking for the result, or left your second like in which would overwrite the $var variable
    – mozboz
    Jul 14, 2020 at 20:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.