Results 1 to 6 of 6
-
04-22-2004, 07:53 AM #1Newbie
- Join Date
- Apr 2004
- Posts
- 7
Select & Use multiple MySQL rows? (PHP)
I can write the queries from scratch, I guess, but it would be easier to do this in some sort of loop.
I have a table (mt_template) where multiple rows have the field template_blog_id = 1
I can give a screenshot of the tables if needed, but here's an idea of what one will be like:
template_id = 1, template_blog_id = 6, template_name = index, template_text = <lots of HTML>, etc, etc.
template_id = 2. template_blog_id = 6, template_name = atom, template_text = <different HTML>, etc, etc.
Anyway, I want to grab every row where template_blog_id = 6 and re-enter it with a different template_blog_id.
What I have right now is something like this, but it's not working.....
PHP Code:while( $rows = mysql_fetch_assoc(mysql_query('SELECT * FROM mt_template WHERE template_blog_id = "6";'))) {
$query = mysql_query('INSERT INTO mt_template (template_blog_id, template_name, template_text, etc. etc. etc.)
VALUES (
"' . $newblogid . '", "' . $rows['template_name'] . '", "' . $rows['template_text'] . '", "' $rows['etc'] . '");');
if(!$query) {
echo mysql_error();
}
}
Can anyone tell me what is wrong?
-
04-22-2004, 08:09 AM #2Retired Moderator
- Join Date
- Apr 2003
- Location
- London, UK
- Posts
- 4,721
Code:"UPDATE mt_template SET template_blog_id = 'new_value' WHERE template_blog_id = '6'";
Hyperconfused (™)
-
04-22-2004, 02:38 PM #3Newbie
- Join Date
- Apr 2004
- Posts
- 7
No, that's not what I want to do. I want to keep the old rows, but add new ones almost exactly the same as them.
-
04-22-2004, 03:52 PM #4WHT Addict
- Join Date
- Apr 2004
- Posts
- 104
Might be a lot easier to debug if code was cleaner...
(don't mind me, Im a big fan of STANDARDS)
anyhow...PHP Code:$sql="SELECT * FROM mt_template WHERE template_blog_id = '6'";
//(note the SINGLE quoted variables within teh sql statement)
$res=mysql_query($sql) or die("Could Not Perform Query Because".mysql_error());
while($rows=mysql_fetch_array($res));
{
//NOTE:: below sql query is assuming all fields are named,
//for this example assume 6 fields in the whole table....
$sql1="INSERT INTO mt_template VALUES(".
"'',". //template_id(auto-increment)
"'$newblogid',". //new blog id, assuming variable already set
"'".$rows['template_name']."',". //template name
"'".$rows['template_text']."',". //template text
"'".$rows['template_etc']."',". //etc.
"'".$rows['template_etc2']."'"; //etc2
$res=mysql_query($sql1) or die("Could Not Perform Query Because".mysql_error());
}
Note 2: I typically structure an insert just like I did above. Why? it's SUPER SIMPLE to de-bug- you can compare fields to row count, and adding a new field into a query is a snap. (see how each field name is given in the comment??)
hope this helps..
Bri!
-
04-23-2004, 09:24 AM #5Web Hosting Master
- Join Date
- Jul 2003
- Location
- Kuwait
- Posts
- 5,104
trukfixer :
Firstly, cleaner is relative. What may seem clean to you, may seem cluttered to others.
Secondly, I don't know what standard you are following, please elaborate.
And finally, your code will not work There are a few errors.
As for the original poster :
Your loop is infinite because you are executing the query for each iteration of the loop.
Remove the mysql_query() call from your while() condition, and use the resulting object in your while(), like so :
PHP Code:$results = mysql_query($query);
while($row = mysql_fetch_assoc($results)) { }
Hopefully, this will get you started.
-
04-23-2004, 10:36 AM #6WHT Addict
- Join Date
- Apr 2004
- Posts
- 104
Originally posted by fyrestrtr
[B]trukfixer :
Firstly, cleaner is relative. What may seem clean to you, may seem cluttered to others.
Secondly, I don't know what standard you are following, please elaborate.
And finally, your code will not work There are a few errors.
works perfectly for me... (I use the above in my own code- only difference is the mysql query string)