0

I have below select statement which is returning Nearby Available user_id's with some other column value

$sql="(SELECT
    user_location.*,
    driver_location.user_id as user_id,
    ( 3959 * acos( cos( radians(37.4219983) ) * cos( radians( driver_location.latitude ) ) * cos( radians( driver_location.longitude ) - radians(-122.0844) ) + sin( radians(37.4219983) ) * sin(radians(driver_location.latitude)) ) ) AS distance 
FROM user_location, driver_location
HAVING distance >= 0)";

Now For each user_id from above query I need to Insert data to request table with some other details.For each user_id there will be a separate row with other details.I have tried below as an array but it't not saving any data

foreach ($sql as $key => $value) {

        $data[] = [
        'pickup_latitude'     => $pickup_lat,
        'pickup_longitude'   =>   $pickup_lan,
        'car_id'   =>   $car_id,
        'user_id'     =>   $value->user_id,
        'user_mobile'   =>$umobile         
    ];
} //for loop

$columns = implode(", ",array_keys($data));
$escaped_values = array_map('mysql_real_escape_string', array_values($data));
$values  = implode(", ", $escaped_values);
$stmt = $con->prepare("INSERT INTO `request`($columns) VALUES ($values)");
$stmt->execute();
9
  • 2
    Why don't you save inside the loop while using prepared statements? Commented Mar 28, 2020 at 10:28
  • can you show us what is your $columns et $values ? Commented Mar 28, 2020 at 10:28
  • 2
    I think you should dump the use of mysql_real_escape_string (Don't use the deprecated mysql_* functions at all) and start using prepared statements ASAP Commented Mar 28, 2020 at 10:37
  • 1
    phpdelusions.net/pdo (for PDO) or phpdelusions.net/mysqli (for mysqli) Commented Mar 28, 2020 at 10:38
  • See this answer might help stackoverflow.com/a/60018884/12232340 atleast you can see how to get array out of for each. Commented Mar 28, 2020 at 11:37

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.