Jump to content


  • Posts

  • Joined

  • Last visited

Recent Profile Visitors

The recent visitors block is disabled and is not being shown to other users.

ChenXiu's Achievements

Advanced Member

Advanced Member (4/5)




Community Answers

  1. Oooooh! That is fantastic! Like I've said lots before, I wish I had learned PHP from the ground up. This sounds like something every brand new PHP person already knows. So, yes, a "do loop" is exactly what I want. Tangentially, when I was "borrowing code" from an example I read somewhere for something else, they had a "try/catch" thing going on. I immediately got rid of the "try" and "catch" thinking "I don't need those stupid things whatever they are..." So, after I get comfortable using "do loops" I should brush up on what try/catch means and does 😃 As well as what 'break' 'continue' and 'next' actually mean (in my code above, using "break" was just a lucky guess 😊 ) p.s. I should have mentioned before... your use of time() in the code is fantastic -- that never would have occured to me. I thought a $counter + sleep() was the only way to do this sort of thing. Right now I can already think of a dozen places where I can use time() in a loop.
  2. Thank you! I've never used a "do" loop before; this will be my first one. Tomorrow morning I'm setting aside time to learn about them, pros and cons, and when to use. It looks elegant!
  3. No, you misunderstood. The 3rd party data provider can take up to one minute depending on how many customers are simultaneously using their service. The 3rd party provider produces results in about 1/4 second if 10 people are using their service. If 200 people are using their service, you're in a queue. When they provide the data is when they provide the data. Is my mySQL code okay?
  4. Data is supposed to be instantly inserted into mySQL table from a 3rd party. However, sometimes it takes up to a minute or so.... Is this "keep checking for up to a minute" loop okay? If not, how can I make it better? $count = 0; $query = $db->query("select data from mytable where sku = 'XYZ123' limit 1")->fetch_assoc(); // if no instant result, keep trying for up to one minute: while (!$query) { $query = $db->query("select data from mytable where sku = 'XYZ123' limit 1")->fetch_assoc(); sleep(1); // wait for one second before trying again $count++; if($count > 60) { break; } // Give up after one minute. } if (!$query) { echo 'Forget it, no data will be provided!'; // Give up. } else { echo 'Success! Data is ' . $query["data"]; // Success! } Thank you.
  5. That sounds like a great idea, thank you! I'll search for one now.
  6. p.s. in my real scripts, I have: $result = $db->query($query); while ($row = $result->fetch_assoc()) { ... } (otherwise none of my mySQL queries would work) But when I type my question here, I goofed it up. I'm noticing the following: I have at least 20 new coding ideas every single day. Whenever I try them out, they *always* generate some sort of an error. ... and then I have to go fix the error. Each time. Looking back this entire year, I think every single block of code I've written has generated some sort of an error which I have to go fix. ... hopefully this gets better 😃 I feel like a first grader in english class.... "The dog jumped over the fense." - oops.... "fence" "The three bares liked porridge" - oops... "bears" while($row = $result = fetch_assoc()....
  7. Thank you! Yes you are correct. I botched that one. And this is super basic stuff I should already know. Sometimes I wish I was in an actual (physical, not online) PHP classroom, and was made to learn PHP from the ground up, starting all the way from the beginning. There are so many rock-bottom absolute-basic things I have no clue about. One day, hopefully 😃
  8. Ha! You're onto me! No one liners. 😃 Something like this? $listings = ''; $query = " select item from table order by date asc limit 10 "; while($row = $db->query($query)->fetch_assoc()) { if(!isset($firstrow)) { $firstrow = $row["item"]; $listings = "The first row is $firstrow<br><br>"; } $listings .= $row["item"].'<br>'; } echo $listings;
  9. This produces 10 results: $query = " select item from table order by date asc limit 10 "; while($row = $db->query($query)->fetch_assoc()) { echo $row["item"]; } Can PHP extract the first of the 10 results without running a duplicate query with "limit 1" in it? (I already tried things like echo $row[0] and echo $row[1] and print_r($row)). Thank you.
  10. Thank you! That's perfect! I don't think I would have figured that one out... Whenever extracting values requires looping through an array using its own keys (e.g. "$array[$key_from_line_above]"), it always throws me for a loop (pardon the pun 😃 ).
  11. $matches is an array as follows: Array ( [0] => Array ( [0] => sku234567 the price equals $33.00 [1] => sku999888 the price equals $82.44 ) [1] => Array ( [0] => 234567 [1] => 999888 ) [2] => Array ( [0] => $33.00 [1] => $82.44 ) ) Using PHP, how do I end up with this: 234567 is $33.00 999888 is $82.44 I have tried just about everything, like echo "$matches[0] is $matches[2]" and $matches[1][1][2] and foreach loops..... I know I can access $matches[1] using a foreach loop, but then how do I access its corresponding value from $matches[2] ? Thank you.
  12. There is no loop, I just have three lines of code that do this: file_get_contents("php://input") ---> Sanitize ---> then insert into mySQL database ---> exit. My computer browser has an html/mysql page that queries the database for the results I'm waiting for. I refresh the browser while patiently waiting. There are 100s of records -- whenever I query 3rd party server and they post results to my endpoint, my mySQL database increases in size. At first, that's what I had done, but this browser page is the same one I use to query the 3rd party for new data. If this browser page is auto refreshing to display the results previous request, I can't be using this browser page to be posting my next request. The more I think about this, the more the logic of it all fades away... 1.) There are 100's of records that are slowly building to the 1000's of records. I only want my most recent request showing up. 2.) How can I continue using the page to post data to query the 3rd party if all of sudden my previous request results commandeers the page... My computer monitor is only so big.... I already have 4 windows open to do what I need to do. It sounds like I'm going to need a 5th browser window open that will say "ChenXiu, Your Results Are Ready." I wish I could think out of the box on this one... I'm sure there is an elegant way of accomplishing this.
  13. 1.) My query script requests data from 3rd party. 2.) The 3rd party processes request, and about a minute or so later, pushes results to my server. 3.) My PHP code uses file_get_contents("php://input") to capture and insert that data into my mySQL table. 4.) I sit at my computer, waiting up to 2 minutes, constantly refreshing my browser's mySQL page to see if mySQL has results yet. To avoid having to sit at my computer refreshing my page like a monkey pressing the "give me a banana" button, I have a question. Question: Without having to learn an install one of today's complex Python/Java/Javascript technologies, is there a simple way to have my browser simply display that data when it's ready? With my limited knowledge, I'm experimenting with SSE, but it looks like SSE polls actual files, for data that's been written. Somehow there might be a way using ajax to have a 2nd page with an infinite loop where mySQL is queried every 5 seconds, and when $db->query is "TRUE" then it will a.) writes the data to a file using file_put_contents and b.) break out of the mySQL loop. And when the file gets written using file_put_contents, SSE will be polling that file and delivering the results. Am I going about this wrong? Currently I am at the "thinking up an idea how to do this" stage. And before I spend 6 weeks trying to write the code, I thought I'd check to see if there isn't already a "one-liner" that accomplishes all of this 😃
  14. When logged into my shared server, I usually see only my own username logged in when I type the SSH command "> who" For example, I normally see: > who; ChenXiu pts/1 2021-09-02 09:55 (ip addr) However, I sometimes other users, like this: > who; ChenXiu pts/1 2021-09-02 09:55 (ip addr) AnotherName pts/2 2021-09-02 09:55 (ip addr) SomeoneElse pts/2 2021-09-02 09:55 (ip addr) I'm assuming this is normal? I tested this on other shared server hostings that I use (using commands like "who" and " last -i " Should this be alarming? Thank you.
  15. Thank you, this will help! The main problem I have is I have a javascript window.open -- the window opens and it flashes the error for a nanosecond and then reloads with correct html data. So I need to figure out what that error message was that only flashed for a nanosecond. Regarding my error reporting code (e.g. "ini_set('display_errors', '1');"), I don't know how (or where) to put the "exit;" in there.
  • 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.