Fatal error: Call to a member function set_charset() - php

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.

Related

Why isn't my PHP able to connect to MySQL?

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);

cannot redirect after mysqli connection error [duplicate]

This question already has answers here:
How to redirect PHP page on MySQL database connection failure
(3 answers)
Closed last month.
I am new to php. I am trying to redirect to a error page in php when I get a mysql connection error, but I get an error stating that I cannot modify the header because the output was already sent on line 8 (where I created the connection). Please can you advise on the proper way of doing this?
<?php
$servername = "localhost:3306";
$username = "danny";
$password = "sql1";
$dbname = "testdb";
try{
$conn = new mysqli($servername, $username, $password, $dbname);
}
catch(Exception $e){
header('Location: /connection_error.php');
die();
}
?>
Thanks
Danny
You need to add this line at the top to tell mysqli to throw exceptions
mysqli_report(MYSQLI_REPORT_STRICT);
$servername = "localhost:3306";
$username = "danny";
$password = "sql1";
$dbname = "testdb";
try{
$conn = new mysqli($servername, $username, $password, $dbname);
}
catch(Exception $e){
header('Location: /connection_error.php');
die();
}
Additionally, you can always check the error code from the last connect call by using mysqli::$connect_errno, whereas it returns an error code value for the last call, if it failed. Zero means no error occurred (hence the falsey).
<?php
/* Variable Defaults */
$servername = 'localhost:3306';
$username = 'danny';
$password = 'sql1';
$dbname = 'testdb';
/* Make Connection */
$conn = new mysqli($servername, $username, $password, $dbname);
/* Check Connection */
if($conn->connect_errno) {
/* Redirect */
header('Location: /connection_error.php');
exit();
}
?>
Try this
<?php
ob_start();
$servername = "localhost:3306";
$username = "danny";
$password = "sql1";
$dbname = "testdb";
try{
$conn = new mysqli($servername, $username, $password, $dbname);
}
catch(Exception $e){
header('Location: /connection_error.php');
}
ob_flush();
?>

MYSQL Database connection for php

I am using my website database with php mysql_connect but my hosting provider is saying I need to use mysqli_connect.
When I edit connection file and update change method its not working with mysqli.
Currently using this code:
MSQL Method:
$con=mysql_connect("localhost","username","password");
mysql_select_db("databasename");
and for mysqli I am using this code but fail
MYSQLI Method:
$con = mysqli_connect("localhost","my_user","my_password","my_db");
Please help. Why is mysqli not working? Is my method wrong?
Try this:
Example (MySQLi Object-Oriented)
<?php
$servername = "localhost";
$username = "username";
$password = "password";
` $dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
Example (MySQLi Procedural)
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>

Php, mysql coding error

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();
}
?>

Global database connection inside functions

Trying to global database connection in all functions in a .php file.
sql.php:
<?php
$servername = "XXXXX";
$username = "XXXXX";
$password = "XXXXX";
$dbname = "XXXXX";
$conn = new mysqli($servername, $username, $password, $dbname);
mysqli_set_charset($conn, "utf8");
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
save.php
$conn = require_once('../path/to/sql.php');
function save()
{
global $conn;
$sql = "INSERT INTO `content` (title, text, cat)
VALUES ('".$_POST["title"]."', '".$_POST["text"]."', '".$_POST["category"]."')";
if ($conn->query($sql) === TRUE) {
show();
}
$conn->close();
}
well i want to get sql.php with require_once and then make it global then put this in save() function.
error is:
Fatal error: Call to a member function query() on a non-object in
/homepages/3/xxxxxxxx/htdocs/xxxxxxxx/xxxxxxxx/content.php on line 37
actually i don't know this way to make it global is correct or not, so please help me to choose best way to make a global database connection inside the functions.
Try
define('DATABASE_USERNAME', 'root');
define('DATABASE_PASSWORD', 'password');
define('DATABASE_HOST', 'localhost');
define('DATABASE_DBNAME', 'db');
$conn = new mysqli(DATABASE_HOST, DATABASE_USERNAME, DATABASE_PASSWORD, DATABASE_DBNAME);
Named constants are great for static global data.
Also you just need to do
require_once 'path/to/sql.php';
And not
$conn = require_once 'path/to/sql.php';
$conn is already being set in your require_once.

Categories