if statement doesnt need action - php

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.

Related

How to fix fatal error in PHP?

I found a system that can generate a report I would like to study this system but when I try to generate the system the login was OK but when I put the password and username this error comes out:
Fatal error: Call to undefined function mysql_select_db() in
C:\xampp\htdocs\inventory\inventory\db.php on line 8
Can anyone else tell me what is the problem in this system?
image
I'm currently using XAMPP V3.2.2
If you are using PHP 7 within your XAMPP then it wont work because that code is too old. mysql_select_db was removed in PHP 7.
http://php.net/manual/en/function.mysql-select-db.php
You will need to install PHP 5 to use that software.
Edit: Pratik Solanki is correct also. The database is being connected using mysqli so depending on the other code in the system you can either change the database connect statement to mysql_connect and use PHP 5 (old mysql connector not recommended) or change all the database statements to use mysqli instead in which case no PHP changes are needed (recommended)
You forgot to include the database while connecting using mysqli_connect function.
In your code, you declared a $mysql_database but you didn't use that.
Code:
<php
$mysql_hostname = "localhost";
$mysql_user = "root";
$mysql_password = "";
$mysql_database = "liveedit";
$bd = mysqli_connect ($mysql_hostname, $mysql_user, $mysql_password, $mysql_database) or die ("Opps something went wrong ");
?>
You connected to database via mysqli module of php and you are trying to select your database via mysql module of php.
Correct way to use it would be like this :
mysqli_connect(dbhostname,dbusername,dbpassword,dbname)
mysqli can be used with the mysql native drivers and mysql can be used via the default libraries provided.
Both are different and the only small mistake you made was making connection with mysqli and selecting db with mysql.
Solution to this is making the whole connection along with selection of db via mysqli function

Cloud9 and MySQL, Connection Refused

I'm sure this will be pretty easy, but I've been looking at this for a while, and haven't figured it out yet.
I've got a project in Cloud9 that I am trying to set up to access a MySQL database.
I've started the MySQL engine (mysql-ctl start), so I don't think that's the issue.
Here are my variables:
$db_hostname = "[username]-[projectname]-[assigned id]"; //this was retrieved by using 'SELECT ##hostname;' within MySQL
$db_database = "c9"; //default cloud9 database
$db_username = "[username]";
$db_password = ""; //the default is no password
$db_port = 3306; //the default cloud9 MySQL port
And here is my mysqli connect statement:
$conn = mysqli_connect($db_hostname, $db_username, $db_password, $db_database, $db_port) or die("Connection failed: " . mysqli_connect_error());
And this is the error that is being sent back:
Warning: mysqli_connect(): (HY000/2002): Connection refused in
/home/ubuntu/workspace/insertWorkoutScript.php on line 11
Call Stack:
0.0009 248728 1. {main}() /home/ubuntu/workspace/insertWorkoutScript.php:0
0.0012 249768 2.
mysqli_connect() /home/ubuntu/workspace/insertWorkoutScript.php:11
Connection failed: Connection refused
Line 11 (the line that the error identifies) is the $conn = mysqli ... statement. I've verified that the variables are being filled (I've got an echo for each variable name and it's value). I've double and triple checked the value I'm using for $db_hostname (which as I said, I retrieved from cloud9 using the SELECT ##hostname; statement). And as I said, I've made sure to Start the MySQL instance with the $ mysql-ctl start line in the terminal. Thoughts on what simple thing I'm missing here?
Thanks
Thank you #HPierce for the reminder to try getenv('IP'). I had tried that earlier but got a bad function response. My guess is that I typed it incorrectly, or was using it incorrectly. I've tried that again, and am now getting a good connection. So the correct settings are:
$db_hostname = getenv('IP'); // as opposed to using results from select ##hostname;
$db_database = "c9"; //default cloud9 database
$db_username = "[username]";
$db_password = ""; //the default is no password
$db_port = 3306; //the default cloud9 MySQL port
Everything else stayed the same. And I've also now solved my SQL insertion error as well.
Thanks again for your help.

Database connection phpmyadmin

I'm working for an e-commerce that has the db on phpmyadmin. In another website I'd like to connect to that database. I have password, username and db name, so I'm using this "connection string":
<?php
$nomehost = "localhost";
$nomeuser = "user";
$password = "pass";
// connection
$conn=mysql_connect($nomehost,$nomeuser,$password);
if (!$conn) exit ("Error connection<br>");
// db name
if (!mysql_select_db("db_name",$conn)) exit("Error db name<br>");
?>
The result is "Error db name". What can I do? Have I to set some oprion in the phpmyadmin?
First of all:
this error is caused by the fact that you are selecting the wrong database in your MySql server. Is your db called db_name???
EDIT: based on the comments you are making: is the server that hosts the php page the same as the mysql server?
Then:
phpmyadmin is just a tool to connect and handle MySql databases and is not a database server itself.
Last but most important:
you are using a deprecated library (mysql) in php to connect to a MySql server. Please consider moving to mysqli or better to PDO

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)

Categories