Results 1 to 6 of 6
  1. #1

    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();
    }

    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?

  2. #2
    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 (™)

  3. #3
    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.

  4. #4
    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()); 


    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!

  5. #5
    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)) { } 
    This way, you only execute your query once.

    Hopefully, this will get you started.

  6. #6
    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)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •