mysqli_connect From Included File Not Working - php

I have this mysqli_connect on a file named config.php :
$DbServer = 'localhost';
$DbUser = 'username';
$DbPassword = 'password';
$DbName = 'dbname';
$con = mysqli_connect($DbServer, $DbUser, $DbPassword, $DbName);
then I have another PHP file named sendingrequest.php :
<?php
session_start();
require_once ('config.php');
$Key = mysqli_real_escape_string ($con, $_GET["key"]);
...
?>
this sendingrequest.php always produce this error message :
Warning: mysqli_real_escape_string(): Couldn't fetch mysqli in /home/public_html/sendingrequest.php on line 4
I tried to modify the script by putting mysqli_connect() on the same file, not by require_once() anymore and surprisingly it works...
<?php
session_start();
//require_once ('config.php');
$DbServer = 'localhost';
$DbUser = 'username';
$DbPassword = 'password';
$DbName = 'dbname';
$con = mysqli_connect($DbServer, $DbUser, $DbPassword, $DbName);
$Key = mysqli_real_escape_string ($con, $_GET["key"]);
...
?>
what's wrong with my require_once? why mysqli_real_escape_string failed to using it? thanks!

Related

convert master connecting error

I got following error when i run code:
Notice: Use of undefined constant newdb - assumed 'newdb' in C:\xampp\htdocs\cj\include\conn.php on line 9 No database selected
The following code for connecting i am using in conn.php file
,
<?php
$lusername = "root";
$lpassword = "";
$lhostname = "localhost";
// connection to the database
$dbhandle = ($GLOBALS["___mysqli_ston"] = mysqli_connect($lhostname, $lusername, $lpassword)) or die("Unable to connect to mysql");
mysqli_select_db($GLOBALS["___mysqli_ston"], newdb) or die("Unable to connect to mysql");
// echo "Connected to mysql<br>";
?>
how to fix it?
This is just something to consider: try using it without ["___mysqli_ston"]
Example
<?php
$servername = "localhost";
$username = "";
$password = "yourpass";
$dbname = "yourDBname";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Read special character sets
$conn->set_charset("utf8");
// Check connection
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
?>
Just an example of different easier approach all up to you.
For the converter
Example
<?php
$servername = "localhost" ;
$username = "root";
$pass = "";
$con = ($GLOBALS["___mysqli_ston"] = mysqli_connect($servername, $username, $pass)) or die("Problem occur in connection");
$db = ((bool)mysqli_query($con, "USE " . info));
?>

Hardcoded user/pass work for MySQL connection, but strings from file do not

First off, I'm trying to make sure that I'm not showing my MySQL password in my index page's source code. I've determined that making a "mysql.conf" file with the information I need will be sufficient.
Here is the section of code, pre-conf file. This worked without any problems:
$dbhost = "mysql.host.com";
$dbuser = "username";
$dbpass = "password";
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
Now, here is the configuration file's contents (mysql.conf):
mysql.host.com
username
password
And the corresponding changes to the code...
$dbConfig = file("./config/mysql.conf");
$dbhost = $dbConfig[0];
$dbuser = $dbConfig[1];
$dbpass = $dbConfig[2];
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
However, with the changes to use the configuration file, the MySQL connection now fails, giving me this error response:
"Could not connect: Access denied for user 'username'#'chain-lightning.dreamhost.com' (using password: YES)"
What am I missing? I've triple-checked that the text in the configuration file is the same as when I used static strings. Thanks!
The error you are getting mostly because of the data getting from file is not as you think. all your value will be added with extra newline value.
from http://php.net/manual/en/function.file.php
Returns the file in an array. Each element of the array corresponds to
a line in the file, with the newline still attached.
use trim function with your variable it will work fine.
$dbConfig = file("./config/mysql.conf");
$dbhost = trim($dbConfig[0]);
$dbuser = trim($dbConfig[1]);
$dbpass = trim($dbConfig[2]);
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
or you can use FILE_IGNORE_NEW_LINES flag in file function
$dbConfig = file("./config/mysql.conf", FILE_IGNORE_NEW_LINES);
$dbhost = $dbConfig[0];
$dbuser = $dbConfig[1];
$dbpass = $dbConfig[2];
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);

Php login to your database

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.

Fatal error: Call to undefined function new_mysqli() in C:\wamp\www\work\conn.php on line 8

I'm making a login page. The connection to mysqli keeps showing me this message:
Fatal error: Call to undefined function new_mysqli() in
C:\wamp\www\work\conn.php on line 8
conn.php:
<?php
$dbhost = "loaclhost";
$dbuser = "root";
$dbpass = "";
$dbname = "users";
$conn = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
?>
change $dbhost = "loaclhost"; to $dbhost = "localhost";

I can not get mysqli_connect to work using Xampp

I want to use mysqli_connect and I have tried all kinds of variations and none of them have work. They all throw "No database selected". I am having a configuration problem or a code error? THIS WORKS
$host = "localhost";
$uname = "root";
$pwd = "";
$database = "testdb";
$conn = mysql_connect($host, $uname, $pwd);
$database = mysql_select_db($database, $conn)
I don't know to make this question any clearer,,,I want to use
THESE DO NOT WORK
$host = "localhost";
$uname = "root";
$pwd = "";
$database = "testdb";
$conn = mysqli_connect($host, $uname, $pwd);
$database = mysqli_select_db($database, $conn);
or
$host = "localhost";
$uname = "root";
$pwd = "";
$database = "testdb";
$conn = mysqli_connect($host, $uname, $pwd, $database);
but it is NOT WORKING, I want to know why it is not working and how to make it work using Xampp. It is most likely a configuration problem but I am lost. I have asked this question before there doesn't seem to be anyone that knows how to fix.
$database = mysqli_select_db($database, $conn);
Is the incorrect syntax, it's
mysqli_select_db($conn, $database);
Additionally, you could delete that line and do the following:
$conn = mysqli_connect($host, $uname, $pwd, $database);
I have tested this on my live server
$conn = mysqli_connect($host, $uname, $pwd, $database);
There is definitely a configuration problem and I will check further.

Categories