1

I want get the value from database, because I need that value to sum it with other value and I cant take it. Here is an example of what I need:

user_id    task_progress
1              2
3              4
5              6

I used

$check = mysql_query("SELECT task_progress FROM dotp_tasks WHERE (user_id = '3')"); 
$result = mysql_fetch_array($check);

And it should echo 4 but it echo Array. What am I doing wrong? And after that can I normally make this

$new=$value + $result;
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
TorresAlGrande
  • 213
  • 1
  • 14
  • start by removing the brackets for `WHERE (user_id = '3')` that's only used for subqueries. https://dev.mysql.com/doc/refman/5.0/en/subqueries.html – Funk Forty Niner Jun 26 '15 at 14:00
  • why not make use of mysql's aggregate functions http://www.mysqltutorial.org/mysql-aggregate-functions.aspx ? – Funk Forty Niner Jun 26 '15 at 14:00
  • also you should be using mysqli or PDO for your database stuff. mysql_ functions are old and deprecated since PHP 5.5. – Alex Jun 26 '15 at 14:01
  • 2
    If you can, you should [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) [statements](http://php.net/manual/en/pdo.prepared-statements.php) instead, and consider using PDO, [it's really not hard](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Jun 26 '15 at 14:01
  • msql_fetch_array returns an array. Try echo $result[0]; – BigScar Jun 26 '15 at 14:04

2 Answers2

4

If you change your code to the following this should be what your looking for:

$check = mysql_query("SELECT task_progress FROM dotp_tasks WHERE user_id = '3'");
$result = mysql_fetch_array($check);

$new=$value + $result[0];

By accessing the array first key you should get the result you want.

1

$result = mysql_fetch_array($check) is an array and you cannot echo an array. As an array you can get the element by using [Number_of_the_col] or ['Name_of_the_col']

In you case use : echo $result[0]; // echo the first element

Hearner
  • 2,711
  • 3
  • 17
  • 34