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');
Related
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'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));
}
?>
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.
I have an online database and what I need to do is to fetch data from it and save to my local database(localhost). I tried adding remote mysql database cause it said it is needed(I added the IP address of my local computer), I dont know if its right. The problem is it gives me an error
"arning: mysql_connect() [function.mysql-connect]: Can't connect to MySQL server on '10.24.168.85' (4) in /home/umalert/public_html/server/_includes/connection.php on line 18
Database connection failed:".
For now I have this connection script:
// create database connection
$connection_local = mysql_connect('x.x.x.x:3306', 'xxx', 'yyy');
if(!$connection_local) {
die ("Database connection failed: " . mysql_error());
}
// selece database to use
$db_select_local = mysql_select_db('umalert_db', $connection_local);
if(!$db_select_local) {
die("Database connection failed: ". mysql_error());
}
Hope you can help me with this. Thank you.
you should export your data from one database and import to another one.
I'm trying to create a very simple create account page. This is the part of the MySQL Database connection:
<?php
function dbconnect()
{
$connection = mysqli_connect("localhost","maistral_quizonline","maistral_kalkas","abcdefghi");
if (mysqli_connect_errno($connection))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
return 0;
}
return $connection;
}?>
When I try to run dbconnect this is what I get as output:
"Failed to connect to MySQL: Access denied for user 'maistral_quizonl'#'localhost' (using password: YES)Access denied for user 'maistral'#'localhost' (using password: NO)"
Now, I'm new to php and maybe i do something really silly, can anyone explain to me this output? Why it seems to try for connection twice and why the connection information is corrupted? What is this: maistral_quizonl? maistral? Why is it cutting the words? And why the first time says that I'm using password and the second time says no? I hope my mistake is not so stupid.
And one other thing. I saw in another post someone suggesting to check if the mysqli_connect function exists so I change this:
echo "Failed to connect to MySQL: " . mysqli_connect_error();
to this:
echo "Failed to connect to MySQL: " . mysqli_connect_error() . var_dump(function_exists('mysqli_connect'));
and the output was this:
"bool(true) Failed to connect to MySQL: Access denied for user 'maistral_quizonl'#'localhost' (using password: YES)Access denied for user 'maistral'#'localhost' (using password: NO)"
lol?
I know that it's pointless to check if mysqli_connect exists after you use it but am I mistaken or isn't the "bool(true)" suppose to print at the and of the line and not the beggining?
What is going on?
Thank you
PS: I have double check that the database exists as well as the user and the priviliges and the password
mysqli_connect accepts parameters in this order 'host', 'username', 'password', and 'dbname', is this the order in which you have provided your details? It does not seem so.
http://us2.php.net/manual/en/mysqli.construct.php