when changing database local to online page blank - php

I've recently made a new database that I had local. Everything worked fine and the scripts I've used (mysql and php codes) have been working properly. Since I've changed the database from local to online it has only caused problems with my script. Of course I've changed the details of connecting to MySQL database (having a password and such). Also, the content after the script won't work either.
Note: I haven't changed my script AT ALL. I've only changed the connection part.
$dbhost = "localhost";
$dbuser = "dbuser";
$dbpass = "dbpass";
$dbname = "compunll_itnj01";
$con = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$so = $con->prepare("SELECT * FROM besteloverzicht");
$so->execute();
--- rest of code
My apologies if I've forgotten to put anything further here. You can ask me and I'll respond asap!

Try to set the ip address of your db online
$dbhost = 'xx.xxx.xxx.xxx';

Related

How do you fetch from a mysql database?

I'm relatively new to any type of programming or coding. I'm not quite understanding why no matter what adjustments I make to my php file I can't seem to pull any data from a table.
Here is a link to the table: https://i.gyazo.com/4ad5e860895014c49dbe0539c38cdec2.png
Above is the test table I have been trying to use. From what I can understand I'm connecting to the database okay, but all of my problems come after the connection. Also, I'm using php 7.0 so a lot of the information I'm finding online has not been helpful.
If there is something glaringly wrong with my table or in my code, please let me know.
Here is my code:
'''
//Set Variables
$serverName = "localhost";
$userName = "root";
$password = "";
$databaseName = "test";
//Create Connection
$connection = mysqli_connect($serverName,$userName,$password,$databaseName);
//Check Connection
if(!$connection){
die("Connection failed: ".mysqli_connect_error());
}
echo "Connected successfully <br>";
//Fetch Data
$query = "SELECT * from table1";
$result = mysqli_query($connection, $query);
$row = mysqli_fetch_array($result, MYSQLI_NUM);
printf ($row[1], $row[2]);
mysqli_free_result($result);
mysqli_close($connection);
I figured out the issue a few hours ago. The code I had posted would have worked perfectly fine if I was connecting to the port for MySQL rather than MariaDB. Didn't realize that the port that MariaDB was connected to was the default.
MariaDB by default was port 3306, but MySQL was 3308. After specifying 'localhost:3308' I was able to start properly pulling rows from my tables.

Wordpress, custom template php pages, phpmyadmin > mysql, unable to connect database [duplicate]

This question already has answers here:
Can I mix MySQL APIs in PHP?
(4 answers)
Closed 4 years ago.
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.
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>";

how to set mysql database connection name

I'm trying to figure out how can I put two differents MYSQL database connections name on the same page, one is for local PC and one is for hosting server.
Does is possible to have two different databases servers name on the same page so that way not to change connection database name in a script before upload.
I have code like this for local PC
<?php
$username = "your_name";
$password = "your_password";
$hostname = "localhost";
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
?>
Can can i add another connection name in the server in same page without changing the connection database!
alidad
Yes, a single page may query as many different servers as needed:
<?php
$server1 = new mysqli("server1.example.com", "user1", "password1", "database1");
$server2 = new mysqli("server2.example.com", "user2", "password2", "database2");
$result = $server1->query("SELECT 'Hello user' AS _message FROM DUAL");
$row = $result->fetch_assoc();
echo htmlentities($row['_message']);
$result = $server2->query("SELECT 'Hello user' AS _message FROM DUAL");
$row = $result->fetch_assoc();
echo htmlentities($row['_message']);
The way is to have a config.ini file in your localhost, and a different one in your production environment. This way you will parse this file and get your credentials.
A better way to do that is using this library: https://github.com/vlucas/phpdotenv
It works basically on the same way, but is easy to maintain.

need help traying to access my database with mysql

Just built my first database to test out learning skills using the simple query below:
<?php
mysqli_connect("xxx","yyy","zzz" , "newtone" );
if (mysqli_connect_error()){
echo ("could not connect to database");
}
?>
This is what I got
:(42000/1044): Access denied for user 'yyy'#'xxx' to database 'newtone'......
I don't get it. So When i Use this:
mysqli_connect("xxx","yyy","zzz" );
if (mysqli_connect_error()){
echo ("could not connect to database");
}
?>
I don't get an error message, just a blank page (notice how the database name is not included on the login argument). What am I doing wrong?
You want to connect with database then need three things for create connection dbhost username,dbhost password and your database name.
<?php
$dbhost = "localhost";
$dbuser = "root"; //In case of localhost (default db username)
$dbpass = ""; //In case of localhost
$dbname = "testDb";//Your database name
$con = mysqli_connect($dbhost,$dbuser,$dbpass,$dbname );
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
Your 2nd example works because you are successfully connecting to the MySql server, and defaulting to a default database other than "newtone"
If you tried a command like mysqli::select_db after connecting, you would see the same error. http://php.net/manual/en/mysqli.select-db.php
The crux of your problem seems to be permissions for your user yyy. Make sure user yyy has various permissions like read/write on the DB "newtone"

Parse error: syntax error, unexpected 'mysql_select_db' (T_STRING)- cannot see my error?

I am trying to connect to a simple database on XAMPP using php- I know the database exists as I can see it on PHPMyAdmin and have created a table called students and added some data.
I have tested that I can run a simple test.php file ( from the htdocs folder on the XAMPP drive) and get a response. I cannot spot what is stopping me connecting to my database- can anyone help?
<?php
// connect to the database
$user_name = "root";
$password = "";
$database = "computing";
$host_name ="localhost";
$con=mysql_connect($host_name,$user_name,$password);
mysql_select_db($database);
//check connection
echo "Connection opened";
mysql_close($con);
?>
Could you please try the following code if it works?
<?php
// connect to the database
$user_name = "root";
$password = "";
$database = "computing";
$host_name = "localhost";
$con = mysqli_connect($host_name ,$user_name ,$password,$database) or die("Error " . mysqli_error($con));
//check connection
echo "Connection opened";
mysql_close($con);
?>
mysql commands are not going to be supported in future releases, so it would be best perhaps to use mysqli or PDO connections.
Also PDO uses parameters (the syntax might take a bit to make sense), so it is great to reduce risk from SQL Injections.
Mysqli: http://php.net/manual/en/function.mysqli-connect.php
PDO: http://php.net/manual/en/class.pdo.php
The code above should work. Maybe try mysql_select_db($database, $con);

Categories