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.
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 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));
I have researched my question but there just isn't much help out there for MySQLI functions. I'm also new here so, please bear with me.
I have a test database named "website" and it contains a table called "users" which has data for all of the members of my website. In a separate file I have the php code that connects to my localhost and selects the database "website." I'm using the newest functions of MySQL so instead of the connection string containing mysql_connect it contains mysqli_connect. The registration process works great and I can find no error in any of the other code that uses the mysqli functions.
My include file (connect.php)
<?php
$link = mysqli_connect("localhost", "useracc", "useracc1", "website");
?>
I set up a test script for the login section.
<?php
include('connect.php');
$user = $_POST['user'];
$query = "SELECT birthday FROM users";
$result = mysqli_query($link, $query) or die ("RESULT ERROR");
echo "$user";
echo "$result";
?>
There is a password box on the previous page but for the test script I left that out and so far, only the $user is shown on the page. I get no error even when I replace "RESULT ERROR" with mysqli_error($link). I've tried interchanging ' for " in every part of the code that uses quotes but that didn't work. I've tried rewording the $query line but that also had no effect. I'm really new to MySQL and php so if you guys could tell me where I'm going wrong I'd really appreciate it.
Thanks for reading my question. Have a great day.
Add the following lines to the connect.php after you connect line
if (mysqli_connect_errno()) {
printf("Can't connect Errorcode: %s\n", mysqli_connect_error());
exit;
}
this will ensure the connection is made without any errors and return them if there are ...
I am looking for a way to test just the connection portion of a php / mysqli connection. I am migrating from a LAMP server build on Vista to the same on Ubuntu and am having fits getting mysqli to work. I know that all of the proper modules are installed, and PhpMyAdmin works flawlessly. I have migrated a site over and none of the mysqli connections are working. The error that I am getting is the "call to member function xxx() on non-object" that usually pops up when either the query itself is bad or the query is prepared from a bad connection. I know that the query itself is good because it works fine on the other server with the exact same database structure and data. That leaves me with the connection. I tried to write a very simple test connection and put it in a loop such as ..
if(***connection here ***) {
echo "connected";
}
else {
echo "not connected";
}
It echoes "connected", which is great. But just to check I changed the password in the connection so that I knew it would not be able to connect and it still echoed "connected". So, the if / else test is clearly not the way to go....
mysqli_connect() always returns a MySQLi object. To check for connection errors, use:
$mysqli_connection = new MySQLi('localhost', 'user', 'pass', 'db');
if ($mysqli_connection->connect_error) {
echo "Not connected, error: " . $mysqli_connection->connect_error;
}
else {
echo "Connected.";
}
For test php connection in you terminal execute:
$ php -r 'var_dump(mysqli_connect("localhost:/tmp/mysql.sock", "MYSQL_USER", "MYSQL_PASS",
"DBNAME));'
You need more error handling on the various database calls, then. Quick/dirty method is to simply do
$whatever = mysqli_somefunction(...) or die("MySQL error: ". mysqli_error());
All of the functions return boolean FALSE if an error occured, or an appropriate mysqli object with the results. Without the error checking, you'd be doing:
$result = $mysqli->query("blah blah will cause a syntax error");
$data = $result->fetchRow(); // $result is "FALSE", not a mysqli_object, hence the "call to member on non-object"