I just started programming in php and would like to ask a question about the database selection code for mysql in the php coding.
I used phpmyadmin to create a database "admin" when in phpmyadmin I click on privileges and see the name as"admin#127.0.0.1". I created a connection to the database using this code in PHP:
<?php $connection = mysqli_connect("127.0.0.1", "admin", "admin123");
if (!$connection)
die("Database connection failed:" . mysqli_error());
and now i want to select the tables in the database I use this command:
$selected = mysqli_select_db("admin", $connection);
if (!$selected)
{
die('Database selection failed:' .mysqli_error());
}
?>
I know it connects because when only using the connection command when opening my broswer I can see the header i put in html, but I get an error with the selection command and cannot continue.
Warning: mysqli_select_db() expects parameter 1 to be mysqli, string given in C:\Program Files\EasyPHP-DevServer-13.1VC11\data\localweb\projects\databaZE.php on line 6##
Warning: mysqli_error() expects exactly 1 parameter, 0 given in C:\Program Files\EasyPHP-DevServer-13.1VC11\data\localweb\projects\databaZE.php on line 8
Database selection failed:
Firstly is there a problem with the way I wrote my database name thats why it cannot connect and giving me error msg's?I used 127.0.0.1 as database, admin#127.0.0.1 but still same msg. I tried both mysql and mysqli but it doesnt seem to work also.
Edit: first time user sorry am a little confused with inputting code.
You have them the wrong way around,
$selected = mysqli_select_db("admin", $connection);
should be
$selected = mysqli_select_db($connection, "admin");
And
die('Database selection failed:' .mysqli_error());
should be
die('Database selection failed:' .mysqli_error($connection));
Related
I have a nice free for searching a database. It was written with PHP and MySql...however for whatever reason, I occasionally need to add an "i" at the end of MySql occasionally to get things working. I can connect and login, but not select the database, since it passes through until I get the
"request "Unable to select database."
Here's the meat from the config.php file that probably has the issue :
$SETTINGS["hostname"]='localhost';
$SETTINGS["mysql_user"]='root';
$SETTINGS["mysql_pass"]='root';
$SETTINGS["mysql_database"]='myDB';
$SETTINGS["data_table"]='data'; // this is the default database name that we used
/* Connect to MySQL */
if (!isset($install) or $install != '1') {
$connection = mysqli_connect($SETTINGS["hostname"], $SETTINGS["mysql_user"], $SETTINGS["mysql_pass"]) or die ('Unable to connect to MySQL server.<br ><br >Please make sure your MySQL login details are correct.');
$db = mysqli_select_db($SETTINGS["mysql_database"], $connection) or die ('request "Unable to select database."');
};
?>
The problem you are facing is the line in which you select the database;
$db = mysqli_select_db($SETTINGS["mysql_database"], $connection) or die ('request "Unable to select database."');
As defined by the documentation of mysqli_select_db() the connection $connection ($link in the docs) should be the first argument:
$db = mysqli_select_db($connection, $SETTINGS["mysql_database"]);
The reason why you need to occasionally add an i to every mysql_* function is because all mysql_* functions are officially deprecated, no longer maintained and removed in PHP 7.0.0. You should update your code with PDO or MySQLi to ensure the functionality of your project in the future.
With mysqli, you can select your database directly using the connection method :
$connection = mysqli_connect($SETTINGS["hostname"], $SETTINGS["mysql_user"], $SETTINGS["mysql_pass"], $SETTINGS["mysql_database"]);
If you want to use mysqli_select_db, you need to reverse your arguments. First the connection, then the database :
mysqli_select_db($connection, $SETTINGS["mysql_database"]);
I got the script to create a simple forum from someone. However I have a problem when going to install it.
Next image database and config php script
For those who understand please give the information.
Try assigning the connection and database select as variables and then put the variable for connection into the database select.
$server="server name";
$username="username for database";
$password="password for database";
$connect_mysql=mysql_connect($server,$username,$password) or die ("Connection Failed!");
$mysql_db=mysql_select_db("database name",$connect_mysql) or die ("Could not Connect to Database");
and so on. If that doesn't work, try what frz3993 says with the privileges.
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 cant seem ton get a connection between this php script and my MySQL database. As far as I am aware this code is correct and should execute the query. The problem is with the connection on line 1. I'm just wondering does anyone know of any reason why this is not working, or am I making a really foolish mistake.
<?php
$con = mysqli_connect("localhost","username","password","database_name");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$query = "INSERT INTO lat_long (uname, lat, lon) VALUES ('boo', 'boo', 'boo')";
mysqli_query($con, $query);
mysqli_close($con);
?>
EDITED: Here is the error
Fatal error: Call to undefined function mysqli_connect() in /var/www/project
/insertar.php on line 7
If you are getting a connection error, chances are your problem will be found on this line:
$con = mysqli_connect("localhost","username","password","database_name");
Are you sure you have got the correct host address, username, password and name of your database schema?
You may also want to check that you have the mysqli php extension installed on wherever this database is being hosted.
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.