0

I want to modify a project to make use of SSL for mysql connections. I managed to create a working ssl connection. But I cannot manage to recreate $handler as the same object/connection as before, so the rest of the source code is not working. How can I accomplish that $handler is exactly the same kind of object as before?

Original code:

  $handler = @new mysqli($host, $settings['user'], $settings['password'], $settings['database'], $port); 

I changed code to:

  $db = mysqli_init();
  mysqli_options($db, MYSQLI_CLIENT_SSL, true);
  mysqli_options($db, MYSQLI_OPT_SSL_VERIFY_SERVER_CERT, false);
  mysqli_ssl_set($db, NULL, NULL, '/usr/local/www/dbssl/mosdb.ca.pem', NULL, NULL);
  mysqli_ssl_set($db, '/usr/local/www/dbssl/mosdb.client-key.pem', '/usr/local/www/dbssl/mosdb.client-cert.pem', '/usr/local/www/dbssl/mosdb.ca.pem', NULL, NULL);   
  $handler = @mysqli_real_connect($db, $host, $settings['user'], $settings['password'], $settings['database'], $port, NULL, MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT);

The answer should be that I should change the code to this if I want the same kind of object for the $handler:

    $handler = mysqli_init();
    $handler->options(MYSQLI_CLIENT_SSL, true);
    $handler->options(MYSQLI_OPT_SSL_VERIFY_SERVER_CERT, false);
    $handler->ssl_set(NULL, NULL, '/usr/local/www/dbssl/mosdb.ca.pem', NULL, NULL);
     

    $handler->real_connect($host, $settings['user'], $settings['password'], $settings['database'], $port, NULL, MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT);
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Lexib0y
  • 519
  • 10
  • 27
  • 1
    Why are you suppressing errors from `mysqli_real_connect()`? If you're having trouble making the connection, don't you think its error messages will help you diagnose the problem? – Barmar Jul 22 '22 at 00:32
  • Don't display errors, log them. – user3783243 Jul 22 '22 at 00:33
  • The connection itself is now working, I have tested that. – Lexib0y Jul 22 '22 at 00:34
  • @user3783243 this is only true for the live server. Besides, definitely, the database connection code is not the place where it must be decided how to process the errors - so this notion, although mostly correct, is just irrelevant here. – Your Common Sense Jul 22 '22 at 06:32

0 Answers0