Jump to content

Recommended Posts

Posted (edited)

Alright, this is indepth PHP stuff, and it's mostly aimed at Raul. I need to figure this stuff out.

 

Here is a snippet of code from my website's common file:

 

>function db_connect()
{
global $VARS;

$Link = mysql_connect($VARS["DB_Host"], $VARS["DB_User"], $VARS["DB_Password"]);
mysql_select_db($VARS["DB_Name"]);

return $Link;
}

//----------------------------------------------------------

function db_query($Query)
{
return(mysql_query($Query));
}

//----------------------------------------------------------

function db_row($Result)
{
return(mysql_fetch_row($Result));
}

//----------------------------------------------------------

function db_num_rows($Result)
{
return(mysql_num_rows($Result));
}

 

As you can see, I have setup database functions in my website's common file to handle everything. I've done it this way just in case I ever need to migrate to a new database system rather than MySQL... Anyway...

 

I've got my pages all calling the db_row() etc. functions to interact with my database. Well, my problem is that when there's an error related to MySQL, the error always says it's in my common file... Like so:

 

>[Tue Feb  3 23:33:06 2004] [error] PHP Warning:  mysql_fetch_row(): supplied argument is not a valid MySQL result resource in /blah/blah/blah/commonfile.php on line xxx

 

As you can see from this example, I cannot determine exactly what page has the MySQL error: Because the query is actually executed every time by my common file.

 

Is there a way to have PHP, when outputting MySQL errors, to also include the executing script? Also known as:

 

>$_SERVER["PHP_SELF"]

 

Any possibility? Thanks for help.

Edited by TCH-Robert
Posted (edited)

Robert, $_SERVER['PHP_SELF'] should work for what you want.

Try this:

>function db_row($Result)
{
  $me = $_SERVER['PHP_SELF'];
  $row = mysql_fetch_row($Result) or die("Error getting row in '$me': ".mysql_error());
  return $row;
}

$_SERVER['PHP_SELF'] will refer to the "primary" file being executed, even if the currently executing code is inside a function that resides in another file.

 

As a reference, I tried this to be sure:

>/*   test1.php   */
<?php
  echo "this is the first test file: ".$_SERVER['PHP_SELF']."<br />";
  require_once("test2.php");
  speak();
?>


-----------------------------


/*   test2.php   */
<?php
  function speak()
  {
     echo "this is the second test file: ".$_SERVER['PHP_SELF'];
  }
?>

 

And when I pointed my browser to test1.php, the output was

this is the first test file: /test1.php

this is the second test file: /test1.php

 

Hope this helps :)

Edited by TCH-Raul
Posted

K, I'll take a look into that, thanks..

 

Well, the problem here is that the error doesn't stop my pages fom executing. I don't know what page the error is at on my website, because it appears on the Error Log under CPanel.

 

I don't want it to die(), but I want it to put the name of the PHP_SELF file in the Error message that gets put in the Error Log...

Posted

Humm... I don't know how it works (never used it) but perhaps PHP's error_log() function is what you need.

 

I suggested the die() function because I thought you didn't want that error. With the die() function, you could see where it is coming from and correct the problem but if you only need to log it, try the error_log() function.

 

By the way, what exactly gets written on the error log? That warning shouldn't be written anywhere, I think... :)

Posted (edited)

Just gave it a try and it's quite simple. Try something like this:

>function db_row($Result)
{
  $me = $_SERVER['PHP_SELF'];
  $row = mysql_fetch_row($Result) or error_log("Error getting row in '$me': ".mysql_error(), 0);
  return $row;
}

The error message goes into Apache's error log by default :)

Edited by TCH-Raul
Posted

Thanks Raul, I didn't know about the error_log() function.

 

So far so good, I can't seem to find the problem myeslf. I'm sure whenever it happens I'll be able to find it. Thanks!

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...