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

  • Home
  • SEARCH
  • 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 8978631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:39:41+00:00 2026-06-15T19:39:41+00:00

I get an error when I use this code to list rows of a

  • 0

I get an error when I use this code to list rows of a table

// Specify your table name
$table_name = 'table_stats';
$hostname = '172.16.11.2';
$username = 'user_stats';
$password = 'stats';
$dbname = 'db_stats';


$dbh  = new PDO("mysql:$hostname;dbname=$dbname",$username,$password);
$stmt = $dbh->query("SELECT * FROM $table_name", PDO::FETCH_ASSOC);
foreach($stmt as $row) { // PDOStatement implement Traversable interface, so you can just loop over the resultset. Sweet!
    print_r($row);
}

?>

and I get this error

 Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)' in /home/mahmedne/public_html/android/updates/test.php:12 Stack trace: #0 /home/mahmedne/public_html/android/updates/test.php(12): PDO->__construct('mysql:172.16.25...', 'mahmedne_stats', 'stats') #1 {main} thrown in /home/mahmedne/public_html/android/updates/test.php on line 12

I can confirm that database settings are 100% correct , using same values if I connect like

     if (!mysql_connect($db_host, $db_user, $db_pwd))
         die("Can't connect to database");

Then it works but not with I do new PDO()

Please advise, is it possible that my is providing limited functionality of PHP ?
I am checking this code on web host and not on my local machine

here is th PDO related info that i got using phpinfo()

PDO

PDO support enabled
PDO drivers sqlite, sqlite2, mysql

pdo_mysql

PDO Driver for MySQL    enabled
Client API version  5.1.65

Directive   Local Value Master Value
pdo_mysql.default_socket    /var/lib/mysql/mysql.sock   /var/lib/mysql/mysql.sock

pdo_sqlite

PDO Driver for SQLite 3.x   enabled
SQLite Library  3.7.7.1

Thanks,

  • 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-06-15T19:39:42+00:00Added an answer on June 15, 2026 at 7:39 pm

    You’re missing host= from your DSN string, it should be like this,

    new PDO("mysql:host=$hostname;dbname=$dbname",$username,$password);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i use this line of code : [self parseXMLFileAtURL:url]; And i get this error
Why do I get this error message with this code? Use of undeclared identifier
I get this error when I use simplejson from django.utils in google app engine
I use the following jquery statements but i get error in this function onGetDataSuccess(result)
I get this error when I try to use time_ago_in_words : Comparison of String
i have this query and i get error in mysql Error Code: 1054. Unknown
In perl 5.8.5, if I do the following, I don't get an error: use
When compiling my project, I get the error: Error: D:\Programming\Projects\Aprovado\examwindow.cpp:6: error: invalid use of
In the below sql statement i get the following error Cannot use an aggregate
I create application where I use google maps. I get error: 06-20 15:23:51.262: W/System.err(28660):

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.