-1

My boss asked me to see if it was possible to generate an HTML table based on SQL data via the company's WordPress site. I'm not super-familiar with PHP or SQL, but I saw that others had been able to do it.

The problem is, after spending a full week on it, I can't seem to pull anything from the server. I've tried dozens of times, but nothing has worked. Every time I tried to connect to the server, it seemed to go through fine, but when I tried a basic query (that was already working in the MySQL workbench), it could never pull any results in.

Today, I did a test that may have solved the issue: I was able to connect to the server, but when I asked for the code to check which database it connected to, it came up blank.

The code:

<?php
$conn = mysqli_connect("IP address", 3306, "socket", "user", "pw");
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}
else {
    echo "Connection active.<br><br>"; }

mysqli_select_db($conn, "dbname")
    or die('Could not select a database.');

?>

And the result is:

Connection active.

Could not select a database.

This code is based on the PHP.net manual, but I've tried a bunch of different variations and they haven't had any luck, either. I have tried using the dbname inside of the $conn variable, but that didn't seem to do anything, either.

If it wasn't already clear, I'm a super-beginner at this and have no idea what I'm doing wrong. I've handled plenty of other site coding issues in the past, and I really don't want this to be the one to defeat me - if anyone out there can help, I'd be eternally grateful.

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • Look at mysqli_error() result to find out the exact error code. Also is there a reason to use a parallel connection to the DB? why not use the WP database API functions? – Eriks Klotins Nov 27 '19 at 17:08
  • Sorry - where exactly would I put that? I tried ```mysqli_select_db($conn, "dbname"); if (mysqli_error()) { printf("Connect failed: %s\n", mysqli_error()); }``` but that just removed the second error message. – Steven Schneider Nov 27 '19 at 17:12
  • Also, to answer the second part of your question: when I looked at the wpdb, the text seemed to indicate that said code was only for using WP's own SQL databases, not an external one. I could be wrong, though - I'll go back and look into it more. – Steven Schneider Nov 27 '19 at 17:42
  • You are definitely not connecting properly, but there is not enough details here to say for us what your connection code should look like. Where is the MySQL server located? Is it local or remote? Do you have a user created with this password? What kind of server is it? MariaDB is sometimes set to port 3307. Check out my answer here, which should give you more idea how to connect properly and how to get the errors: https://stackoverflow.com/a/58808333/1839439 – Dharman Nov 27 '19 at 17:52

1 Answers1

0

It seems like your parameters are out of sequence in mysqli_connect. The correct sequence is ip address or host name, user name, password, database name.

Check this link out

Paulo Hgo
  • 834
  • 1
  • 11
  • 26
  • When I tried to connect without putting the port or socket in manually, it would return a connection error. Previously, I had been including the database name in the code, but when I broke it all down to go step-by-step, that's when the issue reared its ugly head. – Steven Schneider Nov 27 '19 at 17:37