I'm trying to make a MySQL & PHP based control panel for my community. And I've made a settings.php file with arrays with configs. I have class files for functions but there is MySQL function, which will not connect with data I've entered on settings.php..
I've tried other config file options, but none of them works...
on settings.php I have MySQL data like this:
$config['database']['host'] = "---";
$config['database']['user'] = "---";
$config['database']['password'] = "---";
$config['database']['database'] = "---";
And on userdata.php class file, where I'm trying to use those config variables I have:
$mysql = new mysqli($config['database']['host'], $config['database']['user'], $config['database']['password'], $config['database']['database']);
obviously, on userdata.php I have also required the settings.php..
I was expecting the output to be correct, but it shows only 'wrong MySQL data' errors...
I got it working with settings.php file like:
$config = array (
'db_host' => 'xxx',
'db_user' => 'xxx',
'db_pass' => 'xxx',
'db_database' => 'xxx'
and userdata.php like:
include 'settings.php';
$host = $config['db_host'];
$user = $config['db_user'];
$pass = $config['db_pass'];
$database = $config['db_database'];
$mysql = new mysqli($host, $user, $pass, $database);
you can try this way in connection.php file
<?php
include_once("path/config.php");
function OpenCon()
{
$dbhost = $config['database']['host'];
$dbuser = $config['database']['user'];
$dbpass = $config['database']['password'];
$db = $config['database']['db'];
$conn = new mysqli($dbhost, $dbuser, $dbpass,$db) or die("Connect failed: %s\n".
$conn -> error);
return $conn;
}
Related
So i've been having an issue with NetBeans code completion. I'm currently trying to set up a database connection that i can use in my other files. Inside of my connection.php file, I have access to all of the usual database methods. My end goal is to use prepared statements for my queries.
However, when i am inside my register.php file, with connection.php required at the top, suddenly code completion doesn't work anymore. I get the generic popup. This is the file i would like to actually use those prepared statements in. I would also like to avoid having to open a new connection in every file.
So my question to you is, is it me or is it NetBeans?
connection.php
$host = "localhost";
$username = "root";
$pass = "password";
$database = "LoganWebsiteUserLogin";
try {
$conn = new PDO('mysql:host=localhost;dbname=' . $database . ';charset=utf8mb4', $username, $pass);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected Successfully!";
} catch (PDOException $e) {
echo "Connection failed! " . $e->getMessage();
}
register.php
<?php
require 'connection.php';
// Create session variables
$_SESSION['firstName'] = $_POST['firstName'];
$_SESSION['lastName'] = $_POST['lastName'];
$_SESSION['email'] = $_POST['email'];
// trying to access $conn from connection.php here, however autocomplete for
// it is not working. NetBeans doesn't know that $conn is a PDO
$conn->
It works in PhpStorm but not in Netbeans. But anyway, global variables are not recommended and lead to unwanted side effects. Maybe try a different approach.
Example:
connection.php
<?php
function db(): PDO
{
static $pdo = null;
if (!$pdo) {
$host = "localhost";
$username = "root";
$password = "password";
$database = "LoganWebsiteUserLogin";
$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_PERSISTENT => false,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci",
];
$pdo = new PDO("mysql:host=$host;dbname=$database;charset=utf8mb4", $username, $password, $options);
}
return $pdo;
}
register.php
<?php
require_once __DIR__ . '/connection.php';
// Create session variables
$_SESSION['firstName'] = $_POST['firstName'];
$_SESSION['lastName'] = $_POST['lastName'];
$_SESSION['email'] = $_POST['email'];
// Works :-)
db()->
Please tell me that where is error. I am in tention due to this,
Fatal error: Uncaught Error: Call to undefined function mysql_connect() in C:\xampp\htdocs\kalooo1\includes\db.php:2 Stack
trace: #0 C:\xampp\htdocs\kalooo1\index.php(21): include() #1 {main}
thrown in C:\xampp\htdocs\kalooo1\includes\db.php on line 2
db.php
<?php
mysql_connect("localhost","root","");``
mysql_select_db("kalooo");
?>
index.php(21)
include("includes/db.php");
You are getting that error simple because you are using a newer version of php that does not support mysql_* functions, those functions have been depreciated and completely removed from the latest version of php.
You should use mysqli prepared functions or pdo prepared statements.
using mysqli to connect to database you will use it like this:
<?php
$servername = "localhost";
$username = "yourusername";
$password = "yourpassword";
$dbname = "yourdatabse";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
using PDO, you would connect like this:
<?php
$host = 'localhost';
$db = 'yourdb';
$user = 'yourusername';
$pass = 'yourpassword';
$charset = 'utf8';
$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
$opt = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
$pdo = new PDO($dsn, $user, $pass, $opt);
?>
The are good tutorials on the net that you can use to better your understanding, I personally enjoy this site https://phpdelusions.net/pdo You should visit it you will be a pro in no time.
use mysqli_connect("localhost","root","","db_name");
or use
store this in separate php file.
include that file like this. include 'db.php';
try this....
try this
<?php
$con = mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
Is there a way to connect to your database without having to always type:
$dbHost = "localhost";
$dbUser = "root";
$dbPass = "pass123";
$dbName = "LoginDatabase";
$db = new PDO("mysql:dbname=$dbName;host=$dbHost;port=3306", $dbUser, $dbPass);
Is it possible to have a file that always connects to it?
As suggested in comment,
You need to make a connection.php file and add your connection code into it.
connection.php
$dbHost = "localhost";
$dbUser = "root";
$dbPass = "*****";
$dbName = "LoginDatabase";
$db = new PDO("mysql:dbname=$dbName;host=$dbHost;port=3306", $dbUser, $dbPass);
In this file you have $db object.
in other PHP file, write
include 'connection.php';
In that file you can directly access $db object.
Suggestion: You should also check db connection error in your connection.php file. So if you have any connection error then you can fetch it.
instead of writing this line directly
$db = new PDO("mysql:dbname=$dbName;host=$dbHost;port=3306", $dbUser, $dbPass);
Use below code to check error also.
$dsn = "mysql:host=$dbHost;dbname=$dbName;charset=utf8";
$opt = array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
);
$db = new PDO($dsn,$dbUser,$dbPass, $opt);
For more detail, Refer this PDO tag wiki.
I am developing a login/register system for a website but when i'm trying to connect to the database it gives this error:
Warning: mysqli_connect(): (HY000/1045): Access denied for user 'root'#'localhost' (using password: YES) in C:\xampp\htdocs\register.php on line 17
I'm using a external config file wich is been imported.
My config file:
// Connection details
$servername = "127.0.0.1";
$username = "root";
$password = "password";
$dbname = "database";
My connection code:
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
I found one solution that worked to connect but then my config file was useless.
No config but only this:
// Create connection
$conn = mysqli_connect('127.0.0.1', 'root', 'password', 'database');
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
If someone nows a solution where i can keep using my config file please post it here.
Thanks in advance.
Well, if the connection works with this values:
$conn = mysqli_connect('127.0.0.1', 'root', 'password', 'database');
But not with this values:
$servername = "127.0.0.1";
$username = "root";
$password = "";
$dbname = "cloud";
Why don't you just set the "working" values into your config file. So it would look like this:
$servername = "127.0.0.1";
$username = "root";
$password = "password";
$dbname = "database";
Another solution which is described here includes the creation of a new user, so you don't connect via root, but the new created user.
Im having trouble with MySQLi.
Every time I run this code it returns an error on line 13(mysql_select_bd()).
I cant figure out where the problem is.
Code:
<?php
$conn_error = 'Could not connect';
$mysqli_host = 'localhost';
$mysqli_user = 'root';
$mysqli_password = '';
$mysql_db = 'a_database';
#$mysqli_conn = mysqli_connect($mysqli_host, $mysqli_user, $mysqli_password);
mysqli_select_db('a_database', $mysqli_conn);
?>
You have an incorrect usage of the function:
mysqli_select_db('a_database', $mysqli_conn);
The connection must come first before the database name in the arguments:
mysqli_select_db($mysqli_conn, 'a_database');
// ^ connection object, then database name
Alternatively, you could also do this:
$mysqli_conn = mysqli_connect($mysqli_host, $mysqli_user, $mysqli_password, $mysql_db);
Or the object oriented interface:
$mysqli_conn = new mysqli($mysqli_host, $mysqli_user, $mysqli_password, $mysql_db); // personal preference
Instead of doing that you can do something like this:
$conn = mysqli_connect('localhost', 'root', '', 'a_database');