0

I have this code, it only inserts the first $listing, then the others are ignored, mysqli_query doesnot return any errors, I was having a issue with column being truncated, but I fix that issue,
When I copy and paste the $sql1 on phpmyadmin, it works with no issues.

$resource = "Property";
      $classArray = array("RES");
      foreach ($classArray as $pClass){
        //$maxRows = true;
        //$offset = 1;
        //$limit = 9999999;        

        //while($maxRows){
          //$results = $rets->Search($resource, $pClass, $query);
          $results = $rets->Search($resource, $pClass, 'ModificationTimestamp=2018-04-25T00:00:00+'); //, [
              //'Limit' => $limit,
              //'Format' => 'COMPACT-DECODED',
              //'Select' => $selectFields,
              //'Offset' => $offset
            //]);
          //$count = 0;

          $results->toArray();

          foreach($results as $listing){
            //GENERATE TABLE NAME
            $table_name = "rets_".strtolower($resource)."_".strtolower($pClass);

            /// Truncate Table to perform fresh import.
            $truncateSQL = "TRUNCATE TABLE ".$table_name;
            if(!mysqli_query($link, $truncateSQL)){
              echo "Failed to Truncate Table<br/>";
              die();
            }

            //PREPARE DATA TO GO IN DATABASE
            $fields = $listing->toArray();
            //$values = array();

            $fieldValues='';
            $fieldNames='';

            $oldDate = 'DEFAULT';//date("Y-m-d",strtotime("2001-08-06"));
            foreach($fields as $field => $value){
              //echo "Key=" . $field . ", Value=" . $value."<br/>";
                if($field == "ExpectedOnMarketDate"){
                $value = $oldDate;
              }elseif(is_numeric($value)){
                $value = $value;
              }elseif(empty($value)){
                $value = 'DEFAULT';
              }else{
                $value = "'".mysqli_real_escape_string($link, $value)."'"; 
                //$value = $value; 
              }

              $fieldValues.= $value.',';
              $fieldNames.= $field.',';

              unset($field);
              unset($value);
            }

            $fieldValues = rtrim($fieldValues, ',');
            $fieldNames = rtrim($fieldNames, ',');

            $sql1 = "INSERT INTO ".$table_name." ($fieldNames) VALUES ($fieldValues)";
            echo "<hr/>";

            //echo str_replace(",", "<br/>", $fieldNames)."<br/>";
            //echo str_replace(",", "<br/>", $fieldValues)."<br/>";
            echo $sql1;

            if (mysqli_query($link, $sql1)) {
               echo "Record Created for: " . $listing['ListingID'] . "\r\n <br/>";

            }else{
                echo "ERROR: " . mysqli_error($link) . "\r\n\r\n <br/>";
            }
            //$count++;

            unset($fieldNames);
            unset($fieldValues);
            unset($listing);
            unset($sql1);
          }

          //$offset = ($offset + $count);
          //$maxRows = $results->IsMaxrowsReached();
       // }

        echo "Done Getting Listings from server.<br/>";
        die();
      }

Thank you for your time.

2
Contributors
3
Replies
54
Views
3 Weeks
Discussion Span
Last Post by elamigosam
0

without seeing the output on line 67 it's hard to say, but my assumption is that you are geting an output like...

insert into foo (a,b,c) values (1,2,3,1,2,3,1,2,3,12,3) -- kinda suprised this doesn't throw an error...

when instead I believe you want something more like

insert into foo (a,b,c) values (1,2,3), (1,2,3), (1,2,3), (1,2,3) ....

0

hi ryantroop,
sorry for the late responce, you are right, the output of $sql1 is like this: INSERT INTO table_name (column1, column2, column3) VALUES (value1, value2, value3)

What really trows me off is the fact that the first query works and is inserted with no problem, but then the second one is ignored, and I get no errors from mysqli_query()

also if I copy and paste the $sql1 result to phpmyadmin it works with no issues.

I am going to change the code and try what you suggested.

0

for anyone that runs into this issues, i figure it out:
I needed to move:

/// Truncate Table to perform fresh import.
            $truncateSQL = "TRUNCATE TABLE ".$table_name;
            if(!mysqli_query($link, $truncateSQL)){
              echo "Failed to Truncate Table<br/>";
              die();
            }

Outside the foreach loop,

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.