0

I've been stuck on this problem for a while now. I am trying to get the results to display from the api if there are no entries in the db that match the id thats being called from the api and then store those results into the db if there is a Id that matches then pull the results from the db and display them instead of displaying the results from the db.

I keep running into this error which is causing the page to go blank when being displayed PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column '29f1fc0bbd3dfe65a9e0ba6e044fb8e3398967a3' in 'where clause': SELECT * FROM {places} Where {ID} = 29f1fc0bbd3dfe65a9e0ba6e044fb8e3398967a3; I guess its because it cant find the id in the db, but it should still show the results from the api.

My logic makes sense im not sure what I am missing, Any suggestions?

       $listings_key = 'xxxxxxx';
            $xml = file_get_contents('https://maps.googleapis.com/maps/api/place/textsearch/xml?query=loans+near+'.$city_name.'&sensor=true&type=bank&key='.$listings_key.'');
            $listings_request = simplexml_load_string($xml);

            $loans_places = '';
            $loans_places .= '<div class="city-listings-wrapper">';

            foreach($listings_request->result as $pre_list){
                $xml2 = file_get_contents('https://maps.googleapis.com/maps/api/place/details/xml?reference='.$pre_list->reference[0].'&sensor=true&key='.$listings_key.'');
                $xml2 = simplexml_load_string($xml2);
                $l_ID = reset($xml2->result->id[0]);

                $dbresult = db_query("SELECT * FROM {places} Where {ID} = ".$l_ID."");      

                foreach($dbresult as $db_inlist){

                    dpr($db_inlist);

                    $places = array(
                        'Name'      => $xml2->result->name[0],
                        'Address'   => $xml2->result->adr_address[0],
                        'Phone'     => $xml2->result->formatted_phone_number[0],
                        'Website'   => $xml2->result->website[0],
                        'Ref_ID'    => $xml2->result->reference[0],
                        'ID'        => $xml2->result->id[0],
                        'City'      => ucwords(arg(2)),
                        'State'     => arg(1)
                    );


                    if($l_ID != $db_inlist->ID){ 
                        $loans_places .= '<div class="city-listings">';
                        $loans_places .= '<b>'.$xml2->result->name[0].'</b></br>';
                        $loans_places .= $xml2->result->adr_address[0].'</br>';
                        $loans_places .= (isset($xml2->result->formatted_phone_number[0]) ? $xml2->result->formatted_phone_number[0].'</br>' : '');
                        $loans_places .= (isset($xml2->result->website[0]) ? l('website', $xml2->result->website[0]).'</br>' : '');
                        $loans_places .= '</div>';

                        //execute store into db function
                        $nid = db_insert('places')->fields($places)->execute();

                    }else{
                        $loans_places .= '<div class="city-listingsdb city-listings">';
                        $loans_places .= '<b>'.$db_inlist->Name.'</b></br>';
                        $loans_places .= $db_inlist->Address.'</br>';
                        $loans_places .= (isset($db_inlist->Phone) ? $db_inlist->Phone.'</br>' : '');
                        $loans_places .= (isset($db_inlist->Website) ? l('website', $db_inlist->Website).'</br>' : '');
                        $loans_places .= '</div>';
                }
            }
        }

    $loans_places .= '</div>';

return $loans_places;
2

You're currently escaping a bit too much...you only need to use the {} placeholders for table names. With an extra small change to protect you from SQL injection, your code should be something like:

$args = array(':id' => $l_ID);
$dbresult = db_query("SELECT * FROM {places} Where ID = :id", $args);  

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.