How to connect to remote database on Wordpress - php

Probably been covered but I keep getting "connecting to the Wordpress database" which isn't what I'm after. In a nutshell I have a perfectly fine Wordpress site, I also have a MySQL database on another server which has some data I'm after. The code works fine on phpfiddle (is this the standard?) but whenever I try to use it on the Wordpress site, either with shortcodes or in the header.php it keeps hanging, throwing errors and won't connect to the DB, any ideas why?
Edit: To make things clearer, it's a meteorological database I want to connect to in order to pull things like Temperature from. Nothing native or related to Wordpress.
<?php
// Create connection
$con=mysqli_connect("ip","user","pass","db");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_close($con);
?>
Gives me this
Warning: mysqli_connect(): (HY000/2003): Can't connect to MySQL server on...

You should probably be looking at the WordPress WPDB class - https://codex.wordpress.org/Class_Reference/wpdb
For example:
<?php
$wpdb_dbconnect = new wpdb(user, pass, db_name, localhost);
$wpdb_dbconnect->show_errors();
$object = $wpdb_dbconnect->get_results("SELECT * FROM table_name");
foreach ($object as $person) {
echo $person->name . ', ';
}
?>

To connect to a second (and third, and forth...) database, I recommend HyperDB plugin.

Related

Why is my PHP short-code printing text instead of performing function when trying to connect to MYSQL database on Wordpress?

