PHP function select 2 values

Asked

Viewed 117 times

4

Good!

I am creating functions in PHP in order to return the difference between the date TIMESTAMP of the database and today’s date, and for that I created two functions:

function getRegistos()
{
    $query = "SELECT *, DATEDIFF(NOW(),dataDeEntrada) as diferenca FROM `equipamentos` WHERE 1";
    $this->connect();
    $res = $this->execute($query);
    $this->disconnect();
    return $res;
}

function getData()
{
    $query = "SELECT (dataDeEntrada2), DATEDIFF(NOW(),dataDeEntrada2) as diferencaTotais FROM `equipamentos` WHERE 1";
    $this->connect();
    $res = $this->execute($query);
    $this->disconnect();
    return $res;
}

To getRegistos() serves to retrieve the records and the days elapsed and the getData() is only used to get the total days since the product was registered ( because when the administrator updates a product to the outdated column it returns to 0 ).

I don’t know if it’s possible to put the two together or call them differently...

include_once('DataAccess.php');
            $da = new DataAccess();
            $res = $da->getRegistos();
            $res1 = $da->getData();
            while($row = mysqli_fetch_object($res)){ ... }

But only back to me getRegistos(). I also tried to do another while but this time with $res1 which is the value returned by getData() but nothing done. I’m new to PHP and I believe I’m just complicating.

  • 1

    I think you’re complicating things. See: http://sqlfiddle.com/#! 9/a677a/1

  • The link you sent doesn’t work. In the Equipment table I have the following columns: ID, id_user, name, type, status, dateDentry, dateDentry 2, brand, model, symptom, budget

  • It works, just wait a little. At least for me it works.

  • OK today I managed to open. That’s what I did. But I wanted to keep 2 differences. One for the days elapsed and another for the Total days because one will be reset and the other is not why I have 2 datesEntrate in the database.

  • I get it, on Monday you can use SELECT SUM(DATEDIFF(NOW(), d2)) AS TOTAL FROM t; According to my SQLFIDDLE.

  • Exactly, but that would mean creating another function.

  • But that’s what I’ve done. Now the problem is to call both. As you can see I used a while to show all the equipment table information. Now I need another just to show the total days.

  • I understand, sorry for the misunderstanding. But call the Getregistros and do not Call the Getdata?

Show 3 more comments

1 answer

2


Solved

The only thing I’ve changed is calling in functions.

'Cause I used to call them that:

$id = $_SESSION['id'];
include_once('DataAccess.php');
$da = new DataAccess();
$res = $da->getEquipamento($id);
while($row = mysqli_fetch_object($res)){ ... }

And all that was missing was this:

$res1 = $da->getData();
$row1 = mysqli_fetch_object($res1);

I mean, it stayed that way:

$id = $_SESSION['id'];
include_once('DataAccess.php');
$da = new DataAccess();
$res = $da->getEquipamento($id);
$res1 = $da->getData();
while($row = mysqli_fetch_object($res)){
$row1 = mysqli_fetch_object($res1);
... 
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.