get mysql count of id in shell script

55

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.

Share:
55

Related videos on Youtube

GlenR
Author by

GlenR

Updated on September 18, 2022

Comments

  • GlenR
    GlenR over 1 year

    Whats wrong with this query? For example $brand holds the value apple Then I get the error unknown 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
      Funk Forty Niner almost 10 years
      do WHERE brand = '$brand' or WHERE brand = '".$brand."' and make sure you have a column called "brand"
    • Marc B
      Marc B almost 10 years
      You are vulnerable to sql injection attacks, and need to learn basic SQL syntax as well.
    • GlenR
      GlenR almost 10 years
      i still get the error
    • Ali Gajani
      Ali Gajani almost 10 years
      "' .$brand. '" Use concatenation
    • Funk Forty Niner
      Funk Forty Niner almost 10 years
      Well 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
      Funk Forty Niner almost 10 years
      Plus, you're not looking for a column named that, you're looking for a value called "apple" that's inside the "brand" column.
    • GlenR
      GlenR almost 10 years
      I 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
      the_velour_fog over 8 years
      count() tells you how many records match your query - if your table user_Id only has one record where user_Id=12345 count will only return 1
    • Asmita
      Asmita over 8 years
      I am ok with it. But currently nothing is happening. I at least want numeric result 1
    • Asmita
      Asmita over 8 years
      any another solution to find presence of that user_Id?
  • Funk Forty Niner
    Funk Forty Niner almost 10 years
    Add 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
    GlenR almost 10 years
    Still the same. I have edited my question with some error reporting if that makes anything any clearer.............