Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

PHP Loop problem

  • 14-06-2006 10: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, Registered Users 2 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, Registered Users 2 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