I'm using a PHP file stored on my host to connect to a database stored on the same host, this is working fine.
I am using the below to connect to the database (example connection details)
<?php
$db = new PDO('mysql:host=localhost;dbname=myDB', 'myusername', 'mypassword');
My question is; seeing as I have specified the password (and other details) to connect to my server in my PHP file, can't someone with the direct link to my PHP file just download it and open it in a text editor to see those details?
If so, should I be passing the connection details to the php file like this:
<?php
$server = $_POST['server'];
$database = $_POST['database'];
$username = $_POST['username'];
$password = $_POST['password'];
$db = new PDO('mysql:host=$server;dbname=$database', $username, $password);
Expanding a bit on my comment, ideally you want to have this in separate files, one used for global configuration you can then import to your other modules like the example below.
Config.php file:
<?php
$HOST = 'hostname';
$DB = 'dbname';
$USER = 'username';
$PWD = 'password';
... other variables and global config ...
?>
DB Connection File:
<?php
include 'config.php';
$db = new PDO("mysql:host=$HOST;dbname=$DB", $USER, $PWD);
?>
Notice how the string inside the PDO connection is double quoted, because if single quoted, string interpolation won't work.
Your variable $server and $database are not interpreted correctly as you are using single quote '. You need to use double quote " to correctly pass variable values. (Refer for more details What is the difference between single-quoted and double-quoted strings in PHP?) Change your code as below.
<?php
$server = $_POST['server'];
$database = $_POST['database'];
$username = $_POST['username'];
$password = $_POST['password'];
// Replaced ' with "
$db = new PDO("mysql:host=$server;dbname=$database", $username, $password);
Related
I want to improve my non-existing PHP knowledge. To do so I created a MySQL DB and a connection.php file with an SQL query (please ignore SQL injection comments for now, I am aware of it). I trying to figure out, how I can split the connection from the actual query.
<?php
header("Access-Control-Allow-Origin: *");
$username = "root";
$password = "root";
$host = "localhost";
$database="test";
$connection = mysqli_connect($host, $username, $password, $database);
$myNodesQuery = "
SELECT * FROM nodes";
$query = mysqli_query($connection, $myNodesQuery);
if ( ! $query ) {
echo mysqli_error();
die;
}
$data = array();
for ($x = 0; $x < mysqli_num_rows($query); $x++) {
$data[] = mysqli_fetch_assoc($query);
}
//echo json_encode($data, JSON_FORCE_OBJECT);
echo json_encode($data);
mysqli_close($connection);
My thoughts were to create another PHP file and add $connection = mysqli_connect (require('connection.php')) to receive the connection string. Unfortunately, I receive a path error.
Keeping your code as is then:
File connection.php:
<?php
$username = "root";
$password = "root";
$host = "localhost";
$database="test";
$connection = mysqli_connect($host, $username, $password, $database);
The main file
<?php
header("Access-Control-Allow-Origin: *");
require 'connection.php';
$myNodesQuery = "
SELECT * FROM nodes";
// whatever follows
...
Please note that - unless you use a framework - it would be much better if you build your reusable connection class or connection-returning function. And BTW consider using the far superior PDO.
Is it possible to keep MySQL connection open from the beginning of a php file and close it at the end of that particular script?
If yes please help me because I'm having problem with this:
Prevent clash in MYSQL database
Thank you all.
This is how I open and close connection with my database:
$host = "localhost";
$root = "myusername";
$pass = "mypassword";
$DBname = "mydatabase";
$link = mysqli_connect($host, $root, $pass, $DBname);
function close_connection($link) {
$link ->close();
}
I call the function close_connection() whenever I want to stop the connection with my database. Hope this helps!
-Ed
I am stuck after writing all the code, the Website just doesn't connect with mysql at all!
I think this code will help
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password,$dbname);
?>
write this code in html file and save it as .php
I hope it works
Create a database in phpmyadmin and then write down below code in a file and save it with .php extension
<?php
$db_username = "db_username"; // Your database login username
$db_password = "db_password"; // Your database login password
$db_name = "db_name"; // The name of the database you wish to use
$db_host = "db_host"; // The address of the database. Often this is localhost, but may be for example db.yoursite.com
$conn = new mysqli($db_host, $db_username, $db_password, $db_name);
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
set those 4 variable as per your db details. mysqli_connect_error() will give you error if the connection will not establish
I am very new to php. I am trying to make a sign up page with php. So simple but I can't. I've searched across google how to connect it. They said below.
<?PHP
$user_name = "root";
$password = "";
$database = "user";
$server = "127.0.0.1";
mysql_connect($server, $user_name, $password);
print "Connection to the Server opened";
?>
I've successfully created my user database with full set of data in phpmyadmin. And running Apache and Mysql in control panel. I set the password in phpmyadmin and I changed it in $password="mypassword";. But there is no print on my web page. I think the above code is correct and I am having problems before this state. Such as the location of my database, I don't where to put it or just created on myadmin is fine. Thank you for reading my problem and kindly advice me for the beginner course.
<?php
$user_name = "root";
$password = "";
$database = "user";
$server = "127.0.0.1";
// Create connection
$con=mysqli_connect($server, $user_name, $password, $database );
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
Try mysqli instead of mysql and here is why
no no . . sorry . My html path . . on desktop
PHP, in this context, is a server side programming language. You must request the script from a server that will pass it through a PHP interpreter before sending it to the browser.
Type http://localhost/etc/etc into the browser's address bar. Don't open the PHP program directly in your browser (e.g. by double clicking the file in Windows Explorer).
If you are going to use procedural style function calls with the mysqli interface:
$con = mysqli_connect('localhost','myuser','mypassword','mydb')
or die("Error " . mysqli_error($con));
If you are going to use object oriented style with mysqli interface:
$mysqli = new mysqli('localhost', 'myuser', 'mypassword', 'mydb');
Reference: http://php.net/manual/en/mysqli.construct.php
If you are going to use PDO interface
$dbh = new PDO('mysql:host=localhost;dbname=mydb', $user, $pass);
Reference: http://php.net/manual/en/pdo.connections.php
Hey I'm wanting to retrieve some data from the a database. But it seems whenever I enter my credentials into the SQL database to retrieve the data I get the following error:
Since i presume this is a similar use case as your last question
Your php file is missing the configuration of the connection:
<?php
$dbuser = "******";
$dbpass = "*******";
$db = "SSID";
$connect = OCILogon($dbuser, $dbpass, $db);
if (!$connect) {
echo "An error occurred connecting to the database";
exit;
}
So it knows which connection you are using and passing to the checkUserPass() function.
UPDATE:
For the table name you need to pass $dbtable as you can see in the function declaration
function checkUserPass($connect,$username, $password, $dbtable)
so either set a $dbtable variable before calling the function:
$dbtable="register_table";
or send it immediately as a string:
function checkUserPass($connect,$username, $password, "register_table")