Web Hosting Talk







View Full Version : Select & Use multiple MySQL rows? (PHP)


ProdigySim
04-22-2004, 07:53 AM
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.....
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();
}
}
Now, that just gives me an infinite loop and only uses ONE of the rows, trying to insert it over and over again... Am I not using the $rows variable (an associative array) right?
Can anyone tell me what is wrong?

Loon
04-22-2004, 08:09 AM
"UPDATE mt_template SET template_blog_id = 'new_value' WHERE template_blog_id = '6'";

ProdigySim
04-22-2004, 02:38 PM
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.

trukfixer
04-22-2004, 03:52 PM
Might be a lot easier to debug if code was cleaner...
(don't mind me, Im a big fan of STANDARDS)

anyhow...
$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());

}


Notes: when a sql statement is written inside double quotes, variables used in teh statement need to be SINGLE QUOTED...

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!

Burhan
04-23-2004, 09:24 AM
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 :


$results = mysql_query($query);
while($row = mysql_fetch_assoc($results)) { }


This way, you only execute your query once.

Hopefully, this will get you started.

trukfixer
04-23-2004, 10:36 AM
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.

http://alltasks.net/code/php_coding_standard.html



And finally, your code will not work ;) There are a few errors.


Mind pointing them out???

works perfectly for me... (I use the above in my own code- only difference is the mysql query string)