Welcome Guest, Not a member yet? Register   Sign In
Database Error: Show Line Number
#1

[eluser]wilso417[/eluser]
I have a huge site that is using a lot of AJAX and many queries. It is really difficult me to debug database errors without knowing which file and line the query failed. I cannot find a way to turn this on. Any ideas?
#2

[eluser]John_Betong[/eluser]
A bit more information would be helpful.

Can you show a typical error.

Is it your code, GIGO?

Are you checking for results?

If you are not using a log file then you should.

Try this:
Code:
// Test for YOUR_EXPECTED RESULT
// varies for updates, inserts, finding counting rows
//
if (! YOUR_EXPECTED RESULT)
  {
    log_message('ERROR', __LINE__ .': ' .__METHOD__);
    log_message('ERROR', $this->db->last_query());
  }

Also take a look at the help file on Transactions.
 
 
 
#3

[eluser]wilso417[/eluser]
Here is my code:

$this->db->select('junk');
$this->db->from('news');
$this->db->where('id', $content_id);

Here is my database error that gets outputted on screen:


A Database Error Occurred

Error Number: 1054

Unknown column 'junk' in 'field list'

SELECT `junk` FROM (`news`) WHERE `id` = '160'

I would like that error to have the following as well:

content_m.php line 360

I have a lot of queries in one page load and it's not easier for me to track down which one is causing an error.
#4

[eluser]John_Betong[/eluser]
[quote author="wilso417" date="1285359007"]Here is my code:

$this->db->select('junk');
$this->db->from('news');
$this->db->where('id', $content_id);

Here is my database error that gets outputted on screen:


A Database Error Occurred

Error Number: 1054

Unknown column 'junk' in 'field list'

SELECT `junk` FROM (`news`) WHERE `id` = '160'

I would like that error to have the following as well:

content_m.php line 360

I have a lot of queries in one page load and it's not easier for me to track down which one is causing an error.[/quote]
 
I do not understand why you should search for the 'junk' field if it does not exist?
 
I think it is a case of GIGO (Garbage In, Garbage Out). Clean up your input and you should have less errors.
 
 
#5

[eluser]billmce[/eluser]
If you're developing on localhost check out Xdebug.
#6

[eluser]wilso417[/eluser]
I used 'junk' just to give you an example. The question I was trying to get at was below that.

My issue is sometimes I have 10+ queries on one page load in multiple models, libraries, etc. and I get a query error and I cannot find which query caused the error. So I was trying to see if I could get CI to output the line number and file on a query error. Is this not possible?
#7

[eluser]John_Betong[/eluser]
Try this:
Code:
function sql_error($method, $line, $sql)
{
  echo '<br />', 'method() ', $method;
  echo '<br />', 'line : ',   $line;
  echo '<br />', 'SQL: ',     $sql;
  
  // or if you have your config error logging setup
  log_message('ERROR', 'SQL error:' .$method  .': ' .$line  .$sql);

  die;
}


// Your Query
// TRYING TO PRODUCE AN ERROR
  $this->db->select('junk');
  $this->db->from(‘news’);
  $this->db->where(‘id’, $content_id);  

  $query = $this->db->get();

  if($query == NULL OR empty($query))
  {
    $this->sql_error(__METHOD__, __LINE__, $this->db->last_query());
  }

  // no error so do your stuff
#8

[eluser]wilso417[/eluser]
The issue with that is I have to add that if statement to every single query I write. I want the CI database class to do this automatically on query error.
#9

[eluser]John_Betong[/eluser]
>>> The issue with that is I have to add that if statement to every single query I write.
>>> I want the CI database class to do this automatically on query error.

I have just unsuccessfully tried the database.php configuration but could not get it to work on my Localhost. Give it a try and see if it works on you computer.

// config/database.php
// ['db_debug'] TRUE/FALSE - Whether database errors should be displayed.

I also tried PHP try/catch but that did not work.

I also tried setting the PHP error_level(0); and that did not work.

I would be tempted to test for the $query and act upon the result.
&nbsp;
&nbsp;
&nbsp;
#10

[eluser]wilso417[/eluser]
Yes, I have ['db_debug'] = TRUE;

That will show me my database error but will not give me the file/line number. I am thinking I would have to somehow extend the db class to accommodate this. I'm just surprised there is not a built in way to do this in CI.




Theme © iAndrew 2016 - Forum software by © MyBB