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 6120819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:42:43+00:00 2026-05-23T15:42:43+00:00

I have the following php file: <?php function show_error($stage) { echo <p>MySql error: .

  • 0

I have the following php file:

<?php
function show_error($stage) {
    echo "<p>MySql error: " . mysql_error() . '</p>';
    echo "<p>ErrorCode: " . mysql_errno() . '</p>';
    die($stage);

}

$link = mysql_connect('localhost', 'blog', 'sql-password');
if (!$link) {
    show_error('connect');
}
mysql_select_db('blog') or show_error();
$result = mysql_query('select col1 from test_table');
if (!$result) {
    show_error('query');
}
$col = mysql_result($result, 0);
echo "<p>Result is: [" . $col . "]</p>";
mysql_close($link);
?>

When I run this, I get error:

MySql error: No such file or directory

ErrorCode: 2002

connect

However, if I change the line

$link = mysql_connect('localhost', 'blog', 'my-password'); 

to

$link = mysql_connect('127.0.0.1', 'blog', 'my-password'); 

it all works correctly. How can I fix this so that it works with either localhost or 127.0.0.1, and why does using localhost work differently to 127.0.0.1?

Extra info (some google responses indicated these were important):

  • the line 127.0.0.1 localhost appears in my /etc/hosts file, and ping localhost works correctly, and hits the 127.0.0.1 address. There are no other lines mentioning localhost in the /etc/hosts file.
  • I’m running it in Mac OSX Snow Leopard.
  • Finally, the result of the command stat /tmp/mysql.sock is:

    234881044 28829652 srwxrwxrwx 1 _mysql wheel 0 0 “Jul 10 18:29:37 2011” “Jul 10 18:29:37 2011” “Jul 10 18:29:37 2011” “Jul 10 18:29:09 2011” 4096 0 0 /tmp/mysql.sock

  • 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-23T15:42:44+00:00Added an answer on May 23, 2026 at 3:42 pm

    The MySQL client magically knows that ‘localhost’ means the local machine, so it tries to connect to a Unix socket instead of making a network request–which is actually more efficient. The problem here is that you’re either not using a Unix socket, or–probably more likely–the MySQL client libraries don’t know where to find the Unix socket file.

    I wish I could tell you where that configuration lives, but a) I haven’t actively used MySQL in over 3 years, and b) I’ve never used MySQL on OSX.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following CSS conditional statement in my function.php file: function logo_exists() {
I have the following CSS conditional statement in my function.php file: function logo_exists() {
I have the following code: if (include_once(dirname(__FILE__).'/file.php') || include_once(dirname(__FILE__).'/local/file.php') ) { This causes an
In PHP I have the following code that gets a file submitted through CGI:
I have the following in a .htaccess file redirect 301 /page.php http://domain.com/page Which works
In my .htaccess file I have defined following rule, RewriteRule t/([^.]+)/$ /videos/tag.php?tag=$1 [QSA] The
I want the following functionality using php I have a csv file. Each file
I have an .htaccess file with following content: AddHandler x-httpd-php5 .php For some reason,
In a php file i have used include to include the following js.php file
Here is my problem. I have one file (say, func.inc.php) with autoloader function, registered

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.