Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your question is not very specific, and I have the feeling you're not really aware what your concrete problems are. You've already accept an answer that gives you some code but puts you in the wrong direction as it does not solve your underlying problem and wastes code.</p> <p>The first thing you should know if you want to make use and benefit of the object oriented interface of PHP's mysqli extension is, that the <code>Mysqli</code> class represents the database connection (the "link" as it was named for a procedural approach) already:</p> <pre><code>require('inc/dbc.php'); $dbConnection = new Mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME); </code></pre> <p>That's already it. Sure you might want to use some error handling here:</p> <pre><code>if ($dbConnection-&gt;connect_error) { throw new Exception( sprintf('(#%d) %s', $dbConnection-&gt;connect_errorno, $dbConnection-&gt;connect_error) ); } </code></pre> <p>Throwing exception instead of let's say <code>die('message')</code> is done, because you can easily create an exception handler so that you can display a more useful response to the user from within a central place instead of handling each exceptional error-case where it appears (actually at the place where <code>die</code> would be).</p> <p>You can also log and mail the backtrace so you can fix things more easily. Naturally you do not need to use exceptions, however the rule of thumb is to use <code>die</code> only in scripts you throw away in let's say a week and it does not work well with object oriented design.</p> <p>As you will need this code anyway in all places where you will need your database connection, you can create your own connection object to bring those parts together as they belong together:</p> <pre><code>class DatabaseException extends Exception { } class DatabaseConnection extends Mysqli { public function __construct($host, $user, $password, $database = "", $port = NULL, $socket = NULL) { parent::__construct($host, $user, $password, $database, $port, $socket); $this-&gt;throwConnectionExceptionOnConnectionError(); } private function throwConnectionExceptionOnConnectionError() { if (!$this-&gt;connect_error) return; $message = sprintf('(%s) %s', $this-&gt;connect_errno, $this-&gt;connect_error); throw new DatabaseException($message); } } </code></pre> <p>The usage is actually pretty straight forward and very much the same, only the name of the class varies and it's definition needs to be loaded:</p> <pre><code>require('inc/dbc.php'); require('inc/database.php'); $dbConnection = new DatabaseConnection(DB_HOST, DB_USER, DB_PASS, DB_NAME); </code></pre> <p>As written, the connection object already represents your database connection. So every part in your application that needs it, has to ask for it. Let's review your example function:</p> <pre><code>function getOption($id, $db_link) { // $db_link = $this-&gt;db_link; $res = mysqli_query($this-&gt;db_link,"SELECT * from config where id='1'"); $row = mysqli_fetch_array($res); return $row['option']; } </code></pre> <p>I renamed the function and I commented the first line, even this might be what you want. Actually, <em>if</em> that function would be part of the <code>DatabaseConnection</code> object, it could work like this:</p> <pre><code>class DatabaseConnection extends Mysqli { ... public function getOption($id) { $statement = $this-&gt;prepare('SELECT `option` FROM config WHERE id=?'); $statement-&gt;bind_param('i', $id); $statement-&gt;execute(); $statement-&gt;bind_result($option); $statement-&gt;fetch(); $statement-&gt;close(); return $option; } </code></pre> <p>As this example demonstrates, the database connection is already there. However, this is not advisable. Imagine you not only have options but this and that and such and what not more. You would create one function after the other all in one class. Well for a little application that might even so work right, but imagine more and more. You would get one very large class that is responsible for many things. So it would be bad to do this, even if you can use <code>$this</code> already to prepare the statement.</p> <p>Also take note that you should prepare statements. This has been answered here numerous times, if you're not used to it, read about it, it's worth the lines. There are better ways to not repeat code (DRY: Don't repeat yourself) while stepping into object oriented (you should even already do this with procedural).</p> <p>So as to have this all in one class would be a problem, you instead put it in a class of it's own:</p> <pre><code>class DatabaseModelBase { protected $connection; public function __construct(Connection $connection) { $this-&gt;connection = $connection; } protected function prepare($query) { $connection = $this-&gt;connection; $statement = $connection-&gt;prepare($query); if (!$statement) { throw new DatabaseException( sprintf('(%s) %s', $connection-&gt;error, $connection-&gt;errno) ); } return $statement; } } class Option extends DatabaseModelBase { public function find($id) { $statement = $this-&gt;prepare('SELECT `option` FROM config WHERE id=?'); $statement-&gt;bind_param('i', $id); $statement-&gt;execute(); $statement-&gt;bind_result($option); $statement-&gt;fetch(); $statement-&gt;close(); return $option; } } </code></pre> <p>This has some extended error handling again, because most often mistakes are made in the SQL query. And as you can see the individual function to fetch some specific data is placed in it's own class. You can use such classes to group the fetching and updating for specific datatypes.</p> <p>Usage in full:</p> <pre><code>$dbConnection = new Connection(DB_HOST, DB_USER, DB_PASS, DB_NAME); $option = new Option($dbConnection); $optionValue = $option-&gt;find(1); echo $optionValue; # value for option with ID1 </code></pre> <p>The names of the <code>Option</code> object probably is not well, I tried to keep the example lightweight but also offer some separation. For other scenarious you might want to prefer some different kind how to access the db connection, because it is injected into <code>Option</code> via it's constructor, <code>Option</code> does not deal any longer with the details how the database connection is being created.</p> <p>For example you can make the database connection object more smart only to connect to the database the first time prepare or query is actually used with it. So that requests to your website that do not need a database connection would not needlessly connect to the database.</p> <p>You find more examples in other questions and you might want to learn about dependency injection. Also you always want to keep things apart from each other, so you have some objects that are only lightly connected to each other.</p> <ul> <li><a href="https://stackoverflow.com/questions/10919277/how-to-successfully-rewrite-old-mysql-php-code-with-deprecated-mysql-functions">How to successfully rewrite old mysql-php code with deprecated mysql_* functions?</a></li> <li><a href="https://stackoverflow.com/questions/2030624/dependency-injection-simple-implementation">Dependency Injection simple implementation</a></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload