2

I'm new to PHP (and CodeIgniter) and I'm having trouble trying to get a simple call of $this->db->query() to work. In my code (see bottom of post) I am looking through the errors and warnings that have been logged in my database table eventLog and comparing each to a list of accepted error messages. If any message isn't found to be on that list, then I want to acknowledge it by setting "ack = 't'". However the code can't successfully run and gives the message:

Error Number: 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

UPDATE eventLog SET ack = 't' WHERE id =

This message seems to indicate that "$row['id']" is not working the way I intended. It should give me the id of the error or warning, but the error suggests that it's giving nothing, or in the wrong format. What am I missing? Or is there a better way to do this that may bypass my problem?

MY CODE:

$acceptedMessages = array("whatever",
                          "whateverelse"
                          );

$sql = "(SELECT id, mesg FROM eventLog WHERE level = 'error') UNION   
          (SELECT id, mesg FROM eventLog WHERE level LIKE 'warn%')" ; 

foreach ($this->db->query($sql) as $row) { 
  foreach ($acceptedMessages as $messagePart) { 

    $pos = strpos($row['mesg'], $messagePart); 
    if ($pos !== false) { 
      continue 2; 
    } 
  } 
  // if we get here the eventLog message didn't match any accepted messages, so
  // acknowledge it
  $idNum = $row['id']; 
  $sql3 = "UPDATE eventLog SET ack = 't' WHERE id = " . $idNum; 
  $res = $this->db->query($sql3); 
  if (!$res) { echo "Failed to acknowledge error/warning with id " . $row['id'] . " in eventLog"; } 
} 

2 Answers 2

1

$this->db->query($sql) returns a result object, but not an array, so that foreach shouldn't be working. Try this instead:

$query = $this->db->query($sql);
foreach ($query->result_array() as $row) {

Check out the docs for more options: http://ellislab.com/codeigniter/user-guide/database/results.html

Sign up to request clarification or add additional context in comments.

Comments

1

you could write simple query, like, change:

$sql = "(SELECT id, mesg FROM eventLog WHERE level = 'error') UNION   
          (SELECT id, mesg FROM eventLog WHERE level LIKE 'warn%')" ;

to

$sql = "SELECT id, mesg FROM eventLog WHERE (level = 'error' OR level LIKE 'warn%')" ;

AND:

$sql3 = "UPDATE eventLog SET ack = 't' WHERE id = " . $idNum; 
$res = $this->db->query($sql3); 

to

$act = 't';
$sql3 = "UPDATE eventLog SET ack = ? WHERE id = ?"; 
$res = $this->db->query($sql3, array($act, $idNum)); 

or

$act = 't';
$data = array(
    'act' => $act
);

$this->db->where('id', $idNum);
$this->db->update('eventLog', $data);

See More

2 Comments

Also I am aware of the inefficient parts I've put in the code. The first one you point out is because I've simplified it from how it actually appears in the code, where the UNION is necessary. And the second one I made inefficient just to pull the pieces of my problem apart and see if I could tinker with them to solve it.
Alright I'll try rewriting my code a bit, though I'm hoping someone else will figure out my mistake.

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.