Hello I've been making a site on my local server and I finished it so I'm moving everything over to my live server. I've made a database in phpmyadmin on it and I would like to connect to it. I feel like I have the wrong inputs though because it gives me this error.
This is my code for my database connection.
<?php
return $conn = mysqli_connect('localhost', 'gener105_nate', '(Password)', 'user_data');
if (!$conn) {
die("Connection Failed: " . mysqli_connect_error);
}
I didn't actually enter password I just think I shouldn't show it anyway I think I just have something mixed up since I'm new at this.
Oh and the database is located within a database grouping should i input the path to it?
You need to tell it to connect to gener105_user_data instead of just user_data
return $conn = mysqli_connect('localhost', 'gener105_nate', '(Password)', 'gener105_user_data');
Related
I instal wamp server locally on my PC.I create a user registration form using php and mySQL database for pratice.I was able to connect to my mySQl data server and if i input data on the form and submit is work successfully.I normal experience at time when i access the user registration form it lost connection to my SQLdata server even though my wamp server is on.I will troubleshoot by changing some of the parameter by using mysqli_connect() to check if i will be prompted unsuccessful connection still nothing will show.When i check some other time it will work successfully.Please what may be causing this issue?
Run wampserver as administrator by right clicking wampserver.exe file. It may help.
Edit:
With the type of english you are using to produce the problem and giving no code is making it difficult to predict what the problem actually is. But I think there is something wrong with your mysqli connection file.
Add this code bit to the top of your registration.php file like this:
<?php
$connection = mysqli_connect('localhost','root','');
if(!$connection) {
die("Failed to connect" . mysqli_error($connection));
}
else {
echo "Connection Established";
}
$select_db = mysqli_select_db($connection, 'db2');
if(!$select_db) {
die("Database selection failed" . mysqli_error($connection));
}
else {
echo "Database selected";
}
?>
<?php
////rest of ur registration code goes here
Beginner question here. I am learning php and mysql. I have a website that I created using a local server. The database was linked to it fine on MAMP. I am now trying to upload it to a shared server using iPage. It will not connect.
Here is my code
<?php
$connect = mysqli_connect('host','user','password','db_name');
if (!$connect) {echo 'Could not connect';} ?>
I know the user, the password and the database name are correct. My only doubt is regarding the host. My website is in a subdomain. Should my code be:
<?php
$connect = mysqli_connect('subdomainName.domainName','user','password','db_name');
if (!$connect) {echo 'Could not connect';} ?>
or
<?php
$connect = mysqli_connect('domainName','user','password','db_name');
if (!$connect) {echo 'Could not connect';} ?>
or even
<?php
$connect = mysqli_connect('localhost','user','password','db_name');
if (!$connect) {echo 'Could not connect';} ?>
Do I need to do anything in my hosting page to link the database in any way. I have tried all options and nothing works. Thanks
Before implementing code you have to make new database in your website server,for this you have to log in to cpanel and make db in it, use that details and try the third one and use mysqli_connect_error() for getting the error details. like below.
Note: Better to use double quotes here because of string.
// Create connection
$conn = mysqli_connect("localhost", "username", "password");
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
if you are getting Connection failed: no such file or directory. then refer another question PHP - MySQL connection not working: 2002 No such file or directory
Better to contact your hosting provider if you are in shared hosting.
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 just finished writing a website which is working very well on my local server (xampp).
The following is my connection database definitions for the local server:
<?php
$host="localhost";
$username="root";
$password="1234";
$db_name="partnership";
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
?>
I’ve loaded my files successfully and put them on the remote server (www.mydomain.com) and I’ve created the relevant database ‘partnership’ (successfully imported all Tables from phpMySQL local server to the remote server).
I can view the website (www.mydomain.com) but with no connection to the database (MySQL).
The error message is of course "cannot connect".
I’ve changed $host="localhost" to $host=”www.mydomain.com” but still getting the same error message.
Any assistance on the above issue would be greatly appreciated.
Shouldn't you still use localhost as $host if you're trying to connect via the application? The script is on the same server as the DB.
If you are using cPanel, when you create the MySQL table, make sure you assign your user to it (trivial, I know, but Ive forgotten about it a few times myself.)
If your script is running from your remote server, just stick with localhost. Will work fine.
Last, my only suggestion until a better answer is said, try using
$host = 'localhost';
$user = 'username'; // Im pretty sure your username isn't root.
$pass = '1234';
$db = 'partnership';
/* Alot of hosts like to append your cPanel login to your db and username fields.
Check to see what your table is. It might actually be 'youruser_partnership' */
$mysqli = new mysqli($host, $user , $pass, $db);
if (mysqli_connect_error()) {
die('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error());
}
If your host supports mysqli (most do). If not, when you do your die statement, still use die(mysql_error()); to get the exact error. It will more than likely be your username/password you created and assigned to your database
Ah, this is always the most annoying part of the process. Try this and see if it works:
$host="127.0.0.1"; // "localhost" should work, but don't count on it
$username="user"; // username you use to log into phpMyAdmin
$password="password"; // password you use to log into phpMyAdmin
$db_name="dbname"; // database you want to connect to
You'll need to have created this user/password pair and given them the appropriate permissions to access the database. Also note that the mysql PHP functions are deprecated and should not be used. PDO or mysqli are the preferred ones going forward.
I face with a strange problem yesterday. I have server running Debian with installed PHP 4.4.4-8 and mysql 5.5.9. That server serves a several websites.
For some reason randomly I get that error "Access denied for user 'www-data'#'localhost' (using password: NO)" when I try to load the webpage.
If I hit refresh the page loads normally, but afer several clicks that message appears again. Username which that page use to connect to mysql server is not www-data.
Does anyone has faced similar problem ?
www-data is the Debian user that runs apache and php. If you attempt a query when you don't have a valid connection, php/mysql will attempt to create a connection using <unix-user>#localhost with no password. This is where www-data#localhost (using password:NO) is coming from.
The most likely reason that this has started happening now (though it has been running fine for 2-years prior) is that your db load has increased to the point where some connections are unable to succeed (probably due to max_connections, or max_user_connections; though this can also result from other limits like memory, threads, etc). When this happens, your call to mysql_connect will emit an error message, and return FALSE. If you fail to detect this failure, then your next mysql call (probably mysql_query, or mysql_select_db) will attempt the connection to www-data#localhost -- thus causing the problem you're seeing.
I suggest enabling error reporting, and error display (as suggested by #DarkMantis) :
ini_set('error_reporting', E_ALL|E_STRICT);
ini_set('display_errors', 1);
Also, be sure that your call to mysql_connect is not preceded by a # sign; and make sure to check the return value. It should look something like this:
$cxn = mysql_connect('localhost','yourusername','yourpassword');
if( $cxn === FALSE ) { die('mysql connection error: '.mysql_error()); }
It sounds like the query that is causing the error happens when something specific is called. This could mean that when the query is called, you aren't connected to the database with the correct username/password.
Try to ensure that you are definatly connected, use or die(mysql_error()); at the end of all your query variables to debug them.
Also, use the following two lines at the top of your php file:
ini_set('error_reporting', E_ALL);
ini_set('display_errors', 1);
That will show you any little php errors that may occur within your class/file which you may not have picked up before.
If your still having a problem after this, please post your PHP code and I will take a look at it directly.
Thanks!
i faced the same problem.
The problem was in my config.php!
I simply changed the $dbserver from
"127.0.0.1" -> "localhost".
Now the connection works again!
For absent-minded people, this error may happen when mysql_query() is called after mysqli_connect(), when it should be mysqli_query().
Use password 'NO'
(MySQLi Object-Oriented)
<?php
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
Deactivating safe mode fixed it for me:
Notice: mysql_connect(): SQL safe mode in effect - ignoring host/user/password information in /var/www/html/test.php on line 4
For solve this problem. I had to change the connection script Using
(MySQLi Object-Oriented)
<?php
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>