Showing me a syntax error while I am trying to using the SELECT COUNT(*) function to count table rows with conditions?

AminulHub

I am using this code>>>

$recipe_counting    =   $wpdb->get_var( 

            "SELECT COUNT(*)
            FROM '" . $wpdb->prefix . "recipe_ratings'
            WHERE recipe_id ='" . $post_ID . "' AND user_ip='" . $user_IP . "'"

         );

In browser developer tools "Network" tab, it's showing this error when I try to check my output.

<div id="error"><p class="wpdberror"><strong>WordPress database error:</strong> [You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near &#039;&#039;pr4_recipe_ratings&#039;
            where recipe_id =&#039;104&#039; and user_ip=&#039;::1&#039;&#039; at line 2]<br /><code>SELECT COUNT(*)
            from &#039;pr4_recipe_ratings&#039;
            where recipe_id =&#039;104&#039; and user_ip=&#039;::1&#039;</code></p></div>{"status":2}
MichelR

You might try to remove the ' that encloses your tablename since the DB engine couldunderstand that you make the SELECT against a string.

The mariadb documentation for the SELECT statement (https://mariadb.com/kb/en/select/) mentions to use the backstick caracter ` to quote tablenames, not single quote.

You can quote column names using backticks. If you are qualifying column >names with table names, quote each part separately as >`tbl_name`.`col_name`.

Your query becomes the following SELECT COUNT(*) from pr4_recipe_ratings where recipe_id ='104' and user_ip='::1'

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Using the count function on third table in two table select statement in MariaDB

Error while trying to delete rows from table

MYSQL - Select rows fulfilling many count conditions

How to select /count rows in a column based on multiple conditions

"Syntax error at or near ' , '" while trying to SELECT INTO

SELECT COUNT(*) based on joined table conditions

I need to select a data that doesn't exist on a table as zero using the COUNT() function, how do I do so using Sql

While trying to retrieve the AccesToken using HttpClient I am getting an error

I am trying to get rid of rows using multiple values, but I am getting an error

Count the duplicate rows in excel using python and i am getting error TypeError: a bytes-like object is required, not 'str'

I am getting "Object is possibly null" error when trying to return the count of characters and numbers from a string

I am trying to use SQL to count the number of rows for each date in my database

How to select count from Table A with conditions from Table B

Trying to count and return a number of rows in a table

How to select table rows count in LINQ?

php syntax error while am trying to add foreach inside function

PL/pgSQL function to count rows in a table

Using COUNT Function in Table Joins

Selenium Python I am trying to iterate rows in a html table I get error Index out of range

Count Table Rows in a nested select statement

syntax error while trying to INSERT INTO table with postgres

Why am I getting syntax error while trying to update database?

How can I select multiple rows with the count of other rows in the same table that has their primary keys in a separate column?

i am trying to use count() function in laravel but i am running in to this error syntax error, unexpected 'count' (T_STRING)

I am trying to increment count value in DB when the user click on article using laravel

Why am I getting a syntax error when trying to create a table?

i am trying to sort a vector of certain user defined data type but it is giving me syntax error c++

I am trying to do categorical analysis I want to convert count values to percentage and I am getting an error

I am getting syntax error while using foreach in javascipt. I am trying to create a function to increase and decrease the text size on my page

TOP Ranking

HotTag

Archive