Can't get my PHP document to connect to my xampp server - php

I am very new to php, and am sorry if this question turns out to be too vague, but if there is any other information that you need from me let me know.
Anyway, basically what my problem is I have some simple php code that should call die() if it can't connect to the xampp server I have set up, however, even if I put in invalid info for the server or user it prints Successful. I really don't know where to go with this, so if anyone has any suggestions that would be great.
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
if ( !mysql_connect($dbhost, $dbuser, $dbpass) )
{
die(mysql_error());
}
else
echo 'Succesful';
?>
Ok for some reason it was just not working with the root user, so I created a new user who required a password and apparently that connected like it was supposed to. Thanks everyone for the answers.

Your code is correct pistolpete333. For your root user issue, that your not able to connect with root user you can check below file for your phpmyadmin configuration
C:\wamp\apps\phpmyadmin3.5.1\config.inc.php.
In above file you can check your host, username and password of phpmyadmin.

I have found that when trying to connect php to an xampp server there are usually only a few things that could cause the issue you are having.
1. mySQL service is not running
2. no specific database is selected to try to access
3. user does not exist in phpmyadmin.
When you pick a user to connect php to mySQL with you should always make sure that a password is set, that is usually the thing people miss. You can use root if you create another instance of it in the user list and require a password, or you can add a password to the root user already in the list, but the best option is to create a new custom user that only has access to the database you want to access with your php and make sure it has a password required. (make sure any user you use can connect with localhost; root can by default and I think most newly created users can as well).
Below is the php I use for websites I design that need php to access a DB.
<?php
// This configures the variables for database access
$dbhost = 'localhost';
$dbuser = '????';
$dbpass = '????';
$dbname = '????';
// This will connect to the server and then the correct database
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);
?>

This is working because you have specified valid username, host and password to mysql. If you specify wrong password, you get mysql error. This code works and connects to the mysql server though you have not selected any database.
Try this code instead
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$link = mysql_connect($dbhost, $dbuser, $dbpass);
if (!$link) {
die('Not connected : ' . mysql_error());
}
// make foo the current db
$db_selected = mysql_select_db('foo', $link); //foo is your database name
if (!$db_selected) {
die ('Can\'t use foo : ' . mysql_error());
}
?>

Related

making connection code dynamic

the connection to database via PHP is as:
<?php
$DbHost = 'localhost';
$DbName = 'root';
$DbUser = 'root';
$DbPwd = '';
$dbConn = mysql_connect ($DbHost, $DbUser, $DbPwd)
or die ('MySQL connect failed. ' .mysql_error());
mysql_select_db($DbName,$dbConn) or die('Cannot select database. ' . mysql_error());
?>
Here I've hardcoded my hostname, username and password. Everytime I take my project to another machine, I've to change it.
How can I make this connection code dynamic?
You can put machine specific information in a settings.php file and use
require_once "settings.php";
to get hold of the machine settings. Do this on all your machines and put the file in a specific place so your project(s) is/are able to find it.
When you move your project to another machine, it will use the corresponding settings.php file. (Assuming you are not copying this file as well ;) )

MySQL Access in Php to a database created in C-Panel

Basically, i'm having trouble connecting to a mysql database using a php web page.
I created the database in C-panel using the wizard
i'm connecting like this
$db_host = "localhost"; //your host Database address
$db_username = "xxxx"; //your account username
$db_pass = "xxxxx"; //your account password
$db_name = "xxxxx"; //your database name
#mysql_connect("$db_host","$db_username","$db_pass") or die ("could not connect to mysql");
#mysql_select_db("$db_name") or die ("no database");
But all my page seems to do is trigger the " could not connect to mysql"
my page does have wordpress installed at the minute but I'm planning to get rid of it as I'm creating my site myself. I'm just baffled why it can't connect, because in Phpmyadmin ( a feature on C-panel) it says the database is in localhost.
In C-panel you need to add database user and assign certain roles to it before you can connect to the database.

MySQL table doesn't exist when it does exist

