What does the mysqli_error() expects parameter 1 to be mysqli, null given mean?
You need to define: $dbc
before
$code = mysqli_real_escape_string ($dbc, $_GET['invite']);
ex:
$dbc = mysqli_connect("localhost", "my_user", "my_password", "world");
Warning: mysqli_error() expects parameter 1 to be mysqli, null given in /users/mikadoru/www/register.php on line 16
You are not opening any DB!
From PHP manual:
mysqli_connect($db_host,$db_user,$db_pass,$db_database,$db_port);
Warning: mysqli_error() expects parameter 1 to be mysqli, null given in
Look at this line:
mysqli_query($db_mysqli_connection,$query) or die(mysqli_error($connection).$query);
You are using $db_mysqli_connection
to connect, but $connection
to check for error. You must replace $connection
with $db_mysqli_connection
.
Warning: mysqli_error() expects parameter 1 to be mysqli, string given in
There are three main problems with your code:
- Wrong SQL. You have to learn basic SQL syntax prior trying to use SQL from PHP.
- Wrong mysqli syntax. You have to use prepared statements instead of adding variables right in the query.
- Wrong error reporting. You have to set mysqli in exception mode instead of checking every query result manually.
To solve them one by one, read these three helpful answers:
- When to use single quotes, double quotes, and backticks in MySQL
- How can I prevent SQL injection in PHP?
- How to get mysqli error in different environments?
PHP mysqli_query() expects parameter 1 to be mysqli, null given in
Assuming your connection class is in 'connection.php',
class connector {
private $db_handle; //db handles are not strings. Unsure why you were setting this to a string by default.
private $user_name;
private $password;
private $database;
public static $server = "127.0.0.1";
//I'd recommend against this, unless you know 100% that your live server will use 'localhost'
function __construct($user_name="", $password="", $database="")
{
$this->user_name = $user_name;
$this->password = $password;
$this->database = $database;
//There really isn't a need to not have your connector here - but that's me being lazy.
$this->db_handle = mysqli_connect(self::$server,$this->user_name, $this->password, $this->database);
}
function query($sql){
//Do some checks, or if you use prepared statements, accept the prepared data array and parse it here.
return mysqli_query($this->db_handle, $sql);
}
function __destruct()
{
mysqli_close($this->db_handle);
}
}
Then, in your page, do this:
include 'connector.php';
$db = new connector('user','pw','some_db');
$query = $db->query('DELETE FROM table WHERE user_id='.$_POST['something']);
//FOR THE LOVE OF GOD, DO NOT ACTUALLY DO THIS. I DID IT ONLY FOR THE SAKE OF BREVITY. USING RAW DATA SUBMITTED FROM THE USER
// IS DANGEROUS AND LEADS TO SQL INJECTIONS. *ALWAYS* SANITIZE YOUR DATA.
The reason is add the mysqli_connect
and mysqli_close
calls in the __construct
and __destruct
methods accordingly is because I am incredibly lazy and do not want to have to call a ->connect()
method. And to be honest, my laziness has sometimes saved me a lot of work because I do not want to have to repeat myself over and over again (found out this is a coding philosophy called DRY - Don't Repeat Yourself).
How can I find the execution time of a section of my program in C?
You referred to clock()
and time()
- were you looking for gettimeofday()
?
That will fill in a struct timeval
, which contains seconds and microseconds.
Of course the actual resolution is up to the hardware.
PHP.ini causes Warning: mysqli_error() expects parameter 1 to be mysqli, null given in/sys/index.php on line 19
You can use
function consql(&$con){
$con=mysqli_connect("host","BDusername","DBPassWord","DB") or die( mysqli_error($con));
}
So this is call by reference and $con can use at any page once you call the function
Or use global $con;
before the connection line
Warning: mysqli_error() expects exactly 1 parameter, 0 given error
mysqli_error() needs you to pass the connection to the database as a parameter. Documentation here has some helpful examples:
http://php.net/manual/en/mysqli.error.php
Try altering your problem line like so and you should be in good shape:
$query = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error($myConnection));
Related Topics
How to Convert All Images to Jpg Format in PHP
Flip Associative Array and Store New Values in Subarrays to Prevent Losing Duplicated Values
Algorithm to Get the Excel-Like Column Name of a Number
Redirecting from Http to Https with PHP
Determine If Business Is Open/Closed Based on Business Hours
How to Pass an Array Using PHP & Ajax to JavaScript
Laravel Eloquent Sort by Relation Table Column
Adding 30 Minutes to Datetime PHP/Mysql
How to Run a .Sh File from PHP
Show Stock Status Next to Each Attribute Value in Woocommerce Variable Products
How the Usort() Sorting Algorithm Works
Upgrading PHP on Centos 6.5 (Final)
Explain $Ci =& Get_Instance();
Best Way to Handle Errors on a PHP Page
SQL Query, Select Nearest Places by a Given Coordinates
PHP Files Are Downloaded by Browser Instead of Processed by Local Dev Server (Mamp)