New mysqli () is reset to null when using a function - php

I am currently working with mySQL and php to create a web application.
In my main index.php file, I have
<?php
require_once('includes/config.php');
require_once('includes/functionName.php');
?>
My config.php is as below:
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "DB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected Successfully!";
?>
When I call the actual function functionName("test");
it goes into the actual functionName.php and goes to
$sql = "UPDATE Table SET id = " . $test1 . " WHERE name = \"". $name . "\"";
echo $sql;
var_dump ($conn);
This returns UPDATE Table SET id = 09 WHERE name = "test" which is correct as an SQL query.
However, my $conn returns NULL, and as a result I get FATAL ERROR query on NULL.
Please tell me why my $conn is returning NULL, and failing my query?

Thank you it was solved after adding line global $conn

Related

MySQL table value be equal with php var (then display it)

I have the following MySQL table and I would like to do that RVR11 (500) be equal with a php var $RVR11
I have this code but it doesnt work.
$servername = "localhost";
$username = "root";
$password = "";
$conn = new mysqli($servername, $username, $password);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
$price = mysql_query("SELECT RVR11 FROM tablename");
$result = mysql_fetch_array($RVR11);
echo $result['RVR11'];
Sorry about this beginner question.
You should choose a database before any queries, you can pass the fourth arguments to new mysqli constructor.

How to display number of rows in a sql table using php?

I'm trying to get lines' number from the result of the sql query, so I started by connecting to the database, then checking if the connection was established and finally I tried to count the number of lines, here's the code :
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "db_test";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$res = $conn->query('SELECT COUNT (id_offre) AS nb FROM offres');
$data = $res->fetch();
$nb = $data['nb'];
echo $nb;
?>
I get these errors :
Uncaught Error: Call to a member function fetch() on boolean
Call to a member function fetch() on boolean
I used echo to check the 'nb' value, what is the problem?
Your query fails which makes $conn->query() to return false.
So, why does the query fail?
SQL doesn't allow for any spaces between function names and the parentheses, like you have at COUNT ().
So if you change:
SELECT COUNT (id_offre) AS nb FROM offres
to
SELECT COUNT(id_offre) AS nb FROM offres
...it should work.
Note: I would highly recommend you to have a read about mysqli::error() and add some error handling to your code.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "db_test";
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (! $conn) {
die("Connection failed: " . mysqli_connect_error());
}
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: ".$conn - > connect_error);
}
$res = $conn->query('SELECT * FROM offres');
$data_count = mysqli_num_rows($res);
echo($data_count);
?>
check this out!

SQL with PHP variables

I'm trying to update db record using the code below. Even if the redirection is done, the record isn't being updated as it should be. any tips please?
<?php
session_start();
?>
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "Mydatabase";
$id = $_GET["session_id()"];
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "UPDATE leads SET first_name='hola' WHERE id = '$id'";
if ($conn->query($sql) === TRUE) {
header( "thank-you.php" ); die;
} else {
echo "Error updating record: " . $conn->error;
}
$conn->close();
?>
This one causes the problem.
$id = $_GET["session_id()"];
session_id() itself is a PHP function. No need for quoting. Just remove the quotes and it should be fine.
$id = $_GET[session_id()];

Mysqli to variable in PHP

I've been a few days working on my project, but just stay stuck a specific part.
My problem is that i like to get a value from the database (mysqli)
But i always receive a 0. So, no value.
This is my code:
$servername = "localhost";
$username = "user";
$password = "pass";
$dbname = "dbname";
$serialkey = $_GET['hardwareSerial'];
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$user_idSQL = "SELECT user_id FROM ss_devices WHERE serialkey = '$serialkey'";
$user_idResult = $conn->query($user_idSQL);
$user_id = $user_idResult;
The script should see what the userid, it is in equality with the specified serial number.
But when i take a serial number like: FJRI433. I always get a 0. But in the database has this serial number user_id: 3.
I hope someone can help me out whith this problem.
Thanks.
You are executing the query but not fetching any results.
The manual has plenty of examples of using mysqli :
if ($result = $conn->query($query)) {
while ($row = $result->fetch_assoc()) {
//Do stuff with the next row.
}
}
Try this perhaps...
$servername = "localhost";
$username = "user";
$password = "pass";
$dbname = "dbname";
$serialkey = $_GET['hardwareSerial'];
$conn=mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if ( mysqli_connect_errno() ) {
die("Connection failed: " . mysqli_connect_error() );
}
$user_idSQL = "SELECT `user_id` FROM `ss_devices` WHERE `serialkey` = '$serialkey'";
if( $user_idResult = mysqli_query( $conn, $user_idSQL ) ) {
while ($row = mysqli_fetch_assoc($user_idResult)) {
echo $row['user_id'].'<br />';
}
}
mysqli_close( $conn );

I cant insert in mysql from php

whats the solution for this problem Number of elements in type definition string doesn't match number of bind variables
session_start();
$regValue1 = $_GET['un'];
$regValue2 = $_GET['pass'];
$regValue3 = $_GET['fn'] ;
$regValue4 = $_GET['ln'];
$regValue5 = $_GET['age'] ;
$regValue6 = $_GET['sex'];
$regValue7 = $_GET['em'] ;
echo "hello: ".$regValue3.".";
$servername = "localhost";
username = "root";
$password = "b4sonic";
$dbname = "blog";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$stmt = $conn->prepare("INSERT INTO register(un,pass,fn,ln,age,sex,email) VALUES (?,?,?,?,?,?,?)");
bind_param("sss",$regValue1,
$regValue2,$regValue3,$regValue4,$regValue5,$regValue6,$regValue7);
$stmt->execute();
echo "New records created successfully";
$stmt->close();
$conn->close();
?>
I have this code in php , i am trying to insert data to mysql but i face this problem Number of elements in type definition string doesn't match number of bind variables
Your type string in your statement doesn't have enough type specifiers in it.
bind_param("sss",$regValue1,$regValue2,$regValue3,$regValue4,$regValue5,$regValue6,$regValue7);
says that you have type "sss" which only corresponds to 3 of the 7 variables you specified. You need to add types for the rest.
From the documentation:
var1
The number of variables and length of string types must match the parameters in the statement.
The commands you are using are for using PDO to connect to an sql database and you are using mysqli. I gave an example using mysqli below that should work. The other option would be to change the connection to PDO type rather than mysqli.
<?php
session_start();
$regValue1 = $_GET['un'];
$regValue2 = $_GET['pass'];
$regValue3 = $_GET['fn'] ;
$regValue4 = $_GET['ln'];
$regValue5 = $_GET['age'] ;
$regValue6 = $_GET['sex'];
$regValue7 = $_GET['em'] ;
echo "hello: ".$regValue3.".";
$servername = "localhost";
$username = "root";
$password = "b4sonic";
$dbname = "blog";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO register(un,pass,fn,ln,age,sex,email)".
"VALUES (`$regValue1`,`$regValue2`,`$regValue3`,`$regValue4`,`$regValue5`,`$regValue6`,`$regValue7`)";
$conn->query($sql);
echo $conn->affected_rows. " new records created successfully";
$conn->close();
?>

Categories