hello
anyone can help me about:
splitting a text with space between them , then insert each one to db?
for example :
$text = My name is David
I want it divided to:
$text1 = my;
$text2 = name;
$text3 = is;
$text4 = David ;
then insert $text1 into db;
then insert $text2 into db;
then insert $text3 into db;
then insert $text4 into db;
thank you
split text and insert into the column
Moderator: General Moderators
Re: split text and insert into the column
Code: Select all
preg_split('/\s+/', $string)Re: split text and insert into the column
I used this code
but I recived an error below:
Fatal error: Call to a member function prepare() on a non-object in /home/...
any idea?
Code: Select all
$string = $title;
$words = explode(' ', $string);
$stmt = $db->prepare("INSERT INTO tag_term (`id`, `name`, `description`, `weight`, `hits`, `created`) VALUES (NULL ,?, '".$title."' , '0', '0', '".date('Y-m-d G:i:s')."')");
foreach ($words as $word) {
$stmt->execute(array($word));Fatal error: Call to a member function prepare() on a non-object in /home/...
any idea?
Re: split text and insert into the column
$db isn't an object.
How about posting the code to do with how $db is created?
How about posting the code to do with how $db is created?
Re: split text and insert into the column
I created it before :
it's working with one column and tested.
can you help me to use it with some other column?
thank you
Code: Select all
$db = new PDO('mysql:host=localhost;dbname=test', 'user', 'password');
$string = $title;
$words = explode(' ', $string);
$stmt = $db->prepare('INSERT INTO words (word) VALUES(?)');
foreach ($words as $word) {
$stmt->execute(array($word));
can you help me to use it with some other column?
thank you
Re: split text and insert into the column
Did you overwrite $db anywhere? What does
give?
Code: Select all
var_dump($db)