Php, error to loop result of mysqli - php

While Selecting data from the row, i'm getting the error
Notice: Undefined index: password
Here is the Code:
$query = "SELECT username and password FROM `users` WHERE username='$username'";
$result = mysqli_query($con,$query) or die(mysql_error());
$rows = mysqli_num_rows($result);
while ($row = $result->fetch_assoc()){
$hash= $row["password"];
}
Thanks for answering

This Query must returns only one row, you don't need any loop here, if the username is unique. Try to connect to database with OOP(object oriented programming) like this, example:
$mysqli=new mysqli("localhost", "username", "password", "db-name"); mysqli_set_charset($mysqli,'utf8');
$query= $mysqli->query("SELECT username,password FROM `users` WHERE username='$username'");
$row = $query->fetch_assoc();
$hash= $row["password"];

Related

How to change mysql_num_rows etc functions to mysqli? [duplicate]

This question already has answers here:
How to change mysql to mysqli?
(12 answers)
Closed 4 years ago.
I am facing an error as I'm about to launch my PHP files to a free web hosting site. The error showing up is given below:
And below is the code for my project.
$sql= "SELECT * FROM user WHERE staff_id='$staff_id' AND password='$password'";
$query = mysql_query($sql) or die("Error: " . mysql_error()); //this is error on line 42
$row = mysql_num_rows($query);
I'm not sure what the errors are as I am self-taught on PHP. hopefully you guys can point out what change i should make. Thanks in advance!
First, as you suggest in the title, use mysqli for security reasons, or even better, PDO.
With mysqli: (updated)
$stmt = $conn->prepare("SELECT COUNT(*) FROM user WHERE staff_id = :staff_id AND password = :password");
$res = $stmt->execute(["staff_id" => $staff_id, "password" => $password);
$row = mysqli_num_rows($res);
With PDO:
$stmt = $conn->prepare("SELECT COUNT(*) FROM user WHERE staff_id = :staff_id AND password = :password");
$res = $stmt->execute(["staff_id" => $staff_id, "password" => $password);
$row = $res->fetchColumn();
$conn being your database link. The PDO version assumes you don't need the rows but just the count. In case someone tells you to, don't use rowCount on SELECT query, that's not reliable.
$sql= "SELECT * FROM user WHERE staff_id='$staff_id' AND password='$password'";
$query = mysql_query($sql) or die("Error: " . mysql_error()); //this is error on line 42
$row = mysql_num_rows($query);
try this one
$sql= "SELECT * FROM user WHERE staff_id='$staff_id' AND password='$password'";
$query = mysqli_query($con, $sql) or die("Error: " . mysqli_error($con)); // $con is the connection to database like // $con = mysqli_connect("localhost","my_user","my_password","my_db");
$row = mysqli_num_rows($query);

Unable to post to a database MySQL

I am attempting to post a column into my database here as a test and I am unable to do so. I've used the code below and it doesn't seem to be posting. Unless I am missing a trick with PHPmyAdmin I cannot seem to get it working. Any chance anyone could help? Thanks in advance!
<?php
$link = mysqli_connect("XXXX", "XXXX",
"XXXX", "XXXX");
if (mysqli_connect_error ()) {
die("The connection has failed");
}
$query = "INSERT INTO `users` (`email`, `password`)
VALUES('owen#owen.com', 'hfudhf8ahdfufh')";
mysqli_query($link, $query);
$query = "SELECT * FROM users";
if($result = mysqli_query($link, $query)) {
$row = mysqli_fetch_array($result);
echo"Your Email is ".$row["email"];
echo" and your Password is ".$row["password"];
}
?>
The problem is that you're only fetching one row of results. Unless the table was empty before you ran the script, there's no reason to expect that row to be the one that you just added.
If the table has an auto-increment ID field, you can fetch that row:
$query = "SELECT * FROM users WHERE id = LAST_INSERT_ID()";

mysqli_query works in phpmyadmin but not in php

I have looked for an answer for ages now, lots of similar questions but found no solutions yet...
Anyway,
all I am trying to do is get the id of a user from the database using a mysqli_query, the query seems to work when I use it in phpmyadmin but doesn't when I use it as part of a php script.
$username = "bob";
$db = mysqli_connect("localhost", "username", "password", "user_data");
$sql1 = "select id from user_information where username='$username'";
$id = mysqli_query($db, $sql1) or die(mysql_error());
echo $id;
The database connection works fine, I am able to input data through php.
Any suggestions? (Anyone's help is greatly appreciated).
you can't print the result from mysqli_query, it is mysqli_resource and for dumping the error you need to change mysql_error() to mysqli_error()
$username = "bob";
$db = mysqli_connect("localhost", "username", "password", "user_data");
$sql1 = "select id from user_information where username='$username'";
$result = mysqli_query($db, $sql1) or die(mysqli_error());
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
echo $row['id'].'<br>';
}

Displaying Database Record MySQLI

I am trying to display a record from my database, however the page appears blank and doesn't display the data I am expecting. The code follows below:
<?php
$mysqli = new mysqli(localhost, root, USERPASS, DBNAME);
$query = "SELECT * FROM usertable WHERE userID= '" . $_SESSION["sess_uid"] . "'";
$result = mysqli_query($mysqli, $query);
$row = mysqli_fetch_row($result);
echo $row['userQuestion'];
?>
Any help would be appreciated.
Thanks
<?php
// there need to be strings arguments here
$mysqli = new mysqli('localhost', 'root', USERPASS, DBNAME);
// sql injection friendly query
$query = "SELECT * FROM `usertable`
WHERE `userID`='{$_SESSION["sess_uid"]}' LIMIT 1;";
// do we have a result
if($result = mysqli_query($mysqli, $query)){
// fetch a single row
if($row = mysqli_fetch_row($result)){
// print the record
var_dump($row);
}
}
?>
You need to wrap 'localhost' and 'root' as strings.
mysqli_fetch_row returns a numerical array.
You can print the content of the record using var_dump or use mysqli_fetch_assoc instead.

Login Page is not working.

I am having trouble on getting the MySQL column. EVERYTHING in mysql is set with the username, password, database, table, and the column.
Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given
in /Applications/XAMPP/xamppfiles/htdocs/socialhut/login.php on line 8
Here's the code for login.php:
<?php
$username = $_POST['username'];
$password = $_POST['password'];
$conn = mysqli_connect("localhost","root","","data");
$sql = "SELECT * FROM userdata WHERE username='$username' and password='$password'";
$query = mysql_query($sql);
$result = mysql_num_rows($query);
if ($result==1){
session_register($username);
session_register($password);
header('location:members.php');
}else{
mysql_error();
}
?>
Can anyone figure it out?
Thanks!
You're mixing mysqli and mysql calls in the same code. You can't do that.
Try this:
$conn = mysqli_connect("localhost","root","","data");
$sql = "SELECT * FROM userdata WHERE username='$username' and password='$password'";
$query = mysqli_query($conn, $sql);
if ($query === false) {die(mysqli_error($conn));}
$result = mysqli_num_rows($query);

Categories