Jump to content

[SOLVED] getting result and updating table


Walker33

Recommended Posts

I'm getting to the final echo statement "should be updated now", which I thought indicated it was successful, but when I go to the table, no row has been updated.  Not sure what I'm doing wrong exactly.

 

<?php
//Grabbing first available trial license
$result = pg_query("SELECT license FROM triallics WHERE inuse = '2000-01-01 00:00:00' limit 1");
$row = pg_fetch_assoc($result);

//Grabbing date and time;
$today1 = date("Y-m-d H:i:s");
echo "".$today1."<br><br>";

//Updating table with today's date
$modresult = pg_query("UPDATE triallics SET inuse = '$today1' WHERE license = '$row' ");
if (!$modresult) {
  echo "Your pg update query isn't right.\n";
  exit;
}
echo "should be updated now";

exit;
?>

 

As always, any help is sincerely appreciated.

 

Link to comment
Share on other sites

That's not right, you're using a resource ($row) as your license?

 

Try this below:

 

 

<?php
//Grabbing first available trial license
$result = pg_query("SELECT license FROM triallics WHERE inuse = '2000-01-01 00:00:00' limit 1");
$row = pg_fetch_assoc($result);
$license = $row['license'];

//Grabbing date and time;
$today1 = date("Y-m-d H:i:s");
echo "".$today1."<br><br>";

//Updating table with today's date
$modresult = pg_query("UPDATE triallics SET inuse = '$today1' WHERE license = '$license' ");
if (!$modresult) {
  echo "Your pg update query isn't right.\n";
  exit;
}
echo "should be updated now";

exit;
?>

Link to comment
Share on other sites

pg_fetch_assoc() returns an array. No value in your database is going to be equal to that array - you need to extract a value from it first.

 

That said, you can probably do this with the one query, if you explain a bit more about what you're trying to do and tell us your database structure.

Link to comment
Share on other sites

Beautiful!  Thankyou, thankyou.  I'm always trying to learn, as well as getting it right, so if I could ask, why did I have to make $row into $license?  I guess I thought my $row was equal to 'license' as I wrote it?  Sorry if that's a dumb question.  I'm pretty new to this.

Link to comment
Share on other sites

Beautiful!  Thankyou, thankyou.  I'm always trying to learn, as well as getting it right, so if I could ask, why did I have to make $row into $license?  I guess I thought my $row was equal to 'license' as I wrote it?  Sorry if that's a dumb question.  I'm pretty new to this.

 

Well as Gingeroot said, you are returning an array so you have to access the element in the array. Eventhough you know you are only returning 1 value, you still have to access it the same way whether it's 1 or 100 values...

Link to comment
Share on other sites

Okay, I understand now.  Thanks for the explanation.  Yes, that was my mistake; I thought extracting only one value eliminated the need to access it as if it were 100.  Gingeroot also said I could do it in one query?  What I'm trying to do:  We have a table for trial licenses, and all are preset to the inuse date of 2001-01-01 00:00:00.  When one is activated, it sets the date to now.  Our trial app checks the date each time the app is opened and if it's more than one week, the trial is expired and they can't use the trial product any longer.  So when someone uses the trial request form I've built, I want them to be issued the next available trial license, and that license's inuse date to change from 2001-01-01 00:00:00 to now. The code now does that, but if there's a way to simplify it, I'm all ears.  Thanks.

Link to comment
Share on other sites

Okay, I understand now.  Thanks for the explanation.  Yes, that was my mistake; I thought extracting only one value eliminated the need to access it as if it were 100.  Gingeroot also said I could do it in one query?  What I'm trying to do:  We have a table for trial licenses, and all are preset to the inuse date of 2001-01-01 00:00:00.  When one is activated, it sets the date to now.  Our trial app checks the date each time the app is opened and if it's more than one week, the trial is expired and they can't use the trial product any longer.  So when someone uses the trial request form I've built, I want them to be issued the next available trial license, and that license's inuse date to change from 2001-01-01 00:00:00 to now. The code now does that, but if there's a way to simplify it, I'm all ears.  Thanks.

 

 

I'm not aware of any "SQL" tricks that allows you to get one value without doing what we just did...so I am very much open to any new ideas....I know I could use it, there have been many times I only wanted one value....I'm sure you can do something fancy with sub-queries though....

 

I don't see any issue with your approach.....

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.