Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

PHP Loop problem

Options
  • 14-06-2006 11:07pm
    #1
    Closed Accounts Posts: 8,866 ✭✭✭


    Hi guys,

    Can't get this script to work, its getting late and I'm getting irritated! Its a simple loop, all the data in test123 is to be inserted into tbl_firm row by row, but at the moment when I run the script (in my test environment at the moment of course) it inserts the first row and then gives me hundresd of the same error:

    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\Program Files\xampp\htdocs\editor\final.php on line 15
    INSERT INTO tbl_firm (firm_name, firm_address1, firm_address2, firm_address3, firm_address4, firm_phone, firm_fax, firm_contactname, firm_contactname_position, firm_employees) VALUES ("", "", "", "", "", "", "", " ", "", "")

    Here's the script:

    [php]<?php

    include 'db.php';


    $SQL="SELECT * FROM test123 WHERE firm_entered != 'y'";
    $result = mysql_query($SQL) or die("Query failed : " . mysql_error());
    $num_rows = mysql_num_rows($result);



    $i=0;
    while ($i < $num_rows) {

    $row = mysql_fetch_array($result, MYSQL_ASSOC);


    $SQL="INSERT INTO tbl_firm (firm_name, firm_address1, firm_address2,
    firm_address3, firm_address4, firm_phone, firm_fax, firm_contactname,
    firm_contactname_position, firm_employees) VALUES
    (\"{$row}\", \"{$row}\",
    \"{$row}\", \"{$row}\",
    \"{$row}\", \"{$row}\",
    \"{$row}\", \"{$row}
    {$row}\", \"{$row}\",
    \"{$row}\")";

    echo $SQL;
    $result = mysql_query($SQL) or die("Query failed : " . mysql_error());

    $i++;
    }



    ?>[/php]


Comments

  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    Well you are using result twice.

    You are overwriting to original query. Call the second result result2 or something. You don't even use it either, why dont' you just say mysql_query($SQL) or die("....

    Try that ne ways


  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    No the $result isn't the issue, if i print_r($row) after the fetch_array $row contains the correct data... :confused:


  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    Sorry, I misunderstood you, that worked, cheers!


  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    :) no probs

    Also you don't actually need the $i counter. you could just say:
    while ( $row = mysql_fetch_array($result, MYSQL_ASSOC))
    {
    
    you're inner code
    
    }
    


  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    Yeah I noticed that because I had tried that earlier while I was still using $result twice :rolleyes:


  • Advertisement
Advertisement