Jump to content


  • Content Count

  • Joined

  • Last visited

Community Reputation

0 Neutral

About PatRoy

  • Rank
  1. Outputs: string(5) "mysql" string(6) "5.5.28" My weird setup is due to the fact that my 'server' is a farely old NAS running a armv5tel architecture Linux. I had 'kinda' jailed broken the NAS as to get custom servers on it (lighttpd, mysql, etc). But the fact that it's jailbroken, and old, limits me on packages and versions I can install on it (thus the reason its running PHP 7.1). It's pretty much a nightmare to try and compile stuff on it because of missing / eventually broken libraries... I think I have no choice but to live with it and accept the returned strings from MySQL by adopting my workaround to do my validation on my property setters only, instead of within my save(). Thanks for your help and times
  2. UPDATE: Upon reading https://www.php.net/manual/en/mysqlnd.install.php, it seems the mysqlnd is enabled by a ./configure option on either mysqli, or pdo_mysql... Looking at the ./configure command options within phpinfo(), I see the following two: --with-mysqli=shared,mysqlnd --with-pdo-mysql=shared,/ffp Would that mean that mysqli has mysqlnd enabled (thus the reason I see it in phpMyAdmin since it uses mysqli), and my pdo_mysql doesn't have it enabled (assuming, since the lack of it in the configure above) ? HOWEVER, if I just do: var_dump(extension_loaded('mysqlnd')); it returns TRUE. So, super confused here... but then again, is it telling me true, just because it's 'loaded' only if using mysqli to query the database?
  3. I'm not sure I understand how to look / confirm I'm using the mysqlnd driver... Even less how to force use it.. I do have extension=pdo_mysql.so loaded in my php.ini, which I thought was loading the mysqlnd driver (guess not!?). And, when I load the web phpMyAdmin to managemy DBs, the homepage does give me some info on the server, one of them being: Database client version: libmysql - mysqlnd 5.0.12-dev - 2015040. See screenshot attachment... Is the mysqlnd loaded 'per web application' ?
  4. PDO PDO support => enabled PDO drivers => mysql pdo_mysql PDO Driver for MySQL => enabled Client API version => 5.5.28
  5. Thanks for your reply! Well, indeed, I do set my DB connection with PDO::ATTR_EMULATE_PREPARES => FALSE. Would that mean that MySQL automatically just return strings, but if allowing PHP/PDO to do the prepares, then IT only can properly prepare and return native datatypes? I had set it to false, in part to protect against SQL Injections, though I don't remember how this worked out for this... To answer others, I needed to resolve this because I am writing my own ORM system, which loads objects from database into an array of properties: array(11) { ["id"]=> array(8) { ["column"]=> string(2) "id" ["type"]=> string(7) "integer" ["readonly"]=> bool(true) ["required"]=> bool(false) ["value"]=> string(1) "1" ["valueIfNull"]=> NULL ["useDBDefaultWhenNull"]=> bool(false) ["datetimeformat"]=> NULL } ["dateModified"]=> array(8) { ["column"]=> string(13) "date_modified" ["type"]=> string(8) "datetime" ["readonly"]=> bool(false) ["required"]=> bool(false) ["value"]=> string(19) "2020-06-07 22:35:24" ["valueIfNull"]=> NULL ["useDBDefaultWhenNull"]=> bool(false) ["datetimeformat"]=> NULL } .... } As you can see, the "value" are always strings. I have a validDatatype() check that validates $value before submitting into the database. To do so, I simply run a check that gettype($value) is the same as my prop "type". Initially, I wanted to do all of the datatype validation within my save() method. However, if when loading all fields are strings, when I go ahead, mod some values, and then save(), it'll fail datatype validation! My current workaround is to do my validation on my property setters only, instead of within the save(), but I still wondered why I was getting all strings from MySQL.... Cheers for your replies! P.
  6. Hello, My current server setup : PHP version: 7.1.4 MySQL 5.5.28 Database client version: libmysql - mysqlnd 5.0.12-dev I'm using PDO _MYSQL to run database queries. From what I read, in order to have MySQL return native datatypes (as opposed to just strings) : if using MySQLi: set $mysqli->options(MYSQLI_OPT_INT_AND_FLOAT_NATIVE, 1) if using PDO and have mysqlnd: "mysqlnd returns native data types when using Server-side Prepared Statements, for example an INT column is returned as an integer variable not as a string." Now, I do have mysqlnd, but a simple PDO Prepared statement still returns me all strings, even though some columns are set as INT, VARCHAR, DATETIME, etc.. $stmt = $pdo->prepare("SELECT * FROM tbl_users WHERE id=1"); $stmt->execute(); $user = $stmt->fetch(); var_dump($user); OUTPUT: array(13) { ["id"]=> string(1) "1" ["is_admin"]=> string(1) "1" ["is_active"]=> string(1) "1" ["username"]=> string(5) "jdoe" ... } I'm not sure what to look for to resolve this issue if I want it to return INT as INT and not STRING, etc. Does having `extension_loaded('mysqlnd') == true`, make my system automatically use this native mysql driver? How can I tell ? Could this be a version problem perhaps? Much thanks! Pat
  7. The idea is to stop the script whenever an error (or exception) occurs. My handler essentially logs the error with a uniqid() to a log file, sends an email to admins with the full error message + ID, and finally, outputs an html page with a generic message "Oh crap! Something went wrong! Contact admins with ID: <error id>" So, from my understanding, I'd need 2 Error Handler classes: one for errors, and one for exceptions.... OR, I guess I could have in my ErrorHandler class 2 functions : /** * Handle Errors: set from set_error_handler() */ public function handleError($errno, $errstr, $errfile, $errline) { ... $this->sendToEmail(); // send email to admins $this->saveToLog(); // save to log file $this->loadErrorPage(); // print error page and exit()! } /** * Handle Exceptions: set from set_exception_handler() */ public function handleException($ex) { ... $this->sendToEmail(); // send email to admins $this->saveToLog(); // save to log file $this->loadErrorPage(); // print error page and exit()! } Would this be a proper way of doing this then ? I do not like having to trigger_error() in my try/catch... For one thing, I think the script should catch any such mistakes, and for another, with trigger_error(), I do not have access to the Exception object (to stacktrace it, etc.). Thanks for your inputs! Pat
  8. I'm sure it's not much, but I'm not understanding something with custom error handlers: I've created a custom error handler, which I initially set when my page loads : set_error_handler(array ( new ErrorHandler(), 'handleError' )); It seems to catch all internal PHP errors, such as if I: var_dump($non_existing_var); right after the set_error_handler.... Now, I have an object that throws an exception after: set_error_handler(array ( new ErrorHandler(), 'handleError' )); $locale = new \CorbeauPerdu\i18n\Locale(...); // this should throw an exception ... I thought that with an error handler set, I could 'skip' the try/catch for it, but doing so, PHP spits out in its internal log: PHP Fatal error: Uncaught CorbeauPerdu\i18n\LocaleException: .... My error handler doesn't catch it at all before, thus my page breaks!! If I want it to go through the error handler, I have to init my Locale with a try/catch and use trigger_error() like so: set_error_handler(array ( new ErrorHandler(), 'handleError' )); try { $locale = new \CorbeauPerdu\i18n\Locale(...); // this should throw an exception } catch(Exception $e) { trigger_error($e->getMessage(), E_USER_ERROR); } ... Is this normal ? I thought one of the goals of the error_handler was to catch anything that wasn't dealt with? Thanks for your answers!
  9. It is still one line more than me, if you consider the fact that I do it the same way with my DBWrapper 😛 But really, this discussion is non-sense and pointless to me, especially if one doesn't even look at the usage examples / documentation and doesn't care to see how it can really help down the line. Again, you take, or not... Whatever suits you, no bad feelings héhé. Cheers. P.
  10. I think you write a bit more than that... Your two-liner code should really be: try { $mydb = new PDO($dsn, $username, $password, $options); $data = $mydb->prepare("SELECT * FROM users WHERE ID = :id"); $data->execute(['id'=>$userid]); } catch (Exception $e) { die($e->getMessage()); } Plus, I was under the impression that it's still better to also bindValue / bindParam, to also set its proper datatype, protecting against SQL Injections, which you are not using... With your way, I have to re-enter all of the DB configs everytime, which is the least of the problems for me, but still... My example was for a very basic usage. But what if you want to say do multi-inserts, AND commit on every inserts so that if one insert fails, the others still go through, and you can get a list of failed ones? I've had this case! And this class can handle this, without writing too much code. I am not going to debate 'why' I think my class is useful here... Before saying anything, have a look at its UsageExamples.php and decide for yourself. If you don't think it to be useful, then by all means, don't use it I'm not trying to sell it here , but rather just share.
  11. Here's an example of a simple utilization of my DBWrapper: try { $mydb = new DBWrapper(); // get the data and do what you want with it... $data = $mydb->readData('SELECT * FROM users where ID = :id', $userid); ... } catch (DBWrapperException $ex) { die($ex->getMessage()); } The DBWrapper / readData() will take care of creating the PDO object, if it's not already created. It will take care of creating a proper PDOStatement inside it, use bindParam and protect against SQL injections, etc. etc. You can pass the parameter value as simple value to read and storeData() (doing so, DbWrapper will determine itself datatypes to map data to in DB), but you can also pass it an array mapping parameter value => datatype.... I'm not gonna write all of its functionality's here, but you can guys get the idea, I think. Cheers. Pat
  12. By "rewrite", I don't mean rewriting PDO lolll ! I mean I don't like having to always do my new PDO(...) And then do my prepared statements etc... I wanted to shorten my code ! Really, just have a look at the UsageExamples.php. it says it all way better than what I could explain It's... As I said: a wrapper around PDO.
  13. Alright, never mind the wetransfer BS. I've setup a Github repository: https://github.com/ravenlost/CorbeauPerdu/tree/master/i18n/ While I was at it, I've also uploaded and shared my pretty sweet (I think!) DBWrapper as well: https://github.com/ravenlost/CorbeauPerdu/tree/master/Database/ I worked a long time on this DBWrapper as well, just because it annoyed me to re-write PDO code everytime... Check out both of these classe's UsageExamples.php. It shows it all
  14. Hello, Requinix! First thanks for your reply and piece of code example! Things have been a little chaotic around here (where is it not? ;)) and I've been a little offline. I hear what you are saying about not having to 'code it all' right away, stick to whats really needed for needs. Totally ! If this project was a paid one, for a company, then yeah I'd do it like you say, cause you do have deadlines to respect, etc. However, this is just a personal project of mine, and since I've got no work for now (and no life ), it becomes a personal challenge, and a great learning tool! I've never programmed in PHP before. Used to code a lot in Java, VB (yeah' I'm that old ), Perl, etc.. and worked quite a bit as a *Nix administrator, but I've quit all of that with a career change. I haven't really coded in nearly 12 years or so... Thus, need to re-learn a lot That being said, this hole issue initially started because I couldn't get gettext() to work on my server (because it's old NAS), and I really want to use this solution if I ever push my site to 'production' on a hosting provider. So, I decided to create a 'wrapper' class around gettext that would have a boolean set in its constructor to $useGettext true, or false. If true, well all of its functions uses gettext to get the messages. If false, then it uses JSON files... I'm pretty happy now about it. It is super flexible thus will allow using translations on all kinds of hosts! If using JSON files, if also deals with plurals, just as Gettext does with a 'nplurals' and 'plural' value set, same syntax as gettext. and It's even able to handle loading additional domains (a.k.a translation files). Finally, I've made it so that all of its functions to translate messages are the same names as the gettext's functions, like so: * Locale::gettext() or Locale::_() // Lookup a message in the current domain, singular form * Locale::fgettext() or Locale::_f() // Lookup a message in the current domain, singular form, and sprintf's the message with $v value(s) * Locale::ngettext() or Locale::_n() // Lookup a message in the current domain, plurial form * Locale::fngettext or Locale::_fn() // Lookup a message in the current domain, plurial form, and sprintf's the message with $v value(s) * Locale::dgettext() or Locale::_d() // Lookup a message in a given domain, singular form * Locale::fdgettext or Locale::_fd() // Lookup a message in a given domain, singular form, and sprintf's the message with $v value(s) * Locale::dngettext() or Locale::_dn() // Lookup a message in a given domain, plurial form * Locale::fdngettext or Locale::_fdn() // Lookup a message in a given domain, plurial form, and sprintf's the message with $v value(s) Do note that I know 'Locale' is already a used class name within PHP, but mine is in its own namespace: CorbeauPerdu\i18n\Locale I'd love to know if anyone thinks this could be a real problem, and if so, I can just rename it Anyhow, I'm writing all of this because I'm a strong believer in sharing, and wanted to share this class along.. (perhaps even create a project in Github or something). I'll repost link here if I do... I'd love to share you the LocaleUsageExamples.php and the class itself, but can't attach any PHP in this forum. If you or anyone else is interested or curious, here's a temporary wetransfer link: https://wetransfer.com/downloads/eb670a961970210705a2824df897e43b20200416125315/ebbcf9d142a3eaa576eca92dc1638c7220200416125329/885d76 Thanks again for the time you've taken and your inputs! Pat
  15. For now, I just need the messages changed. However, I never know down the road. PLUS I'm trying to create a versatile package which I can re-use in many projects.. Thus also one of the reasons my class can use either one, Gettext or JSON files... And on top, I want to do '"things right" the first time
  • 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.