admin管理员组

文章数量:1331692

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;

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;
Share Improve this question edited Jul 14, 2020 at 19:12 Howdy_McGee 20.9k24 gold badges91 silver badges177 bronze badges asked Jul 14, 2020 at 5:07 StymarkStymark 372 bronze badges 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. – Jacob Peattie Commented 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? – bjornredemption Commented Jul 14, 2020 at 8:53
  • Data type is varchar but only saves 500 – Stymark Commented Jul 14, 2020 at 18:09
Add a comment  | 

1 Answer 1

Reset to default 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

本文标签: phpI can39t separate the array by commas