0

I have used the below code in mysql query:

$all_PIDs=array();
foreach($pID as $p)
{
    $all_PIDs[]=$p->ID;
}
$AIDS=implode(',',$all_PIDs);
$table_tsk  = new Timesheets_Table_Tasks();
$select_tsk = $table_tsk->select()
            ->from($table_tsk, array
              (
                'Total'=>'SUM(timesheets_tasks.Time)',
                'Charged'=>'SUM(timesheets_tasks.Time_Charged)'
              ))
           ->where('timesheets_tasks.ProjectID IN ('.$AIDS.')')
            ;

But using the above code I am getting the following error:

"An error has occured SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '))' at line 1"

I have added a quotation mark(") for IN clause. But the problem is the query only displays for the first $AIDS number. Could someone help me to clear the error? Thanks!

0

2 Answers 2

0

It should be specified as:

->where('timesheets_tasks.ProjectID IN (?)', $all_PIDs)

so you're passing an array of integers, not the comma-separated list of it

6
  • Thank you for your reply. I have tried your suggestion. It displayed the wrong values. As per your suggestion,the out put is 43:30 28:45. I have tried the hard coded query like : ->where('timesheets_tasks.ProjectID IN(40,118,139)'). then the output is 51:30-34:30. So I think as per your suggestion the query only takes the first parameter(40)
    – NewPHP
    Aug 25, 2013 at 23:57
  • @NewPHP: I have no idea what is 51:30-34:30. "So I think as per your suggestion the query only takes the first parameter" --- it accepts whatever you pass in array. If the result is unexpected - check what you passed in all_PIDs
    – zerkms
    Aug 25, 2013 at 23:58
  • I mean it is the output. I just need to say the output is incorrect as per your suggestion. correct output is 51:30-34:30. But I am getting 43:30 28:45by using ->where('timesheets_tasks.ProjectID IN(?)',$AIDS).
    – NewPHP
    Aug 25, 2013 at 23:58
  • @NewPHP: the output is incorrect per your query parameters, not by my answer. If you see the unexpected results - the issue is with you parameters not with this answer.
    – zerkms
    Aug 25, 2013 at 23:59
  • Sorry, you are not getting my point. Let me explain. $AIDS is 40,118,139 as per my query. I have tried to use ->where('timesheets_tasks.ProjectID IN(?)',$AIDS) and getting output as 43:30 28:45. Then I have tried ->where('timesheets_tasks.ProjectID IN(40,118,139)') and getting output as 51:30-34:30. Hope they are the same query but different results
    – NewPHP
    Aug 26, 2013 at 0:02
0

On your codes the quotes are not part of your MySQL query but only your PHP portion. DO this

$AIDS= "'".implode("','",$all_PIDs)."'";

And then

>where('timesheets_tasks.ProjectID IN ('.$AIDS.')'
3
  • So it will be timesheets_tasks.ProjectID IN ('1,2,3')
    – zerkms
    Aug 25, 2013 at 23:48
  • @zerkms, Oh Yeah. Missed that. I have fixed it now.
    – Starx
    Aug 25, 2013 at 23:52
  • @zerkms, I have fixed my error. Why are you still keeping the downvote? If you are not responsible for that vote I draw my comment.
    – Starx
    Aug 26, 2013 at 0:03

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.