warning of mysqli_real_escape_string() for this code [closed] - php

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
Query:
if ($_SERVER["REQUEST_METHOD"]=="POST") {
$username = mysqli_real_escape_string(trim($_POST["username"]), $db);
$password = mysqli_real_escape_string(trim($_POST["password"]), $db);
$password = md5($password);
$sql = "Insert into login(username,password) values('$username','$password');";
$result = mysqli_query($db,$sql);
echo"Successful Registration";
if($result) {
echo("Successfully updated");
}else{
die ("no database");
}
}
Error:
Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, string given in C:\xampp\htdocs\test.php on line 14
Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, string given in C:\xampp\htdocs\test.php on line 15
Warning: mysqli_query() expects parameter 1 to be mysqli, resource given in C:\xampp\htdocs\test.php on line 19
Successful Registrationno database

Your parameters are in the wrong order, read the documentation again. For example:
$username = mysqli_real_escape_string(trim($_POST["username"]), $db);
Should be:
$username = mysqli_real_escape_string($db, trim($_POST["username"]));
See http://php.net/mysqli_real_escape_string (the procedural style) for the right parameter order.

Related

Problem Trying to get property 'num_row' of non-object in and Undefined variable: stmt_result in Php [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I am facing a problem when i am try to check user and password from database while login it keep
reply an error message :
Notice: Trying to get property 'num_row' of non-object in /Applications/XAMPP/xamppfiles/htdocs/studyact/login.php on line 27
User name or Password is incorrect, please check and try again.
i type user and password correct! enter image description here
php file :
<?php
//html
$user_staff = $_POST["user_staff"];
$pass_staff = $_POST["pass_staff"];
// Create connection
$servername = "localhost";
$username = "root";
$password = "";
$db ="studyact";
$con = new mysqli($servername, $username, $password,$db);
// Check connection
if ($con->connect_error) {
die("Connection failed: " . $con->connect_error);
}else{
$stmt =$con-> prepare("select * from loginstaff where user_staff = ?");
$stmt->bind_param("s",$user_staff);
$stmt->execute();
$stmtresult = $stmt->get_result();
if($stmtresult-> num_row > 0){
$data = $stmtresult-> fetch_assoc();
if($data["pass_staff"] === $pass_staff){
echo "<h2>Login Successfully</h2>";
}
else{
echo "<h2> Sorry User name or Password is incorrect.</h2>";
}
}else{
echo "<h2> User name or Password is incorrect, please check and try again.</h2>";
}
}
?>
You've got a typo on line 27
if($stmtresult->num_rows > 0)
mysqli_stmt::$num_rows — Returns the number of rows fetched from the server

I keep getting these PHP error messages when trying to use MySqli [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I've provided the code, so people can better help me. Okay, I'm doing a PHP tutorial based on YouTuber mmtut's tutorials. Using local PHPMyAdmin, double checked that my naming conventions are all right, and am getting these two errors:
Warning: mysqli_query() expects parameter 1 to be mysqli, string given
in C:\xampp\htdocs\PHPMySQL\index.php on line 11
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result,
null given in C:\xampp\htdocs\PHPMySQL\index.php on line 12
My PHP code:
Within the includes folder:
$dbServername = "localhost";
$dbUsername = "root";
$dbPassword = "";
$dbName = "phplessons";
$conn = "mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName)";
?>
Within the index.php file:
<?php
include_once 'includes/dbh.inc.php';
?>
<!DOCTYPE html>
<html>
<body>
<?php
$sql = "SELECT * FROM posts;";
$result = mysqli_query($conn, $sql);
$resultCheck = mysqli_num_rows($result);
if($resultCheck > 0) {
while ($row = mysqli_fetch_assoc($result)) {
echo $row['subject'];
}
}
?>
</body>
</html>
Your connection property is a literal. You want something like this:
$conn = mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName);

What is the issue with this mysqli statement [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I am not quite sure how to convert the mysql code here to a mysqli version. I keep getting the error like:
Warning: mysql_result() expects parameter 1 to be resource, object given in....
Can you please help? thanks.
<?php
function the_user($username) {
$myqli = mysqli_connect("localhost", "root", "", "sometable");
$username = sanitize($username);
$user_query = mysqli_query($myqli, "SELECT COUNT('user_id') FROM 'users' WHERE 'username' = '$username'");
return (mysql_result($user_query, 0) == 1) ? true : false;
}
?>
The 's around the column names & table name. Should be -
SELECT COUNT(user_id) FROM users WHERE username = '$username'
Or backticks.
Also mixing mysql & mysqli.
Instead of mysql_result you should use mysqli like mysqli_fetch_array.
You should use :
$row = mysqli_fetch_array($user_query, MYSQLI_NUM);
return ($row[0] == 1) ? true : false;
Instead of:
return (mysql_result($user_query, 0) == 1) ? true : false;
Ofcourse don't forget to fix sql syntax as described in earlier answer:
$user_query = mysqli_query($myqli, "SELECT COUNT(user_id) FROM users WHERE username = '$username'")

Error in mysqli_num_rows [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
What's wrong with this code:
<?php
session_start();
if(!isset($_SESSION['username']) && isset($_COOKIE['username'], $_COOKIE['password']))
{
$checkQuery = "SELECT password, id FROM accounts WHERE username='".$db->real_escape_string($_COOKIE['username'])."'";
$checkResult = mysqli_query($db, $checkQuery);
$check = mysqli_fetch_array($checkResult);
if($check['password'] == $_COOKIE['password'] && mysqli_num_rows($checkQuery)>0)
{
$_SESSION['username'] = $_COOKIE['username'];
$_SESSION['userid'] = $check['id'];
}
}
?>
It shows this error:
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result,
string given...
Looks like you should change
mysqli_num_rows($checkQuery)
to
mysqli_num_rows($checkResult)

Problems with mysqli [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm trying to read some data from a form and then use them using php. I use only mysqli in my php file but I get some warnings and i don't understand why. Here is some code:
<?php
// Connects to your Database
$con = mysqli_connect("localhost", "root", "password", "database");
error_reporting(E_ALL);
ini_set('display_errors', 1);
$all_string = $_GET['name'];
if($all_string == NULL) exit("Nothing is written\n");
echo "$all_string"."<br/>";
$tok = strtok($all_string, ",");
$sql = "SELECT * FROM Suggrammata";
/*line 22*/$results = mysqli_query($sql, $con);
/*line 24*/while($is = mysqli_fetch_array($results))
{
.
.
.
?>
Here are the warnings:
Warning:
mysqli_query() expects parameter 1 to be mysqli, string given in
/var/www/php_anazhthshs/euresh_suggrammatwn_aplh.php on line 22
Warning:
mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given
in/var/www/php_anazhthshs/euresh_suggrammatwn_aplh.php on line 24
Can you help? Thanks in advance!
Arguments must be inverted.
mysqli_query($con, $sql);
mysqli_query()

Categories