I created tables in phpMyAdmin - but when I try to connect php to the table it says:
can't use college unknown database 'college'
I was trying to move a zip file to the folder where the php file is (using xampp):
<?php
define('DB_NAME','college');
define('DB_USER','root');
define('DB_PASSWORD','');
define('DB_HOST','localhost');
$link=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD);
if(!$link){
die('couldnt connect:'.mysql_error());
}
$db_selected=mysql_select_db(DB_NAME,$link);
if(!$db_selected){
die('can use'.DB_NAME.':'.mysql_error());
}
echo 'connect successful';
?>
First:
Confirm that you are accessing phpMyAdmin on the correct host. In your code, it's clear that PHP is attempting to connect to MySQL on localhost. Is phpMyAdmin running on localhost? If not, then you've identified your error - ensure that you are connecting to the correct server - wherever you are running phpMyAdmin.
Second:
If you're sure that you are running phpMyAdmin on the same host as you're trying to connect to with your php, then double-check that the correct database actually exists.
Connect to phpMyAdmin, and select the tab labeled 'SQL' at the top. In the query box, enter SHOW DATABASES;, then select the 'go' button underneath the query box.
On the results page, ensure that your 'college' database exists. If it's not listed in the results, then you will need to re-create the database.
Otherwise, it's likely a permissions issue with your user (root) accessing your 'college' database.
If you provide more detailed information, we may be able to provide more assistance.
Related
I am getting this error when I try to use a php login page to connect to the database. I have both apache and MySQL running. My apache is listening on port 8080. I have created a user "michael" with password "17701788" with privileges to all databases using the phpmyadmin interface. I have tried changing the conf.ini file of phpmyadmin but it still didnt work.
This is my dbconnect.php file
<?php
$dbConn = mysqli_connect('localhost', 'michael', '17701788', 'autoservice');
if(!$dbConn) {
die("Failed to connect to database " . mysqli_connect_error());
}
?>
Check the hostname. You might wanna try 127.0.0.1 instead of localhost. Its the same thing but sometimes things get funny. if this does not work. delete the current created user and create another. this time;
1. be sure not to copy and paste the password especially as it could have white spaces.
2. Enter each credential carefully.
run and lets see.
When the coffee gets finished, stress makes us not see very tiny errors. lol
I am using cakePHP to connect to a database in XAMPP phpMyAdmin. I have added a new user, set the password and set the privilleges. I cannot for the life me create a connection to the database on my localhost. What I have done so far.... I have a live web server with a database. I can connect to this database no problem. I have a database running in MySQL workbench on localhost. I can connect to this no problem. This tells me that there is nothing wrong with my connection code and the problem must sit with phpMyAdmin in XAMPP. I have created a simple connection PHP script to try and connect to the database and it doesn't work. The same script can connect to my live web server and my localhost MySQL workbench database but not my XAMPP database. My connection script is as follows:
<?php
$con= mysqli_connect("localhost","root","","test");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// Perform a query, check for error
if (!mysqli_query($con,"INSERT INTO Persons (FirstName) VALUES ('Glenn')"))
{
echo("Error description: " . mysqli_error($con));
}
mysqli_close($con);
?>
The error message I get is;
Failed to connect to MySQL: Access denied for user 'root'#'localhost' (using password: NO)Error description:
If i go to phpMyAdmin in XAMPP and go to the test database I can clearly see that this user has full access. I have tried using the host name as 127.0.0.1, localhost and ::1. Nothing seems to work. I just dont understand why XAMPP will not let me connect to the database. I hope I'm not missing something obvious so any help will really be appreciated. here is a screen shot of the users and their privilleges for the database test
That's the second reason why one should never use 'root' unless one knows exactly what one is doing. Only a few expirenced ones really know what they're doing each and every moment...
Add a specific useraccount like 'data' on your localhost and grant it the needed priviledges on your DB.
I am actually using the username 'login' with an auto generated password that is of high complexity. This however doesn't work either. I have used the 'root' user with no password to highlight the fact that not even the admin accounts can connect.
Open config.inc.php file in the phpmyadmin directory
Find line 21: $cfg['Servers'][$i]['password'] = '';
Change it to: $cfg['Servers'][$i]['password'] = 'your_password';
Restart XAMPP
I'm creating a web application in PHP and I need to connect to a database and retrieve information from it. The database in question is being hosted on phpMyAdmin. I'm currently using the following PHP code to connect to it.
//Attempt to connect to the database
$conn = mysql_connect('localhost', 'my_username', 'my_password') or die (mysql_error());
//Tell the user if they were successful
echo "Connection successful!";
//Close the connection
mysql_close();
When I run the website, it produces the following SQL error:
"No connection could be made because the target machine actively refused it."
I'm sure that my username and password are spelled correctly and I believe that 'localhost' is the server name that I need to use. Is there a different mysql_connect command that I need to use for phpMyAdmin? If not, how can I solve this problem?
Edit:
Upon publishing the website to Microsoft Azure (where I need to host it), I've found that it produces a different error:
"An attempt was made to access a socket in a way forbidden by its access permissions."
How will I be able to fix this error? Or will fixing the original error also solve this one?
Do not use mysql_* functions. They are deprecated. For more information, see this question: Why shouldn't I use mysql_* functions in PHP?
phpMyAdmin hosts nothing; it is simply a PHP application that connects to your database just like your own app is attempting to do.
Take a look at the phpMyAdmin config file and ensure you are using the same host. Also try the same username/password. If this succeeds, it's advisable to set up a dedicated username/password for your application. This can be done within phpMyAdmin on the privileges page.
Try use IP instead of localhost.
User has permissions? Check them
Trying to figure out how to connect to a local SQL Server with an online PHP file. However I am getting connection errors.
Warning: mysql_connect() [function.mysql-connect]: [2002] No
connection could be made because the target machine actively refused
it. (trying to connect via tcp://localhost:xxxx) in
E:\web\example.com\uploads\readDB.php on line 8
In particular I am having trouble trying to specify if, and where, I do the connection to the PC and then to the database with any associated passwords.
My current connection looks like this
mysql_connect("localhost","the_sql_server_name\SQLEXPRESS","mysql");
mysql_select_db("ACME") or die(mysql_error());
$data = mysql_query("SELECT * FROM TradingAccount")
or die(mysql_error());
My server is run locally (sql server studio management 2012), and I am trying to connect to its specific DB called ACME, from the table TradingAccount.
Can someone show me how to connect to this local table successfully with PHP? I use Windows Authentication at the moment when I run the server as well, in case that matters.
I haven't been able to find any useful resources, and I have only ever used PHP to connect to online DB's as well, nothing locally before.
Your Problem is in this line:
mysql_connect("localhost","the_sql_server_name\SQLEXPRESS","mysql");
You wrote the_sql_server_name\SQLEXPRESS there. I don't understand why. The syntax of the method is:
mysql_connect("hostname", "username", "password");
For more information, visit the PHP manual entry for mysql_connect.
This sounds like a stupid question, but I'm beginning in PHP and MySQL and failing at the first step, trying to connect to the database:
<html>
<head>
</head>
<body>
<?php
$con = mysqli_connect("localhost", "root", "root", "ASOIAF.odb");
if (mysqli_connect_errno()) {
echo "Failed to connect to database";
} else {
echo "Connected to database";
}
mysqli_close($con);
?>
</body>
</html>
I don't understand the first host parameter. A yahoo help page said that the host should be "mysql", but when I used this it failed to connect as well. The database, ASOIAF, is in OpenDocument database format (odb), on LibreOffice Base. I'm using Uniserver to run PHP and MySQL; both are currently running. The web application is stored in Uniserver's www folder, and run on Google Chrome through localhost.
What should I be inserting in the host portion of the connect statement? Or have I made a more basic syntax error in the PHP code that is preventing a connection from being made?
What is my host name for MySQL?
We don't know what you have called the computer you are running MySQL on.
A yahoo help page said that the host should be "mysql"
That is specific to Yahoo Web Hosting. It doesn't sound like you are using their service, so that is inapplicable.
I'm using Uniserver to run PHP and MySQL
That suggests the computer running the webserver running PHP and the computer running the MySQL server are the same machine. That makes localhost appropriate.
localhost is the standard name given to a computer on the private network that only it connects to. For MySQL is has a special meaning and causes the connection to be made via a socket instead of TCP/IP (which is more efficient).
What should I be inserting in the host portion of the connect statement?
localhost if the above is correct.
Or have I made a more basic syntax error in the PHP code that is preventing a connection from being made?
I can't see any syntax errors. I suspect the problem may be down to your database configuration. Have you told the MySQL server about ASOIAF.odb? Is that what the database is actually called inside MySQL (as opposed to just being the file that stores the configuration for LibreOffice to connection to that database).
Your biggest mistake is not using mysqli_error to find out what problems PHP is reporting.
if (mysqli_connect_errno()) {
echo "Failed to connect to database";
echo mysqli_error();
use
mysqli_connect("localhost", "root");
Hope it will work