I have tried a number of things to connect to my database in cloud 9 but I keep getting similar errors.
This is my PHP code:
<?php
// Create connection
$con=mysqli_connect($IP, "$C9_USER", "", "c9");
//(host,username,password,dbname)<- guide for me
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
I took the basic code from w3schools and I used it with a document explaining how cloud 9's mysql database works: https://docs.c9.io/setting_up_mysql.html
But I can't seem to connect without getting the follow error:
Failed to connect to MySQL: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
So I'm wondering if there's a different way to change the socket to the address that cloud 9 recommended: Note: MySQL socket file can be found in ~/lib/mysql/socket/mysql.sock
Credit to Loz Cherone
When using Cloud 9 IDE, the php variables $ID and $C9_USER mentioned in this article are not defined.
In order to retrieve these variables for use in your code, you must use the cloud 9 ide terminal by pressing ALT + T and entering:
echo $ID
echo $C9_USER
Then take those values and place them in a variable in your php code like so:
<?php
// Create connection
$IP = "value from terminal";
$C9_USER = "value from terminal";
$con=mysqli_connect($IP, $C9_USER, "", "c9");
//mysqli_connect(host,username,password,dbname); << guideline
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
SIDE NOTE: Make sure when running the mysql code that you have the data base turned on. You can turn it on by typing mysql-ctl start in the terminal.
Related
I am working on php at the moment and I am having problem with connecting to a server data base I am using MAMP
this is my code
<?php
$con = mysqli_connect('localhost','root','root','something');
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
else
{
echo "string";
}
?>
I really hope you help me in my learning process
this is the error massage I got
Failed to connect to MySQL: Unknown database 'something'
Are you sure your password is "root"? If you're using WAMP (as suggested by localhost), the default password for mysql is "" (blank).
I think you have changed the localhost name as demo..and by default localhost username and password is root and ``(no-passsword)..So just try it..
$con = mysqli_connect('demo','root','','something');
I am reviewing my old work and have started to change the mysql syntax to mysqli however I am having issues trying to connect to my database. It throws the following error: No database selected.
This is my code:
$con = mysqli_connect("localhost","root","password","db1");
// Check connection
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
Your assistance would be greatly appreciated.
Your code is correct, probably you are giving a database name that is wrong, check if that database actually exists.
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.
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));
}
?>
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.