ERROR: mysqli_num_rows() expects parameter 1 to be mysqli_result - php

Im very new to php and the mysql side of things and i was hoping someone could help me out, My code is returning the error: mysqli_num_rows() expects parameter 1 to be mysqli_result
Here is my code
$username = $_POST['username'];
$password = $_POST['password'];
if(isset($username) && isset($password)) {
$connect = mysqli_connect("localhost","root","") or die("Couldnt connect!");
mysqli_select_db($connect,"phplogin") or die("Couldnt find DB.");
$query = "SELECT * FROM users WHERE username={$username}";
$numrows = mysqli_num_rows($query);
echo $numrows;
If someone could help me out and tell me what i done wrong and how i could echo out the number of rows i would glady appreciate it.

Function mysqli_num_rows expects result, you gave it string.
Before num_rows you have to call $result = mysqli_query($db_connect, $query). Then you can count records using mysqli_num_rows($result);.

You have to actually run the query first before you can see how many rows you have:
$query = "SELECT * FROM users WHERE username={$username}";
// Run the query
$result = mysqli_query($connect, $query);
$numrows = 0;
if(!$result)
{
$numrows = mysqli_num_rows($result);
}
Remember to look up on the php.net website if you get stuck, it's a goldmine of help: http://www.php.net//manual/en/mysqli-result.num-rows.php

Related

Trouble querying database with php*fixed*

I am having trouble selecting my data from a database and displaying it. I have looked at tutorials and i still get the same error. Some help would be appreciated. The error i am getting is couldnt fetch result.
$sql = "SELECT * FROM data";
$result = mysql_query($sql) or die("couldnt fetch result");
if($result > 0){
while ($rows = mysql_fetch_array($result)){
$username = $rows['username'];
echo $username;
}
}
Just do that (assuming got it right connecting to DB, first thing to check !)
$sql = "SELECT * FROM `data`"; // data is a reserved keyword, protect it !!!
$result = mysql_query($sql) or die("couldnt fetch result"); // potentially diying here
if($result){
while ($row = mysql_fetch_assoc($result)){
$username = $row['username'];
echo $username;
}
}
If what you're getting is literally 'couldnt fetch result' it means your mysql_query() fails, and die statement takes over. Check your database connection.
I think the very simple problem is that you check if the $result is greater then 0. But you get an resource.
$conn = mysql_connect.......
$sql = "SELECT * FROM data";
$result = mysql_query($sql) or die("couldnt fetch result");
if($result){
while ($rows = mysql_fetch_array($result)){
$username = $rows['username'];
echo $username;
}
}
And if you see your die statement you have an error in your SQL Syntax. Its very short but its possible that your table doesn't exist in that database you're trying to connect. I hope you have a connect before and its not your complete code.
You use the old mysql functions. Its better to use MySQLi or PDO.
And DATA is a reserved keyword its possible that you get problems if you use it in your query. Rename your table in prefix_data for example.
https://dev.mysql.com/doc/refman/5.7/en/keywords.html

mysql_num_rows() expects parameter 1 to be resource, string given in [duplicate]

This question already has answers here:
mysql_num_rows giving error "mysql_num_rows() expects parameter 1 to be resource"
(3 answers)
Closed 5 months ago.
I have read through many other threads about this exact problem, but i for some reason can not solve my problem. Really need some help.
if (!$username||!$password||!$email)
echo "Please fill out all fields";
else
{
//encrypt password
$password = md5($password);
//check if username already taken
$check = mysql_query("SELECT * FROM Test WHERE username = '$username'");
if (mysql_num_rows($check)>=1)
echo "Username already taken";
else
It said
Warning: mysql_num_rows() expects parameter 1 to be resource, string
given in /Users.....
if (mysql_num_rows($check)>=1) This line..but when i run it in phpmyadmin, it returns results to me ok.
Please help
Try to like this:
$query = "SELECT username FROM $this->table WHERE username='$sUser'";
$result = mysql_query($query);
$num_rows = mysql_num_rows($result);
Change:
$check = mysql_query("SELECT * FROM Test WHERE username = '$username'");
to
$check = mysql_query("SELECT * FROM Test WHERE username = '$username'") or die(mysql_error());
And you will see any potential errors that happens in the query.
Also, I would highly recomend using either PDO or Mysqli instead of mysql functions, as they are deprecated and will be removed in future php versions.
First You make sure that connection established correctly to the database.
Instead of writing query directly in
$check = mysql_query("SELECT * FROM Test WHERE username = '$username'");
Store the query in variable as
$query = "SELECT * FROM Test WHERE username = '".$username."'";
and execute it as
$check = mysql_query($query);
if you are still facing the issue,
echo the query as
echo $query;
and execute the query directly in phpmyadmin and you can find the issue.
I hope this helps.
You can try like this
$sql= "SELECT * FROM Test WHERE username = '".$username."'";
$check = mysql_query($sql);
I think your $check returns null that is no user with this username and null can't be a parameter mysql_num_rows() function.
if($check)
{
echo "Username already taken";
}
else
{
echo "Username available";
// do other actions
}
Ok, if anyone having the same issue, just add the variable within the if() statement two times, like so:
$Query = mysql_query($sql);
IF($Query && mysql_num_rows($Query)> 0){ // continue with the code}
This should fix the issue.

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

MySQLi Query will not execute

I am playing around with this trying to pull some articles from a database.
Here is the code i have so far: ($host etc are defined in config)
include ("admin/config.php");
$mysqli = mysqli_connect($host, $user , $pass , $database);
$query = "SELECT * FROM articles ORDER BY date LIMIT 5";
$result = mysqli_query($mysqli,$query);
while($row = mysqli_fetch_array($result))
{
Some code to display results
}
This is the error that gets generated but i just cant see what i am doing wrong. Any help will be much appreciated.
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in /website....
there is error in your query.
to get it always run all your queries this way
$result = mysqli_query($mysqli,$query) or trigger_error($mysqli->error."[$query]");

PHP Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource [duplicate]

This question already has answers here:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource [duplicate]
(3 answers)
Closed 9 years ago.
<?php
session_start();
$user = $_SESSION['login'];
$mysql_connect = mysql_connect("localhost", "root", "");
$mysql_select_db = mysql_select_db("site");
$sql = "SELECT * FROM `msg_inbox` WHERE `to` = '$user'";
$result = mysql_query($sql) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
$mysql_connect = mysql_connect("localhost", "root", "");
$mysql_select_db = mysql_select_db("site");
$query = ("UPDATE msg_inbox SET unread = 0 WHERE id= ".$row['id']);
$result = mysql_query($query);
}
And I receive error: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in on line 8
Can you help me?
You have reassigned $result in your loop. After the first iteration, the variable $result will hold a boolean indicating the success or failure of your UPDATE statement.
Change:
$result = mysql_query($query);
To:
$result2 = mysql_query($query);
Or just:
mysql_query($query);
And be careful of SQL injection holes.
EDIT actually, your whole code could and should be shortened to:
<?php
session_start();
$user = $_SESSION['login'];
// A blank password for root? Really?
$mysql_connect = mysql_connect("localhost", "root", "");
$mysql_select_db = mysql_select_db("site");
$sql = "
UPDATE `msg_inbox`
SET `unread` = 0
WHERE `to` = '".mysql_real_escape_string($user)."'
";
// Don't show the result of mysql_error() in a production environment!
$result = mysql_query($sql) or die(mysql_error());
You inner query is overwriting $result, causing the outer loop to fail.
As well, you're opening a new connection inside the loop. This is highly inefficient and wasteful. Let's say your main query finds 500 rows. That means you're opening 500 connectsion to mysql. This is utterly pointless.

Categories