Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6529213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T09:36:36+00:00 2026-05-25T09:36:36+00:00

Environment – PHP5.2.17, MySQL5.0.92 I am implementing an error logging framework in my application

  • 0

Environment – PHP5.2.17, MySQL5.0.92

I am implementing an error logging framework in my application and I am trying to log errors and warnings to a database.

$db = mysql_connect('host', 'database', 'password');
if (!$db)
{
  log(mysql_error());
}
...continue code

I wanted to test the logging and passed the wrong host to the mysql_connect function. As expected I got the warning on screen

Warning: mysql_connect() [function.mysql-connect]: Unknown MySQL server host 'host' (1) in /path/to/my/php/file.php on line 4

And my log function was called but mysql_error() is null. According to the php manual (http://php.net/manual/en/function.mysql-connect.php), mysql_error() should return the last mysql error. In the manual they even have an example of how to echo the error to the screen using mysql_error().

I tried using mysql_error($db), but it returns an error stating that $db is not defined.

I tried replacing my function with echo mysql_error(); and I doesn’t show anything.

I tried using mysql_errno(); and I get null also.

I know the script is catching the warning because it is outputting it to the screen.

One way to resolve my problem is to store the warning in a variable.

I appreciate any help anyone can provide. Thank you.

UPDATE – 2 SOLUTIONS:

SOLUTION 1: (thank you to Mario for suggesting this in one of his comments to this post)

Use PHP function error_get_last() – reference (http://php.net/manual/en/function.error-get-last.php).

New working code with this solution:

$db = mysql_connect('host', 'database', 'password');
if (!$db)
{
  $error = error_get_last();
  log($error['type'] . ": " . $error['message'] . " in file " . $error['file'] . " on line " . $error['line']);
}
...continue code

SOLUTION 2: (thank you to PauPRO for suggesting this in answer 1 below)

Before using $php_errormsg, we need to set track_errors to 1 in the php.ini file. Just include the following line in your php.ini file

track_errors = 1

Alternatively you can set track_errors to 1 inside the script see below the code with this alternative:

ini_set('track_errors', 1);
$db = mysql_connect('host', 'database', 'password');
if (!$db)
{
  log(mysql_error());
}
...continue code

Both solutions work!!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T09:36:37+00:00Added an answer on May 25, 2026 at 9:36 am

    Make sure you turn on track_errors before using $php_errormsg:

    // Preferably do this in your php.ini file, instead of in code.
    ini_set('track_errors', 1); 
    
    $db = mysql_connect('host', 'database', 'password');
    if (!$db)
    {
        log($php_errormsg);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Environment Visual Studio 2008 SP1 Visual C# WPF Application Project .NET Framework 3.5 Problem
Environment: Windows Server 2003 R2 Enterprise 64bit, SP2 .NET framework is supposedly installed (2.0
Environment I'm working on a C++ application that uses SQL Native Client 9.0 to
Environment: .net 3.5 WinForms application I am looking for some ideas how to stop
Environment: xcode 3.2.1, document-based core-data application. I have a document-based cocoa app which uses
Environment.SpecialFolder.CommonApplicationData *returns C:\Documents and Settings\All Users\Application Data under XP that is writeable for all
Environment: Visual Studio 2008 SP1 How can I write the following regex? I'm trying
Environment : Visual Studio 2010 Professional .NET Framework 4 C# Added Service Reference using
Environment: - PHP 5.3.5 - Oracle 11g database Table Name: tips_categories id category picture
Environment: NetBeans 6.9.1, GlassFish 3.1 I have a Java Web Application. How to get

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.