0

My SQL table is as follows:

$sql=  "CREATE TABLE EMPLOYEE(
        ID INT(3) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
        Name VARCHAR(20) NOT NULL,
        Date DATE,
        Age INT(3) NOT NULL
        )";

I need to find the age by finding difference between the current date and input date. The problem is that when I use the following code:

$sql1="SELECT CURDATE()";
$sql=$conn->query($sql1);     //$conn is DB connection variable
if($sql)
{
  while($row=mysql_fetch_array($sql))
  { 
    echo $row['CURDATE()'] ;
  }
}

It gives this error:

mysql_fetch_array() expects parameter 1 to be resource, object given.

I am fairly new to PHP using DB and need help to get things around. I would be obliged if I can also get help through working code in calculating the age. This refers to querying date from DB, finding difference in years and storing it back in DB in age field.

Please don't close this question as DUPLICATE as I was not able to get my doubts cleared with posts of other users.

Dan Guzman
  • 43,250
  • 3
  • 46
  • 71
  • 2
    Well, of course. You're mixing APIs. Don't use `mysql_*` functions. – aynber Dec 28 '17 at 18:23
  • 1
    **Warning** - This extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information. Alternatives to this function include: – Abdulla Nilam Dec 28 '17 at 18:25
  • 2
    *"Please don't close this question as DUPLICATE as I was not able to get my doubts cleared with posts of other users."* - Huh?? What, you expecting some type of special treatment here? – Funk Forty Niner Dec 28 '17 at 18:34
  • +Funk Forty Niner I meant that I had done my homework and had tried everywhere to look for my problem. It was only after failed attempts that I posted a question here. – SARTHAK MEHRA Dec 28 '17 at 18:38
  • Note: The object-oriented interface to `mysqli` is significantly less verbose, making code easier to read and audit, and is not easily confused with the obsolete `mysql_query` interface. Before you get too invested in the procedural style it’s worth switching over. Example: `$db = new mysqli(…)` and `$db->prepare("…")` The procedural interface is an artifact from the PHP 4 era when `mysqli` API was introduced and should not be used in new code. You wasted a lot of time here confusing the two because of this, avoid it in the future. – tadman Dec 28 '17 at 19:20
  • Note: Try and get out of the habit of declaring SQL statements in throw-away variables that are used only once. It's a lot easier to follow code where the query is supplied directly to the function, and there's no longer a chance of messing up and sending in `$sql3` instead of the visually similar `$sql8`. – tadman Dec 28 '17 at 19:20

1 Answers1

1

If your connection is mysqli, try mysqli_fetch_array instead http://php.net/manual/en/mysqli-result.fetch-array.php

Ctznkane525
  • 7,297
  • 3
  • 16
  • 40