I'm trying to display a table from my MYSQL database on a website I'm creating on Wordpress. I'm using the code shown in the code block. This code is within a PHP short-code snippet and added to a Wordpress widget. Once I get past the ">" of <your-MySQL-database-password>, the short-code seems to screw up, and starts to print the text of the code as opposed to actually making the connection. The text that prints is:
, ,'', 'Connect'); if (!$conn){ echo 'Connection error:
mysqli_connect_error(;}?>
When I delete the ">" at the end of <your-MySQL-database-password> all of the printed text disappears except for the `, , at the very beginning.
<?php
$conn = mysqli_connect(
<location-of-your-database>,
<your-MySQL-database-username>,
'<your-MySQL-database-password>',
'Connect');
if(!$conn){
echo 'Connection error: ' . mysqli_connect_error();
}
?>
I suspect if I had a better fundamental knowledge of PHP I could figure this one out pretty easily.
Writing the text into the short-code and skipping the snippet
Creating the snippet and entering the snippet identifier into the short-code block (same result as 1.)
Deleting characters to see what's effecting the rest of the code.
Looked up to see what issues people have with snippets/shortcode on Wordpress
This might helps
<?php
$con = mysqli_connect("localhost","your-db_username","your-db_password","my_db");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
exit();
}
?>

frequent lost of connection to mySQL server

I instal wamp server locally on my PC.I create a user registration form using php and mySQL database for pratice.I was able to connect to my mySQl data server and if i input data on the form and submit is work successfully.I normal experience at time when i access the user registration form it lost connection to my SQLdata server even though my wamp server is on.I will troubleshoot by changing some of the parameter by using mysqli_connect() to check if i will be prompted unsuccessful connection still nothing will show.When i check some other time it will work successfully.Please what may be causing this issue?
Run wampserver as administrator by right clicking wampserver.exe file. It may help.
Edit:
With the type of english you are using to produce the problem and giving no code is making it difficult to predict what the problem actually is. But I think there is something wrong with your mysqli connection file.
Add this code bit to the top of your registration.php file like this:
<?php
$connection = mysqli_connect('localhost','root','');
if(!$connection) {
die("Failed to connect" . mysqli_error($connection));
}
else {
echo "Connection Established";
}
$select_db = mysqli_select_db($connection, 'db2');
if(!$select_db) {
die("Database selection failed" . mysqli_error($connection));
}
else {
echo "Database selected";
}
?>
<?php
////rest of ur registration code goes here

Accessing SQL Server 2012 from php under Linux

I'm in troubles...
I have a web server apache/php under linux, also i have another server with SQL Server database under windows. I need to get access to SQL Server database from my apache/php.
What do i need, please help. I have no idea how to start.
**sorry for my english.
Could do with a bit more clarification but I'll try my best..
If you mean through the code, you would do it like this:
Create a new php file called "connect.php" and insert all of this code, replacing the tags like USERNAME with the correct details.
<?php error_reporting(E_ALL); ini_set('display_errors', 1);
// Create connection
$con = mysqli_connect("HOST","USERNAME","PASSWORD","DATABASE");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$select_db = mysqli_select_db($con, 'DATABASE');
if (!$select_db){
die("Database Selection Failed" . mysqli_error($con));
}
?>
Next, EVERY file that requires a connection to the database, simply add
include("connect.php"):
To it at the start of the page and it should be fine :)
I seriously recommend you read up a bit more though as you seem like a bit of a newb (we all started somewhere)!
Hope this is what you were after!
Look for dblib, you will need to add that driver.

Can't connect to database "Fatal error: Call to undefind function mysqli_connect()"

I've been scouring this site for 5 hours now trying to get this sorted, I rarely ask for help but this is one of the weirdest and most annoying things I've encountered.
First of all I'd like to say that this DID work fine, I have limited examples of what the cause is but I'll list them anyway.
Here's the full error message:
Fatal error: Call to undefind function mysqli_connect() in C:\wamp\www\game\connect.php on line 3
And here's the code
<?php
// Create connection
$con=mysqli_connect("localhost","root","","game");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$select_db = mysqli_select_db($con, 'game');
if (!$select_db){
die("Database Selection Failed" . mysql_error());
}
?>
Weird thing is, it was working completely fine and just suddenly stopped, this has happened more than once.
Here's what I've tried:
Checking the extensions are enabled -
Rebooting various times -
Setting the correct php path -
Using many example codes that "work" -
I also had some code that inputted data straight from phpdesigner into the database, which successfully worked, but that no longer works and I've made literally 0 changes.
The last time it stopped working, I filled out a registration form on my site as a test (that doesn't work either) and it suddenly went off. When filling in the form I click register and nothing happens besides a refresh.
Bit extra: In my httpd file the pfp and pfpinidir are as follows
php5_module"c:/wamp/bin/php/php5.5.12/php5apache2_4.dll"
PHPIniDir "C:\wamp\bin\apache\apache2.4.9\bin\php.ini"
Your mysqli extension might not be enabled. so u need to enable that.
You have two PHP binaries on your machine. One is connected to the Apache, and is the one u use when you surf to a web page.
The phpinfo() shows you which php.ini file is used by the web php binary. u need to edit this file and in it activate the mysqli extension
You're trying to connect to DB twice, plus you're mixing MySQL APIs with mysql_error(), they do not mix together.
The 4th parameter is the DB name which is what you've done in the first example.
Use either:
<?php
// Create connection
$con=mysqli_connect("localhost","root","","game");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
or omit ,"game" from mysqli_connect() - while mysqli_error() requires DB connection parameter.
<?php
// Create connection
$con=mysqli_connect("localhost","root","");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$select_db = mysqli_select_db($con, 'game');
if (!$select_db){
die("Database Selection Failed" . mysqli_error($con));
}
?>

Php / Html - database disconnect

I have a working php code which simply connects database lists the data and closes it.
I embedded this working php code into my html but it does not parse the rest of the html. It is simple blank after listing data from database and php close tag " ?> ". I tried to get rid of " $db->disconnect(); " in php and now it works.
Do you have any idea why?
Do i really need to use $db->disconnect(); every time?
Thanks
beginning of the html code
<ul>
<?php
$db = new mysqli('localhost', 'root', 'root', 'troy');
if (mysqli_connect_errno()) {
printf("Can't connect to MySQL Server. Errorcode: %s\n", mysqli_connect_error());
exit;
}
$query = "SELECT * FROM platters";
$result = $db->query($query);
while($row = mysqli_fetch_array($result)) {
echo "<li>" . $row['name'] . "</li>";
}
$db->disconnect();
?>
</ul>
rest of the html code
$db->disconnect(); is not listed under the documentation, is this a custom function that you have created?
Instead of disconnecting the entire database connection, you should just perform:
$result->close();
this will free up the current query, and enable you to perform further connections throughout that PHP Script.
Only close the connection to the database entirely if you are done with your SQL Connection for that script
Edit
After re-reading your question, $db->disconnect(); is not a function for MySQli. See the entire documentation here:
http://php.net/manual/en/book.mysqli.php
You should use:
$db->close(); which closes the current connection to the database (if used on your database variable)
http://www.php.net/manual/en/mysqli.close.php
Run it on the command line
php test.php
and see the error message
PHP Fatal error: Call to undefined method mysqli::disconnect() in test.php on line 28
This is the reason, why it outputs everything from the database, but omits everything after that line.
When you remove the $db->disconnect(); statement, the error is gone and the PHP script can work it's way until the end.

Categories