SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Guru TacMaf's Avatar
    Join Date
    Dec 2005
    Location
    Manchester, United Kingdom
    Posts
    663
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    While loop only using first row

    Hi,

    I'm trying to update my DB with all the data from another table. The problem is the loop only uses the first row in temp, but does its job too many time.

    Table temp has 1000 rows.

    Table products ends up with 5008 identical rows (apart from primary key). All data is from row 1 of temp.

    PHP Code:
    public function insertBloodyProducts(){
        
    $q "SELECT * FROM ".DB_PREFIX."temp";
        
        while(
    $result $this->query($q)){
            
    $row $this->fetch_assoc($result);
            
    $cleanURL preg_replace('/[^a-zA-Z0-9]/i','-',$row['tempName']);
            
    $cleanURL preg_replace('/ /i','-',$cleanURL);
            
    $categories = array("161"); //Array for future additions
            
        
        
        
    $data = array(...);
            
        
    $q1 $this->insertProductQuery($data);

        
    $result1 $this->query($q1);

    }


    This has been driving me crazy. Can anyone see where I'm going wrong?

    Cheers,
    Rhys

  2. #2
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Yep, your while loop is using the QUERY and not the results of the query
    while($result = $this->query($q)){
    $row = $this->fetch_assoc($result);
    ....

    $result = $this->query($q);
    while(
    $row = $this->fetch_assoc($result)) {
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  3. #3
    SitePoint Guru TacMaf's Avatar
    Join Date
    Dec 2005
    Location
    Manchester, United Kingdom
    Posts
    663
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, I changed that earlier trying to fix another issue. That fixed the issue with the first row. But it still seems to insert 2 of everything. Not too sure why?

  4. #4
    SitePoint Guru TacMaf's Avatar
    Join Date
    Dec 2005
    Location
    Manchester, United Kingdom
    Posts
    663
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nevermind. The function was run twice because of another function on the page. All sorted. Thanks.

  5. #5
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Are you saying you want to ultimately:

    a) copy the contents of one table to a temp one
    b) edit slightly the column of the new temp one when it has been filled

    ?

  6. #6
    SitePoint Guru TacMaf's Avatar
    Join Date
    Dec 2005
    Location
    Manchester, United Kingdom
    Posts
    663
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No it's an ecommerce site for a client. I often get sent enormous CSVs full of products. But it is expected they just "slot in". But with so many different tables and fields, I've found its best to put them into a temp table and call a function to run through it and execute it as it would if they used the "New Product" form. All sorted now, but where are data entry employees these days? You build a nice easy CMS/ecommerce system, but it's still your job to use it.

    Cheers,
    Rhys

  7. #7
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Just to say there is a REPLACE function in Mysql which may not be as easy to write as PHP, but would probably be quicker to run, in case you did not know.


Bookmarks

Posting Permissions

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