0

I'm trying to figure how to call a query once.

I have 6 different variables for images, title and desc.

In this code, I need to know how to loop for id from 0 to 6.

    $date = new DateTime("NOW");

    $image1 = 'SSSS';
    $title1 = 'AAAA';
    $desc1 =  'BBBB';

    $image2 = 'RRRR';
    $title2 = 'GGGG';
    $desc2 =  'VVVV';

    ///  4 vars later....

    $id = 6;


    $get = $this->db->queryRow("UPDATE `featured` SET `image` = '{$image.$id}', `title` = '{$title.$id}', `desc` = '{$desc.$id}', `date` = '{$date->format('Y-m-d H:i:s')}' WHERE id = '{$id}'");
    return(object) $get;
10
  • This should help: stackoverflow.com/questions/9257505/… Commented Jul 7, 2016 at 19:35
  • @ckimbrell But I don't want to call query six times due to performance reasons, it's possible to update 6 rows once in some sql command. Commented Jul 7, 2016 at 19:43
  • Performance reasons? What are they? Commented Jul 7, 2016 at 19:44
  • @u_mulder I need to keep mininum count of requests to mysql database Commented Jul 7, 2016 at 19:45
  • @Ivan which mysql driver are you using? Commented Jul 7, 2016 at 21:41

3 Answers 3

1

To build a collection of Querys use the multi_query function. Loop to build your Query string to pass to the db and concatenated by a semicolon.

<?php
for($i=0;$i <= $maxquerys;$i++){
  $query = "UPDATE `featured` SET `image` = '".$image.$id."', `title` =     ".$title.$id."', `desc` = '".$desc.$id."', `date` = '".$date->format('Y-m-d H:i:s')."' WHERE id = '".$id."';"
}

/* execute multi query */
if ($mysqli->multi_query($query)) {

  while ($mysqli->next_result());
}

/* close connection */
$mysqli->close();

you may check also the result by

echo  $mysqli->affected_rows;
?>
Sign up to request clarification or add additional context in comments.

Comments

1

I have tried to use a simple $query model and it works fine. Create a valid Query string to pass to the db

<?php
$query = "UPDATE `featured` SET `image` = '".$image.$id."', `title` = ".$title.$id."', `desc` = '".$desc.$id}."', `date` = '".$date->format('Y-m-d H:i:s')."' WHERE id = '".$id."';"

$result=$mysqli->query($query);
  // Verify results
if(!$result) {
  $ErrMessage  =  "ErrSqlQuery:" . $mysqli->error . "\n";
  $mysqli->close();
  die($ErrMessage);        
}

you can check also the result by

echo  $mysqli->affected_rows;
?>

1 Comment

I think the author wants minimum number of queries. this would require x queries and connections for x length arrays.
1
$query_build = "";
foreach($arr as $$image){
    $query_build .= "UPDATE `featured` SET `image` = '{$image.$id}', `title` = '{$title.$id}', `desc` = '{$desc.$id}', `date` = '{$date->format('Y-m-d H:i:s')}' WHERE id = '{$id}';";
}
$get = $this->db->queryRow($query_build);

Accumulate all the queries and execute all at once.

Comments

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.