PHP num_rows work but shows error [duplicate] - php

This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 6 years ago.
I have users who add rows into db. Right above their picture I show the count of the rows they added. Pretty simple and it works fine. The problem is that I also get error which doesn't make sense:
mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in...
while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
$result = mysqli_query($con,"SELECT * FROM my_table WHERE user_ids = ".
$row['user_id']."");
$reput = mysqli_num_rows($result);
}
$query works fine too, any idea how to satisfy the error?

You should use single quotes when calling something that isn't a boolean
WHERE user_ids = '".$row['user_id']."'"

Related

mysqli_fetch_all() not working when table name is a variable [duplicate]

This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 3 years ago.
I am trying to get an array from a database. I got the table name from a previous PHP file and I am trying to return the array. However, it only responds with an error.
If I replace the $db_name in the query with the name as a string it works fine but that is not what I want. I don't know why it is not working. Is it just not liking the query? It only does not work when I put in the table name as a variable.
$db_name = $_SESSION['databaseMenu'];
echo $db_name;
$sql="SELECT feed FROM '".$db_name."' ";
$result=mysqli_query($con,$sql);
// Fetch all
$outp = mysqli_fetch_all($result,MYSQLI_ASSOC);
$arra = array_values($outp);
return $arra;
Like I said earlier, when the query is a simple text it returns the array however when I put in the variable it responds with the error:
'''
Warning: mysqli_fetch_all() expects parameter 1 to be mysqli_result, boolean given in
'''
Any advice is appreciated.
Use
$sql="SELECT feed FROM ".$db_name." ";
Instead of
$sql="SELECT feed FROM '".$db_name."' ";

PHP MySQL not adding to database [duplicate]

This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(1 answer)
Reference - What does this error mean in PHP?
(38 answers)
Closed 5 years ago.
Trying to do a school project, essentially need to take the choice of animal from a dropdown menu, and add the ID of that animal to the order table of the database. dropdown is on a seperate page which works fine, and posts result to this page.
the code:
<?php
include_once("connect-db.php");
if(isset($_POST['Submit'])) {
$choice = $_POST['choice'];
$result = mysqli_query($mysqli, "SELECT * FROM animals WHERE AnimalSpecies=$choice");
while($res = mysqli_fetch_array($result))
{
$id = $res['AnimalID'];
}
$query = mysqli_query($mysqli, "INSERT INTO order('AnimalID') VALUES('$id')");
}
The connectdb file is fine, i have used it in another page. additionally, $choice is working fine, i had it echo manually and it shows the right value. I dont get any error message, it just doesnt add anything to the order table.

what is the error [duplicate]

This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
Closed 6 years ago.
what is error in this code output is
Warning: mysql_result() expects parameter 1 to be resource, boolean given in C:\xamp\htdocs\LMS\LMS\all_books.php on line 60
<?php
$sql1=mysql_query("SELECT COUNT(*) FROM book_list where book_id=$id AND status=0 AND item_type=0 AND condition='new'");
$count1 = mysql_result($sql1, 0, 0);
echo $count1;
?>
The function mysql_result() expects a resource (a mysql result-set) as its first parameter but has instead been supplied with a boolean (true/false) value.
This is likely because your SQL query is malformed (condition is a reserved keyword); You ought to enclose your table and column names in backticks, for example:
SELECT COUNT(*) FROM `book_list` WHERE `book_id`=$id AND `status`=0 AND `item_type`=0 AND `condition`='new'

PDO "SELECT" not returning result [duplicate]

This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Using LIKE in bindParam for a MySQL PDO Query [duplicate]
(2 answers)
Closed 7 years ago.
I'm trying to get PDO to return the results of a wildcard search. My code is:
$search = "%Notes%";
$result = $db->prepare("SELECT * FROM books WHERE 'name' LIKE :search");
$result->bindParam(':search', $search);
$result->execute();
while($arr = $result->fetch(PDO::FETCH_ASSOC)){
echo $arr['name'];
}
At the moment, I get a blank screen. If I run the sequel through PHPMyAdmin:
SELECT * FROM books WHERE name LIKE '%Notes%'
I get the appropriate result.
I assume it's something to do with the way I am formatting my PDO statement, I know you can't have a dynamic column name but I don't see what is going wrong?
in your query you have 'name' change that to just backticks instead of quotes
aka
$result = $db->prepare("SELECT * FROM `books` WHERE `name` LIKE :search");
you can also just remove the backticks

MySQL query Error with PHP [duplicate]

This question already has answers here:
MySQL & PHP Parameter 1 as Resource
(3 answers)
Closed 9 years ago.
I'm trying to do this:
<?php
$query ="SELECT * FROM servers, bans WHERE servers.ServerID = bans.ServerID ORDER BY BanID DESC";
$result = mysql_query($query);
while($row=mysql_fetch_assoc($result)){
?>
but I get this error:
Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\Access\repository\HT2\WH\www.voltzgaming.com\public_html\GBans\index.php on line 139
Your mysql_query($query) is returning a false because you have a syntax error in the SQL.
Use mysql_error() after the query is run to see what the problem is.

Categories