get mysql count of id in shell script
Your command:
count=$(mysql -uroot -proot csv_imports -e "select count(user_Id) from test_data where user_Id=\"12345\";")
Will probably fetch something like:
+---------------+
| count(userid) |
+---------------+
| 5 |
+---------------+
Because that is the default output of a mysql
query.
To suppress the header and column names, you should include the options -s
(silent) and -N
(skip column names)
That way, the mysql
command only returns the 5
(based on my output) which will be stored in the variable using:
count=$(mysql -s -N -uroot -proot csv_imports -e "select count(user_Id) from test_data where user_Id=\"12345\";")
Try to write the value of your count
variable in a terminal using:
echo "$count"
If it only returns a 5
(again, based on my output), you can use it as a numeric value in test expressions and calculations.
Related videos on Youtube
GlenR
Updated on September 18, 2022Comments
-
GlenR over 1 year
Whats wrong with this query? For example
$brand
holds the valueapple
Then I get the errorunknown column 'apple'
Can anyone see the problem?
if(isset($_GET['brand']) ? $_GET['brand'] : 0) { $brand = (isset($_GET['brand']) ? $_GET['brand'] : 0); $sql = mysqli_query($link, "SELECT COUNT(id) FROM products WHERE brand = $brand AND status = 1 ORDER BY id DESC") OR die(mysqli_error($link));
Edit: With error checking
error_reporting(E_ALL); ini_set('display_errors', 1); mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
I get this error 'Fatal error: Uncaught exception 'mysqli_sql_exception' with message 'Unknown column 'apple' in 'where clause'''
-
Funk Forty Niner almost 10 yearsdo
WHERE brand = '$brand'
orWHERE brand = '".$brand."'
and make sure you have a column called "brand" -
Marc B almost 10 yearsYou are vulnerable to sql injection attacks, and need to learn basic SQL syntax as well.
-
GlenR almost 10 yearsi still get the error
-
Ali Gajani almost 10 years"' .$brand. '" Use concatenation
-
Funk Forty Niner almost 10 yearsWell then, you don't have a column named "apple" (or brand), do you? Make sure you don't have a space in your column name too.
-
Funk Forty Niner almost 10 yearsPlus, you're not looking for a column named that, you're looking for a value called "apple" that's inside the "brand" column.
-
GlenR almost 10 yearsI have a column named 'brand' the value 'apple' is in the variable $brand and is a value in the 'brand' column. I have edited my question with the error given by the error reporting you asked.........
-
the_velour_fog over 8 years
count()
tells you how many records match your query - if your tableuser_Id
only has one record whereuser_Id=12345
count will only return1
-
Asmita over 8 yearsI am ok with it. But currently nothing is happening. I at least want numeric result 1
-
Asmita over 8 yearsany another solution to find presence of that user_Id?
-
-
Funk Forty Niner almost 10 yearsAdd error reporting to the top of your file(s)
error_reporting(E_ALL); ini_set('display_errors', 1); mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
-
GlenR almost 10 yearsStill the same. I have edited my question with some error reporting if that makes anything any clearer.............