unable to connect to mySQL Db - php

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)

Related

if statement doesnt need action

I'm making a user login system with php. Obviously to do that I need to have databases and I do using PHP MyAdmin. The connection in my php to mysql didn't work at first so I looked it up online and found this code:
<?php
$mysql_host = 'localhost';
$mysql_user = 'root';
$mysql_pass = '';
$mysql_db = 'a_database';
if(!mysql_connect($mysql_host, $mysql_user, $mysql_pass)||!mysql_select_db($mysql_db)){
die(mysql_error());
}
?>
And this would be enough to connect to SQL. So here is my question: how can it be that you don't have to actually connect to the database in order for it to work? Because to my knowledge of php all that is doing is checking if that works, it isn't actually connecting. So is this code correct? Or do you need to add
mysql_connect($mysql_host, $mysql_user, $mysql_pass)||!mysql_select_db($mysql_db))
to this code to make it work? Thanks in advance.
(I'm using XAMPP btw)
mysql_connect tries to connect to to database. If it's false (connection failed) - your script dies. Otherwise - connection established succesfully and we need to check second condition
mysql_select_db selects db. It is false - your script dies too, if not - db selected successfully.

cannot connect php to postgres database using pg_connect

I just try to connect my php code to postgres database but unfortunately it is not work, here is my code :
<?php
ini_set ("display_errors", "1");
error_reporting(E_ALL);
$host = "host=127.0.0.1";
$port = "port=5432";
$dbname = "dbname=public";
$credentials = "user=postgres password=postgres";
$db = pg_connect( "$host $port $dbname $credentials" ) or die('Could not connect');;
if(!$db){
echo "Error : Unable to open database\n";
} else {
echo "Opened database successfully\n";
}
browser result :
Warning: pg_connect(): Unable to connect to PostgreSQL server: FATAL: database "public" does not exist in /var/www/test/pgtest.php on line 11
anybody know what is wrong here ?
Are you sure the name of database is 'public'? As fas as I remember 'public' is a name of default schema inside the PostgreSQL database.
Please make sure the database 'public' really exists.
From the command line, become the postgres user, start psql, and issue the \list command to list your databases. Be sure the database you're trying to open is in the list.
Be sure your pg_hba.conf file allows the database to be opened.
Remove everything except $dbname from the pg_connect call and allow the API to use the defaults. Add parameters back as indicated by the error message(s).
sorry but finally I found that my question was wrong, public and testdb are Schemes inside postgres database,

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

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());
}
?>

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);

Trouble with PHP mysql_select

Ok, I've just started learning php, and for some reason the mysql_connect() and mysql_select_db() do not appear to connect to the database.
I also need a good example of a mysql table, as I am not sure what to put in my table. How do I manage to connect my site to the database?!
Here is the code I am using in my connect.php page.
<?php
//not working
$connect = mysql_connect('localhost',
'myuser', 'password', 'mypass')
or die("This function should work.");
//still a lingering error
$db_select = mysql_select_db("mydbname", $connect);
?>
If you just start with PHP then a good idea would be to get Apache and MySQL on your own computer. There are plenty of easy installable packages like WAMP (http://www.wampserver.com/en/). There are examples and HOWTOs there. The good in this package, is that there is a PHPMyAdmin that comes packed, and you can use it to make a sample table, or just to play around.
Next, for the concrete code, there is a nice example at the PHP documentation site (http://php.net/manual/en/function.mysql-connect.php).
<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Not connected : ' . mysql_error());
}
// make foo the current db
$db_selected = mysql_select_db('foo', $link);
if (!$db_selected) {
die ('Can\'t use foo : ' . mysql_error());
}
?>
When using the local server you can use the ROOT user in MySQL. For a test table you can make one called, for example friends, with two VARCHAR fields for name and phone number. And you can play around to list them edit them and so on.
Good luck.

Categories