This question already has an answer here:
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(1 answer)
Closed 5 years ago.
I just need a simple mysqli-Connection invoked from a php-Script. The following script doesn't respond. Would be way easier if some kind of error-message appears, or even some echo-statements for debugging purposes. But no. Only white screen.
<?php
private $db_config_file = realpath(dirname(__FILE__)) . "config/db.ini";
private $db = NULL;
if (!file_exists($this->db_config_file)) {
$msg = "The config-file <code>"
. $this->db_config_file
. "</code> doesnt't exist.";
show_error_page($msg);
}
$config = parse_ini_file($this->db_config_file);
$host = $config[$host];
$user = $config[$user];
$password = $config[$password];
$dbname = $config[$dbname];
$conn = new mysqli($host, $user, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} else echo "Connection established!";
$conn->close();
?>
The db.ini:
host = localhost
dbname = bl
user = mm
password = noadmin
try to change
$host = $config[$host];
$user = $config[$user];
$password = $config[$password];
$dbname = $config[$dbname];
to
$host = $config['host'];
$user = $config['user'];
$password = $config['password'];
$dbname = $config['dbname'];
you are using $host = $config[$host], it need to be $config['host']
Related
So I imported mysql data to phpadmin, under codeigniter and xampp, but when I run the code, I got an error of Class "mysqli" not found
image error
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "sex_disaggrated";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
I already change the **.env **file. into this one
database.default.hostname = localhost
database.default.database = sex_disaggrated
database.default.username = root
database.default.password =
database.default.DBDriver = MySQLi
EDIT:
Php Mysqli
This question already has answers here:
MySQL Error: : 'Access denied for user 'root'#'localhost'
(28 answers)
Closed 2 years ago.
The code
index.php file
<?php
include_once 'includes/dbh.php';
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<?php
$sql = "SELECT * FROM data";
$result = mysqli_query($conn, $sql);
$datas = array();
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
$datas[] = $row;
}
}
//print_r($datas);
//foreach ($datas[0] as $data) {
// echo $data['text']." ";
//}
?>
</body>
</html>
includes file
<?php
$server = "localhost";
$username = "";
$password = "";
$database = "test";
$conn = mysqli_connect($server, $username,
$password, $database);
if (!$conn) {
die("Connection failed: " .
mysqli_connect_error());
}
The Warning
Warning: mysqli_connect(): (HY000/1045): Access denied for user ''#'localhost' (using password: NO) in C:\xampp\htdocs\databasetoarray\includes\dbh.php on line 8
Connection failed: Access denied for user ''#'localhost' (using password: NO)
Other info
The database and php files are all correctly named.
The Goal
I am trying to get an array out of a database using a tutorial I found on the internet.
The Questions
Does this method even work if using correct coding? If so what can I change to make it work?
The default password for mysql is root if you want it to set another you should change it.
Please connect to localhost/phpmyadmin and verify the credentials and update it here:
$server = "localhost";
$username = "";
$password = "";
$database = "test";
By default the username will be root and password will be blank.
on localhost you shold use username is root, password blank and correct database name use following code
$server = "localhost";
$username = "root";
$password = "";
$database = "test";
$conn = mysqli_connect($server, $username,
$password, $database);
if (!$conn) {
die("Connection failed: " .
mysqli_connect_error());
}
EDIT: SOLVED
I'm trying to pass some variables for a mysql connection from a separate file. However, they seem to only be coming through in a string and not as variables.
connect.php:
<?php
function connect(){
require ($_SERVER['DOCUMENT_ROOT'].'/db-connect.php');
$conn1 = new mysqli($servername, $username, $password, $dbname);
if ($conn1->connect_error) {
die("Connection failed: " . $conn1->connect_error);
}
echo $conn1->host_info . "\n";
}
connect();
?>
db-connect.php:
<?php
$servername = "localhost";
$username = "-";
$password = "--";
$dbname = "---";
?>
Message coming through:
$servername = "localhost"; $username = "-"; $password = "--"; $dbname = "---";Connection failed: Access denied for user '-'#'localhost' (using password: NO)
notes:
The $password at the end is different that what is in the db-connect.php file. It is an older password that I used to have when I first tried it. Also, the user '-'#'localhost' is an old user and not the user from $username.
Any ideas please and thanks?
Matt
Perhaps this way around may work better for you.
<?php
require ($_SERVER['DOCUMENT_ROOT'].'/db-connect.php');
function connect(){
global $servername, $username, $password, $dbname;
$conn1 = new mysqli($servername, $username, $password, $dbname);
if ($conn1->connect_error) {
die("Connection failed: " . $conn1->connect_error);
}
echo $conn1->host_info . "\n";
}
connect();
?>
This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 7 years ago.
When I try to insert mysql rows i get this error:
I found this code at: www.w3schools.com/php/php_mysql_insert.asp
Parse error: syntax error, unexpected T_VARIABLE in C:\xampp\htdocs\enviar.php on line 17
This is the code:
<?php
$servername = "localhost";
$username = "*****";
$password = "*****";
$dbname = "*******";
$produto = $_REQUEST['produto'];
$mesa = $_REQUEST['mesa'];
$conc = "fila"
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Send information to mysql
$sql = "INSERT INTO lanchesystem ( Produto, Mesa, conc)
VALUES ('$produto', '$mesa', '$conc')";
if ($conn->multi_query($sql) === TRUE) {
echo "Pedido ConcluĂdo!";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
// Close connection
$conn->close();
?>
How to solve this??
Add a semicolon:
$conc = "fila";
I also recommend using a text editor with a linter. It'll save you some time and headache.
I am new to PHP and need to modify some code in order to compile with my Microsoft SQL Server. The original code is like this. I downloaded it from usercake
<?php
/*
UserCake Version: 2.0.2
http://usercake.com
*/
//Database Information
$db_host = "localhost"; //Host address (most likely localhost)
$db_name = "202"; //Name of Database
$db_user = "202"; //Name of database user
$db_pass = "password"; //Password for database user
$db_table_prefix = "uc_";
GLOBAL $errors;
GLOBAL $successes;
$errors = array();
$successes = array();
/* Create a new mysqli object with database connection parameters */
$mysqli = new mysqli($db_host, $db_user, $db_pass, $db_name);
GLOBAL $mysqli;
if(mysqli_connect_errno()) {
echo "Connection Failed: " . mysqli_connect_errno();
exit();
}
//Direct to install directory, if it exists
if(is_dir("install/"))
{
header("Location: install/");
die();
}
?>
I already installed sqlsrv and tested the link. It works with my database. Then I changed the code to this:
<?php
//Database Information
$server = "servername";
$connectionInfo = array("Database"=>"databasename","UID"=>"xxxxxx", "PWD"=>"xxxxxx" );
$db_table_prefix = "uc_";
GLOBAL $errors;
GLOBAL $successes;
$errors = array();
$successes = array();
/* Create a new sqlsrv object with database connection parameters */
$mssqlsrv = new sqlsrv($server, $connectionInfo);
GLOBAL $mssqlsrv;
if(sqlsrv_connect_errno()) {
echo "Connection Failed: " . sqlsrv_connect_errno();
exit();
}
//Direct to install directory, if it exists
if(is_dir("install/"))
{
header("Location: install/");
die();
}
?>
I get the following error message:
Fatal error: Class 'mssql' not found in
I think this line is the problem:
$mssqlsrv = new sqlsrv($server, $connectionInfo);
But I do not know how to fix this.
I would use PDO in this case: http://www.php.net/manual/en/pdo.construct.php
You can create a DSN connection to SQL Server
$dsn = "sqlsrv:Server=servername;Database=databasename"
$user = 'dbuser';
$password = 'dbpass';
try {
$dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
The information to connect to SQL server is available here: http://www.php.net/manual/en/ref.pdo-sqlsrv.connection.php