SitePoint Sponsor |
|
User Tag List
Results 1 to 8 of 8
Thread: insert data from ....
-
Oct 8, 2001, 14:41 #1
- Join Date
- Nov 2000
- Location
- Dallas, TX
- Posts
- 133
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
insert data from ....
How do I insert data from titletext1 and bodytext1 into combined1 (same to all the rows)?
id title body combined
-------------------------------------------
1 titletext1 bodytext1 combined1
2 titletext2 bodytext2 combined2
3 . . .
4 . . .Trigger8
**************************
I killed a 6-pack just to watch it die!
-
Oct 8, 2001, 15:03 #2
- Join Date
- Jan 2001
- Location
- buried in the database shell (Washington, DC)
- Posts
- 1,107
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
What you want is to "update" each *existing* row in the database.
this would be done something like this:
Code:UPDATE table SET combined = title + body
Matt - Sybase DBA / PHP fanatic
Sybase/MySQL/Oracle | I don't like MySQL
Download Sybase | DBForums.com - for all your RDBMS talk
-
Oct 8, 2001, 15:05 #3
- Join Date
- Aug 2001
- Location
- Kent, Ohio
- Posts
- 367
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
If there's a single operation in mysql that you can use like MattR had suggested (you can tell that my mysql knowledge is lacking ;p), you'd want to use that. The following is another approach.
PHP Code:
$query = mysql_query("select id,title,body from tablename");
while ($result = mysql_fetch_object($query)) {
mysql_query("update tablename set combined1='".$result->title.$result.body."' where id='$result->id'");
}
Last edited by cupid; Oct 8, 2001 at 15:09.
-
Oct 8, 2001, 15:06 #4
- Join Date
- Mar 2001
- Location
- Mexico
- Posts
- 1,287
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Try this,
PHP Code:$sql = 'SELECT id,title,body FROM yourtable';
$result = @mysql_query($sql):
if ($result) {
while ($row=mysql_fetch_array($result)) {
$combined = $row['title'].$row['body'];
$sql = "UPDATE table SET combined='$combined' WHERE ID=".$row['id'];
if (@mysql_query($sql))
echo "Rows combined\n";
else
echo "Database problem\n";
}
}
Last edited by Paul S; Oct 8, 2001 at 15:11.
-
Oct 8, 2001, 15:08 #5
- Join Date
- Mar 2001
- Location
- Mexico
- Posts
- 1,287
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Wow, so many options
-
Oct 9, 2001, 07:27 #6
- Join Date
- Nov 2000
- Location
- Dallas, TX
- Posts
- 133
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Is there anything wrong in the following script?
PHP Code:<?
MYSQL_CONNECT($hostname, $username, $password) OR DIE( "Unable to connect to database");
@mysql_select_db("$dbName") or die( "Unable to select database");
$sql ='SELECT id,Topic,Frequency,Heading FROM tablename';
$result = @mysql_query($sql);
if ($result) {
while ($row=mysql_fetch_array($result)) {
$combined = $row['Heading'].$row['Topic'].$row['Frequency'];
$sql = "UPDATE table SET keyword='$combined' WHERE ID=".$row['id']";
if (@mysql_query($sql))
echo "Rows combined\n";
else
echo "Database problem\n";
}
}
?>Trigger8
**************************
I killed a 6-pack just to watch it die!
-
Oct 9, 2001, 07:40 #7
- Join Date
- Nov 2000
- Location
- Dallas, TX
- Posts
- 133
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Now I got it right.
But how do I seperate the values from each columns so that they don't really combined together.
The output that I get now is 'text1text2text3'. How to make it 'text1 text2 text3'?
Also, Is there anyway to only insert text without special characters..like \()_-...
Thanks!Trigger8
**************************
I killed a 6-pack just to watch it die!
-
Oct 9, 2001, 15:15 #8
- Join Date
- Mar 2001
- Location
- Mexico
- Posts
- 1,287
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Hi,
you can do it by adding,
$combined = $row['Heading'].' '.$row['Topic'].' '.$row['Frequency'];
Bookmarks