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();
}
?>
Related
I am using a Windows 10 PC which has XAMPP installed (v3.2.2).
If I do this in MySQL:
select version();
-- 10.1.32-MariaDB
That confirms the version...
I have a test database called "wp", and the username and password are both set to "wp".
I can connect to the database without a problem using PDO, but I can't connect using mysqli or mysqli_connect, and I can't see why. I've wasted a few hours on this today so far. I'm trying to install Wordpress on XAMPP but the installer says it can't connect to the database, so I have been testing around this, hence this question.
mysqli
$servername = "localhost";
$username = "wp";
$password = "wp";
$dbname = "wp";
$db = new mysqli($servername, $username, $password, $dbname);
// Warning: mysqli::__construct(): (HY000/1045): Access denied for user 'wp'#'localhost' (using password: YES) in C:\xampp\public_html\_conn.php on line 6
mysqli_connect
$host = "localhost";
$uname = "wp";
$pwd = "wp";
$dbname = "wp";
$conn = mysqli_connect($host, $uname, $pwd, $dbname);
// Warning: mysqli_connect(): (HY000/1045): Access denied for user 'wp'#'localhost' (using password: YES) in C:\xampp\public_html\_conn.php on line 13
pdo
$config['db'] = array(
'host' => 'localhost',
'username' => 'wp',
'password' => 'wp',
'dbname' => 'wp'
);
try {
$pdo = new PDO('mysql:host=' .$config['db']['host']. ';port=33066;dbname=' .$config['db']['dbname'], $config['db']['username'], $config['db']['password']);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$pdo->setAttribute(PDO::NULL_EMPTY_STRING, true);
$pdo->exec("SET CHARACTER SET utf8mb4");
}
catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
// connects fine
The wp user has the correct level of access:
Since the same user details are used to connect via PDO, but they don't work for the other methods.
I have checked using phpinfo(); and can see that mysqli is set up as far as I can see (I searched for mysqli_connect but can't see it listed):
mysqli phpinfo
Am I missing something obvious?
Try this
//This line of code connects to mysql database
define("HOST_NAME", "localhost:3306");
define("HOST_USER", "wp");
define("HOST_PASS", "wp");
define("HOST_DB", "wp");
$db = new mysqli(HOST_NAME, HOST_USER, HOST_PASS, HOST_DB);
// This line of code checks if connection error exists.
if($db->connect_error) {
echo $db->connect_errno . " " . $db->connect_error;
} else {
echo "Connection successful.";
}
I am trying to connect to my MySQL database using the following PHP code
<?php
require "credentials.php";
$conn = mysqli_connect($hostname, $username, $password, $databaseName);
if(mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
And I am getting the following output:
Failed to connect to MySQL: Connection refused
What could be the reasons that this is happening, and how can I fix it?
Thanks in advance.
from your code it seems that you are using undefined variables to connect to database. if you load them from external file, than use something like this in that file:
define("DBPASSWORD", "database_pass");
define("DBUSERNAME", "databaseusername");
define("DBNAME", "database");
define("DBHOST", "database_host");
and than use in file you shown us:
$conn = mysqli_connect(DBHOST, DBUSERNAME, DBPASSWORD, DBNAME);
OR another way, the way I can't recomend is using globals.
WARNING: I discourage using globals, when not needed.
Your credentials.php should look like that:
global $databaseName;
global $password;
global $username;
global $username;
$databaseName= "db_name";
$password= "db_password";
$username= "db_username";
$hostname= "db_host";
and than in other places, first you need to call the globals before usage.
require "credentials.php";
global $databaseName;
global $password;
global $username;
global $username;
$conn = mysqli_connect($hostname, $username, $password, $databaseName);
I am using Cpanel to develop my application. It gives the error
Fatal error: Call to a member function set_charset() on a non-object in /home/restaur /public_html/restaurant/includes/connect_database.php on line 3
when I access to "www.yyyyy.com/filename/"
And here is my connect_database script
<?php
include($_SERVER['DOCUMENT_ROOT'].'/YOUR_FOLDER/variables/variables.php');
$connect->set_charset('utf8');
?>
my close_database script
<?php
include($_SERVER['DOCUMENT_ROOT'].'/YOUR_FOLDER/variables/variables.php');
$connect->close();
?>
my variable script
<?php
// database configuration
$host ="db_host_name";
$user ="db_username";
$pass ="db_pasword";
$database = "database_name";
$connect = new mysqli($host, $user, $pass,$database) or die("Error : ".mysql_error());?>
it should be like
$host ="db_host_name";
$user ="db_username";
$pass ="db_pasword";
$database = "database_name";
// variables.php
$connect = new mysqli($host, $user, $pass, $database);
/* check connection */
if ($connect->connect_errno) {
printf("Connect failed: %s\n", $connect->connect_error);
exit();
}
Now in connect_database.php
// Make sure your included path is correct
include($_SERVER['DOCUMENT_ROOT'].'/YOUR_FOLDER/variables/variables.php');
global $connect;
$connect->set_charset('utf8');
this will help you to know more in depth global
I was having this exact issue, i was having encoding problems but adding
$connect->set_charset('utf8');
(my variable was actually $conn->set_charset('utf8');) just after
$conn = new mysqli($servername, $username, $password, $dbname);
and it worked beautifully.
I want to connect to mysql database using php and following is my configuration file.
<?php
$host = "localhost";
$db = "payroll"
$username ="root";
$password = "";
mysql_connect ($host,$username,$password);
mysql_select_db($db,$username);
?>
but when I run my program it gives me this error:
SQL error: No database selected SQL errno: 1046
SQL: select language, admin from user where username='admin' and password='abc123'
What's wrong with my code?
You forgot a semicolon here
$db = "payroll";
^--- Here
Don't forget to enable error reporting on your code. This is how you do it.
This(mysql_*) extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used. Switching to PreparedStatements is even more better to ward off SQL Injection attacks !
Switch to Prepared Statements..
A kickstart example..
<?php
$dsn = 'mysql:dbname=payroll;host=localhost';
$user = 'root';
$password = '';
try
{
$dbh = new PDO($dsn, $user, $password ,array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e)
{
echo 'Connection failed: ' . $e->getMessage();
}
Read more here on PHP Manual
$mysqlhost="localhost"; // MySQL-Host
$mysqluser="user"; // MySQL-User
$mysqlpwd="password"; // Password
$connection=mysql_connect($mysqlhost, $mysqluser, $mysqlpwd) or die
("CouldnĀ“t connect");
$mysqldb="database"; // Your Database
mysql_select_db($mysqldb, $connection) or die("Couldnt select database");
I always use this. Here you get every errormessage you need to find your error.
Try this
<?php
$host = "localhost";
$db = "payroll"
$username ="root";
$password = "";
$con = mysql_connect ($host,$username,$password);
mysql_select_db($db,$con);
?>
I'm having a problem with a function that connects me to the database using mysqli. What I'm aiming to have is to just type getConnected(); where I need the connection.
This is the code:
function getConnected()
{
$host = 'localhost';
$user = 'logintest';
$pass = 'logintest';
$db = 'vibo';
$mysqli = new mysqli($host, $user, $pass, $db);
if ($mysqli->connect_error) {
die('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
}
This is the error I get when I try to use $mysqli after calling getConnected():
Notice: Undefined variable: mysqli in C:\xampp\htdocs\xampp\loginsystem\index.php on line 19
As some users have suggested (and is the best way), return the mysqli instance
function getConnected($host,$user,$pass,$db) {
$mysqli = new mysqli($host, $user, $pass, $db);
if($mysqli->connect_error)
die('Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error());
return $mysqli;
}
Example:
$mysqli = getConnected('localhost','user','password','database');
You don't need to create such function. The connection to mysqli is only 3 lines of code when done properly. Definition and execution of such function would add twice as much.
Remember to always follow the three steps when opening a connection:
Enable mysqli error reporting.
Create instance of mysqli class.
Set the proper charset. Recommended one is utf8mb4
If you would like to have these 3 lines wrapped in a function you could do something like this:
function getConnected(): \mysqli {
$host = 'localhost';
$user = 'logintest';
$pass = 'logintest';
$db = 'vibo';
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli($host, $user, $pass, $db);
$mysqli->set_charset('utf8mb4');
// return the instance of mysqli class
return $mysqli;
}
However, hardcoding the values does not make much sense. The values should be coming either from environment variables (e.g. read using getenv() function) or from config file. For example:
function getConnected(): \mysqli {
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli(getenv('DB_HOST'), getenv('DB_USER'), getenv('DB_PASS'), getenv('DB_NAME'));
$mysqli->set_charset('utf8mb4');
// return the instance of mysqli class
return $mysqli;
}