php - Query after connecting to remote MySQL database using mysqli_real_connect

229

I can successfully connect to a remote MySQL database using mysqli_init(), mysqli_ssl_set and mysqlii_real_connect. I can't seem to find an example of a query being done after connecting. The result of mysqli_real_connect is a boolean value, whereas mysqli_connect returns an object representing the connection to the MySQL server. How do I refer to the connection after using mysqli_real_connect? Can anyone provide an example?

<?php error_reporting(E_ALL);
ini_set("display_errors", "1");
$obj = mysqli_init();
mysqli_options($obj, MYSQLI_OPT_CONNECT_TIMEOUT, 5);
mysqli_ssl_set( $obj,
                NULL,
                NULL,
                'ca-cert.pem',
                NULL,
                NULL);
$link = mysqli_real_connect($obj, 'xxxxxxxxxxxx.com', 'user', 'password, 'database_name');
if (!$link)
{
    die('<br /><br />Connect Error (' . mysqli_connect_errno() . ') '.mysqli_connect_error());
} ?>
205

Answer

Solution:

The way that you'd do it using your example ismysqli_query($obj, QUERY HERE);

However, you should consider using the OOP (Object Orientated Programming) style, as that would be easier for you to reference within the future.

<?php

Class Database {
      private $obj;

      public function __construct() {
          $this->obj = new mysqli('xxxxxxxxxxxx.com', 'user', 'password', 'database_name');
          if (!$this->obj) {
        die('<br /><br />Connect Error (' . mysqli_connect_errno() . ') '.mysqli_connect_error());
          }
      }

      public function query($query) {
          return  $this->obj->query($query);
      }
} 

?>

You would call this class by doing:

$db = new Database();

And from there on, you could simply call your function by using the following example.

$db->query("SELECT * FROM users"); // or whatever query you are parsing.

People are also looking for solutions to the problem: javascript - How to use send responses back to ajax and process each response separately

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.