Page 1 of 1
split text and insert into the column
Posted: Sun Jan 24, 2010 5:52 am
by fer0an
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
Re: split text and insert into the column
Posted: Sun Jan 24, 2010 6:26 am
by requinix
will split a $string into parts separated by whitespace.
Loop over that and
run the INSERT query.
Re: split text and insert into the column
Posted: Sun Jan 24, 2010 8:42 am
by fer0an
I used this code
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));
but I recived an error below:
Fatal error: Call to a member function prepare() on a non-object in /home/...
any idea?
Re: split text and insert into the column
Posted: Sun Jan 24, 2010 9:29 am
by requinix
$db isn't an object.
How about posting the code to do with how $db is created?
Re: split text and insert into the column
Posted: Sun Jan 24, 2010 11:46 am
by fer0an
I created it before :
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));
it's working with one column and tested.
can you help me to use it with some other column?
thank you
Re: split text and insert into the column
Posted: Sun Jan 24, 2010 1:22 pm
by requinix
Did you overwrite $db anywhere? What does
give?
Re: split text and insert into the column
Posted: Sun Jan 24, 2010 1:28 pm
by fer0an
no