Jump to content

Jacques1

Members
  • Posts

    4,207
  • Joined

  • Last visited

  • Days Won

    209

Everything posted by Jacques1

  1. PDO::closeCursor() frees the result set. Since the statement destructor does this automatically, the method is irrelevant except for edge cases. When you have a large result set and a script which keeps the statement open for a long time (thus preventing destructor execution), then it might be useful to manually free the result set. Or when you've run into an out-of-sync error due to an unbuffered query and a strange coding style (like another user did recently).
  2. I know this will probably sound crazy to you, but how about reading the replies? The solution is right in front of you. All you have to do is stop complaining and open your eyes.
  3. Have you heard of this cool new search engine called Google search? You should try it. Actually, you can just open the PHP manual: Note: When using mysqli_stmt_execute(), the mysqli_stmt_fetch() function must be used to fetch the data prior to performing any additional queries. So, is there a query before the above statement where you haven't fetched/freed the result set yet?
  4. I recommend you switch to PDO now. Then we can stop wasting time on code that will be rewritten anyway. Your code above fetches the rows as associative arrays (that's what the _assoc means), but then your trying to access the data as if the rows where objects (using the -> operator). You need to choose either objects or associative arrays. Both at the same time isn't possible.
  5. You are aware of shell injections, yes?
  6. The database interface for the 21st century is PDO.
  7. What is failing? What is the exact error message? If there is none, fix your PHP error reporting and also enable mysqli errors: // this comes before the mysqli connection code // make mysqli throw an exception whenever it encounters a problem $mysqli_driver = new mysqli_driver(); $mysqli_driver->report_mode = MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT; My guess: Your prepared statement uses $conn_mysqli, but your query uses $mysqli. Obviously one of them is wrong.
  8. The only solution is to learn how the relational database model works (this is the basis of SQL) and then create a proper data layout. Your task of assigning results to tests is trivial and should be a matter of minutes. But your CSVs have turned it into a nightmare of implode/explode gymnastics and undefined indexes. Now imagine having to solve a difficult problem. The code also needs works. Don't just throw all your PHPSQLHTMLCSS into one big blob of text. Structure the code. CSS belongs into a separate file; the PHP code belongs on top of the script, then comes the HTML markup: <?php // PHP code goes here; this is where you process the request, do your queries etc. ?> <!-- HTML markup goes here --> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Title</title> <link rel="stylesheet" href="/path/to/external/stylesheet.css"> </head> <body> </body> </html> Dynamic queries are implemented with prepared statements. Manual escaping is fragile, messy and obsolete.
  9. Sure, but going to a website which clearly isn't the official one (ups.com uses HTTPS with an EV certificate for everything), downloading a .doc file (why would UPS use such a weird format?), opening it directly in Word and probably clicking past the macro warnings is a pretty long chain of actions. I'm not saying this to put you down or pretend like I'm immune to attacks. My point is that you need a lot more paranoia on multiple levels, not just related to strange e-mails.
  10. Forget about trying to “clean” the system. Cut the Internet connection, save your data (or what's left of it), wipe the PC and start over. And for the love of god, stop opening random files from the Internet.
  11. You should consider using a proper FFmpeg API (like this one) instead of scraping the data from the shell: $ffprobe = FFMpeg\FFProbe::create(); $duration = $ffprobe ->format('/path/to/video/mp4') // extracts file informations ->get('duration'); // returns the duration property (taken from the documentation) If you absolutely must use the shell, use the right function. It doesn't make sense to echo the output, catch it and then write it to a variable when you can just write it to a variable: $duration = shell_exec('ffmpeg -i '.escapeshellarg($tmp).' 2>&1');
  12. It's still unclear to me why you can't just install the current Debian (or Ubuntu) in your VirtualBox, install PHP 7 via the package manager and be done with it. Then we don't have to spend our time debugging ancient PHP versions in old Ubuntu versions in Vagrant (which none of us three is familiar with).
  13. Mentioning isn't the problem. I think none of us is masochistic enough to implement them. Anyway, I would really switch to a database system which can handle hierarchical data. Right now, every single task requires yet another workaround from this forum, turning her application more and more into an unmaintainable black box. PostgreSQL actually has an extension specifically for tree structures: CREATE TABLE test (path ltree); INSERT INTO test VALUES ('Top'); INSERT INTO test VALUES ('Top.Science'); INSERT INTO test VALUES ('Top.Science.Astronomy'); INSERT INTO test VALUES ('Top.Science.Astronomy.Astrophysics'); INSERT INTO test VALUES ('Top.Science.Astronomy.Cosmology'); To get all descendants of the “Science” category: SELECT path FROM test WHERE path <@ 'Top.Science'; path ------------------------------------ Top.Science Top.Science.Astronomy Top.Science.Astronomy.Astrophysics Top.Science.Astronomy.Cosmology (4 rows) Looks fairly promising.
  14. Don't respond with a JSON document when you want an empty body.
  15. Redis requires an extra server. I meant something like ZeroMQ.
  16. The version without Barand's MySQL gymnastics: $familyTreeStmt = $database->prepare(' WITH RECURSIVE family_tree (dog_id, dogname, generation, ancestors) AS ( SELECT id, dogname, 1, ARRAY[id]::int[] FROM dog WHERE :dog_id IN (dog.father_id, dog.mother_id) UNION ALL SELECT dog.id, dog.dogname, family_tree.generation + 1, family_tree.ancestors || dog.id FROM family_tree JOIN dog ON family_tree.dog_id IN (dog.father_id, dog.mother_id) ) SELECT * FROM family_tree ORDER BY ancestors '); $familyTreeStmt->execute([ 'dog_id' => 1, ]); foreach ($familyTreeStmt as $dog) { echo str_repeat('-', $dog['generation']).$dog['dogname'].'<br>'; } Output -dog-1 --dog-1-1 ---dog-1-1-1 --dog-1-2 -dog-2 -dog-3 Yes, that's a single query and a single loop which fetches the exact subtree which is needed (as opposed to the whole table).
  17. If the C++ application already has an implementation for JSON-RPC over Unix domain sockets, you don't really have a choice. If both applications currently have no communication component at all, I would use a message-oriented mechanism (e. g. a message queue), so that you don't have to assemble the data from a byte stream.
  18. I don't think you understand the post. What I recommend is to abandon toy database systems like MySQL in favor of more serious software like PostgreSQL. Then we can all stop wasting our time with workarounds. If MySQL is the only choice available, I recommend picking the workaround carefully. Your quick hacks are often good enough for amateur applications which never grow. But sometimes applications do grow, and that's when the PHPMySQL duct tape comes off.
  19. unix is the scheme for Unix sockets, yes. A fair warning: If you're going back to low-level sockets (for whatever reason), be prepared for extensive research and learning on your own. Understanding the PHP stream API is actually the trivial part. Then you need to implement and possibly even design a protocol to handle the communication, which isn't something you'll find in a step-by-step tutorial. And as always, we can only comment on the information you've provided. Whether your design choices even make sense is impossible to tell without knowing the full context.
  20. For the record: This loads the entire table into the application and builds the entire tree of every single dog that has ever been registered.
  21. With a Google search or the PHP manual (hint: stream functions). They're working on it.
  22. Traversing an entire family tree requires a recursive query, and MySQL doesn't have that. There are workarounds, though: If you can define a maximum number of generations (e. g. 3), you can join the tables with itself that many times: dog AS children LEFT JOIN dog AS grandchildren ... LEFT JOIN dog AS grandgrandchildren .... You can run the query multiple times. This will be inefficient, but it may be acceptable for small amounts of data. There are hacks with stored procedures (not recommended). So is there a fixed limit?
  23. I suggest you switch to PDO right away. Using mysqli correctly is a pain in the ass, even for more experienced programmers. Whether this function even makes sense is also debatable. I would do the validation in the main script. <?php function connectToDatabase($host, $user, $password, $database, $characterEncoding) { $dsn = 'mysql:host='.$host.';dbname='.$database.';charset='.$characterEncoding; return new PDO($dsn, $user, $password, [ PDO::ATTR_EMULATE_PREPARES => false, // use actual prepared statements, don't emulate them PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, // make PDO throw an exception in case of an error PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, // fetch associative arrays by default ]); } <?php function showErrorPage($message, $statusCode) { http_response_code($statusCode); echo $message; } <?php require_once '/path/to/config.php'; require_once '/path/to/functions.php'; require_once '/path/to/database.php'; $database = connectToDatabase( $config['database']['host'], $config['database']['user'], $config['database']['password'], $config['database']['dbname'], $config['database']['encoding'] ); if (!isset($_GET['id'])) { showErrorPage('Missing URL parameter: id.', 400); exit; } if (!ctype_digit($_GET['id'])) { showErrorPage('Invalid URL parameter: id (integer expected).', 400); exit; } $memberStmt = $database->prepare(' SELECT id -- additional data FROM members WHERE id = :member_id '); $memberStmt->execute([ 'member_id' => $_GET['id'], ]); $member = $memberStmt->fetch(); if (!$member) { showErrorPage('Unknown member id.', 400); exit; } // at this point, $member contains all the member data you've selected
  24. So what have you tried so far? Your code doesn't show any parsing attempts. The task looks rather strange. You're posting data to your own scripts and scraping the response. How will the final application look like? Where does the data go? Why do you have to resort to scraping? Your choice of tools is strange as well. Consider using cURL with a standard HTML parser like DOMDocument.
×
×
  • 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.