I have this query:
mysql_select_db('scanner');
$query = "SELECT * FROM scanner.proxy ORDER BY RAND() LIMIT 20";
$result = mysql_query($query) or die(mysql_error());
it tells me:'scanner.proxy ' doesnt exist.. even though I do have it the table with the database. It is weird, cause my other queries work..but this one doesnt.
UPADTE:
Event when I put this:
$user='root';
$user='root'
$password='<removed_password>';
$dbname = 'scanner';
$host = 'localhost';
$link = mysql_connect($host, $user, $password) or die (mysql_error());
mysql_select_db($dbname, $link) or die (mysql_error());
it gives me this...
Unknown database 'scanner'
But I can see the scanner database in the phpmyadmin
Even when I type in phpmyadmin the sql statement
SHOW TABLES FROM 'scanner'
it says it cant find scanner
We solved this problem which occurred when connecting to multiple remote MySQL databases within the same script by adding a re-connect method before every mysql_query statement. The re-connect method invoked both mysql_connect and mysql_select_db functions. We had previously tried setting the new link parameter to true in the connect statement(s) but We still got database errors and all kinds of funny behavior.
make sure the user you use to connect has enough rights to query that table. Perhaps that table was created by another user.
EDIT: Perhaps you need to grant access to scanner from a remote location. Running this sholud help on that.
GRANT ALL ON scanner.* TO your_user#'IP_ofServer_where_PHP_runs' IDENTIFIED BY 'PASSWORD';
You have set the $dbname = 'aws_backlinks', but you are trying to connect to a different database within your query. Change your connection string to include this db instead or just connect to the server and set your database at the time of the query.
$db_host = 'localhost:Port';
Specify port for localhost.
I was facing the same problem and specifying the port solved the problem.
You're missing the argument that specifies the connection (result of mysql_connect()) in your mysql_select_db() call:
$db_host = 'localhost';
$db_username = 'root';
$db_password = 'whatever';
$db_name = 'scanner';
$link = mysql_connect($db_host, $db_username, $db_password);
mysql_select_db($db_name, $link);

unable to connect to mySQL Db

I am a newbie to PHP and to the net world, and trying to connect to a mySQL Db using this PHP code:
<?php
echo "Hello World \n";
$mysql_host = "MySQL Host"; // this is what specified to use in mySQL management page at my host
$mysql_database = "mysql_database";
$mysql_user = "mysql_user";
$mysql_password = "mysql_password";
echo $mysql_host;
$conn = mysql_connect($mysql_host, $mysql_user, $mysql_password);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db($mysql_database);
if (!$db_selected) {
die ('Can\'t use '.$mysql_database.' : ' . mysql_error());
}
?>
please, what can be wrong with this code? I couldn't find any syntax error and still - I am not able to connect to the Db
can it be and the problem is related to the host only?
The following variables should contain values that are respectively:
$mysql_host - IP or host name of the server with your database,
$mysql_database - name of the database,
$mysql_user - name of the database user,
$mysql_password - password for specific user to the specific database,
Make sure all of them are correct and then the problem should be resolved. If it is not, let us know.
If you are actually typing MySQL Host in your code, then you're not providing a valid hostname. You should try with localhost instead.
Otherwise, you current host must have provided you with the needed information.
You should also tell us what error your script gives you, when it dies. That enables us to give a qualified answer instead of guessing.
as #Michael says,
$mysql_host = "MySQL Host";
is definitely wrong, that field must have a either a valid host/socketname or localhost..
These guys are spot on. Also, the password is usually blank on a localhost program.
But, this is just something I noticed:
Your $conn variable is missing an 'n' in your 'if' statement.
You are storing connection in $conn but in if condition you are checking with ($con).
(There might be other errors. But I can see this error)

Mysql database connection error

whenever I am trying to connect phpMyadmin through php script it is showing some errors like this Warning: mysql_connect(): Access denied for user 'www-data'#'localhost' (using password: NO) I am using ubuntu 11.04.So can you tell me how to solve this?
This error simply means that your connection information is incorrect. You are trying to connect to the MySQL database using the username "www-data#localhost" with no password. Check your MySQL permissions to see what you need to do. Either this login needs a password or this login is not specified as being permitted to access the data.
To check what permissions you have for that user, run this MySQL script:
SELECT * FROM user WHERE user='www-data';
To add rights to that user (if they are missing), run this script:
GRANT SELECT ON database.* TO 'www-data'#'localhost';
This works for me.
$dbhost = 'localhost';
$dbuser = 'www-data';
$dbpass = '<PUT PASSWORD FOR HERE>';
$dbname = '<YOUR DATABASE NAME>';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Could not connect: ' . mysql_error());
mysql_select_db($dbname);

Categories