-1

Working with a Wordpress website that is being hosted by Godaddy. I am familiar with creating custom templates and scripting pages (php) and running them on this specific Wordpress webpage. I am at the point where I need to access my mysql database and interact with custom templates+data via programmatically. Just in the past few days I've searched hundreds threads+tutorials and copied examples with no luck. I have a created a php file called display_data.php Code:

<?php /* Template Name: display_data */ ?>

<?php
$db_host = 'localhost'; // Server Name
$db_user = 'root'; // Username
$db_pass = ''; // Password
$db_name = 'tutorial'; // Database Name

$conn = mysql_connect($db_host, $db_user, $db_pass, $db_name);
if (!$conn) {
    die ('Failed to connect to MySQL: ' . mysqli_connect_error());  
}

$sql = 'SELECT * 
        FROM sales';

$query = mysqli_query($conn, $sql);

if (!$query) {
    die ('SQL Error: ' . mysqli_error($conn));
}
?>

I get this error: "Failed to connect to MySQL: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)"

I have had no luck figuring out what this error specifically means in my case and I've searched for many hours.

I am at the point where I believe I am missing something stupid simple that I am looking past.

Either I am filling the parameters wrong - $db_user, $db_pass, $db_name, or missing something entirely.

I used the same parameters (User name, password) from the ones I got from Godaddy to access phpmyadmin, and for $db_name I used the one I see in my database (picture).Taken from my database, I used what is said to the right of "Database:"

Taken from Gogdaddy (Database PHPMyadmin - View)

Please any help is greatly appreciated, and be nice. I've never used mysql before but I am also not a total noob to programming.

Thank you.

sorak
  • 2,607
  • 2
  • 16
  • 24
Dave DF
  • 3
  • 2
  • Hi Dave. For WordPress related issues, please use http://wordpress.stackexchange.com/ – Greeso Mar 13 '18 at 17:15
  • ok, I will post this there. – Dave DF Mar 13 '18 at 17:21
  • Funky Forty Niner, I am NOT trying to mix mysql and mysqli together, that was a mistake I made when pasting the code, I was trying to see if mysql worked when mysql"i". This isn't a duplicate so hold your horses. – Dave DF Mar 13 '18 at 17:36

1 Answers1

0

Why dont you just try using wpdb? This is what you should use to work with secondary databases in WP.

$mydb = new wpdb('username','password','database','localhost');
$rows = $mydb->get_results("select Name from my_table");
echo "<ul>";
foreach ($rows as $obj) :
echo "<li>".$obj->Name."</li>";
endforeach;
echo "</ul>";
  • Ah, I've been switching between mysql and mysqli and had those mixed up. I fixed that and am now getting a different error which is a great thing lol Error: Failed to connect to MySQL: Edit: actually getting same error (I didn't replace parameters after I had switched to "mysqli" – Dave DF Mar 13 '18 at 17:23
  • Check your database information. Something is not right there ... – CarpinchoDem Mar 13 '18 at 17:26
  • Just edited post. You should try with wpdb. – CarpinchoDem Mar 13 '18 at 17:32
  • Ok I will try that, thank you – Dave DF Mar 13 '18 at 17:39
  • Yes, that worked! Thank you SOOOOO MUCH CarpinchoDem, a thousand thank you's to you. You have no idea the headache and frustration this gave me. The the answer is you don't use mysqli when dealing with wordpress, use wpdb ect... Thank you – Dave DF Mar 13 '18 at 17:53