Skip to content
This repository has been archived by the owner on Mar 27, 2020. It is now read-only.

Commit

Permalink
Added DB::fetchOne()
Browse files Browse the repository at this point in the history
Added examples to README.md
  • Loading branch information
jasny committed Oct 20, 2012
1 parent fcaf68c commit 7169b8c
Show file tree
Hide file tree
Showing 7 changed files with 322 additions and 155 deletions.
19 changes: 18 additions & 1 deletion README.md
Expand Up @@ -8,4 +8,21 @@ A simple DB layer in PHP for using MySQL, featuring
* Quoting tables/fields and values
* Fetch all rows, column, key/value pair and single value
* Simple saving by passing associated arrays
* Query exceptions (instead of returning false)
* Query exceptions (instead of returning false)

## Example ##

<?php
new DB($host, $user, $pwd, $dbname);
$result = DB::conn()->query("SELECT * FROM foo");
$result = DB::conn()->query("SELECT * FROM foo WHERE type = ?", $type);
$result = DB::conn()->query("SELECT * FROM foo WHERE type = ? AND cat IN ?", $type, array(1, 7));

$items = DB::conn()->fetchAll("SELECT id, name, description FROM foo WHERE type = ?", MYSQLI_FETCH_ASSOC, $type);
$item = DB::conn()->fetchOne("SELECT * FROM foo WHERE id = ?", MYSQLI_FETCH_ASSOC, $id);
$names = DB::conn()->fetchColumn("SELECT name FROM foo WHERE type = ?", $type);
$list = DB::conn()->fetchPairs("SELECT id, name FROM foo WHERE type = ?", $type);
$name = DB::conn()->fetchValue("SELECT name FROM foo WHERE id = ?", $id);

DB::conn()->save('foo', $values);
DB::conn()->save('foo', array($values1, $values2, $values3));
4 changes: 4 additions & 0 deletions composer.json
Expand Up @@ -18,6 +18,10 @@
"require": {
"php": ">=5.3.0"
},
"suggest" : {
"jasny/config": "Configure your DB and make it work out of the box",
"jasny/dbquery-mysql": "Supports building, parsing and modifying MySQL queries"
},
"autoload": {
"psr-0": {
"DB": "src/"
Expand Down
68 changes: 60 additions & 8 deletions docs/class-DB.html
Expand Up @@ -123,7 +123,7 @@ <h1>Class DB</h1>
<br />
new DB($host, $user, $pwd, $dbname);<br />
$result = DB::conn()-&gt;query("SELECT * FROM foo WHERE id = ?", $id);<br />
<b>Located at</b> <a href="source-class-DB.html#11-293" title="Go to source code">DB.php</a><br />
<b>Located at</b> <a href="source-class-DB.html#11-317" title="Go to source code">DB.php</a><br />
</div>


Expand Down Expand Up @@ -317,6 +317,58 @@ <h4>Example</h4>
</div>


</div>
</div></td>
</tr>
<tr data-order="fetchOne" id="_fetchOne">

<td class="attributes"><code>
public
array

</code>
</td>

<td class="name"><div>
<a class="anchor" href="#_fetchOne">#</a>
<code><a href="source-class-DB.html#131-153" title="Go to source code">fetchOne</a>( <span>string|mysqli_result <var>$query</var></span>, <span>integer <var>$resulttype</var> = MYSQLI_ASSOC</span> )</code>

<div class="description short">

<p>Query and fetch all result rows as an associative array, a numeric array, or
both.</p>

</div>

<div class="description detailed hidden">

<p>Query and fetch all result rows as an associative array, a numeric array, or
both.</p>



<h4>Parameters</h4>
<div class="list"><dl>
<dt><var>$query</var></dt>
<dd><code>string|mysqli_result</code><br>$query SQL Query or DB result</dd>
<dt><var>$resulttype</var></dt>
<dd><code>integer</code><br>$resulttype MYSQLI_ASSOC, MYSQLI_NUM, or MYSQLI_BOTH</dd>
</dl></div>

<h4>Returns</h4>
<div class="list">
<code>array</code><br />
</div>


<h4>Example</h4>
<div class="list">
DB::conn()-&gt;fetchOne("SELECT * FROM mytable");<br />
DB::conn()-&gt;fetchOne("SELECT * FROM mytable", MYSQLI_NUM);<br />
DB::conn()-&gt;fetchOne("SELECT * FROM mytable WHERE id=?", MYSQLI_ASSOC, $id);<br />
</div>


</div>
</div></td>
</tr>
Expand All @@ -331,7 +383,7 @@ <h4>Example</h4>

<td class="name"><div>
<a class="anchor" href="#_fetchColumn">#</a>
<code><a href="source-class-DB.html#131-153" title="Go to source code">fetchColumn</a>( <span>string|mysqli_result <var>$query</var></span> )</code>
<code><a href="source-class-DB.html#155-177" title="Go to source code">fetchColumn</a>( <span>string|mysqli_result <var>$query</var></span> )</code>

<div class="description short">

Expand Down Expand Up @@ -378,7 +430,7 @@ <h4>Example</h4>

<td class="name"><div>
<a class="anchor" href="#_fetchPairs">#</a>
<code><a href="source-class-DB.html#155-176" title="Go to source code">fetchPairs</a>( <span>string|mysqli_result <var>$query</var></span> )</code>
<code><a href="source-class-DB.html#179-200" title="Go to source code">fetchPairs</a>( <span>string|mysqli_result <var>$query</var></span> )</code>

<div class="description short">

Expand Down Expand Up @@ -427,7 +479,7 @@ <h4>Example</h4>

<td class="name"><div>
<a class="anchor" href="#_fetchValue">#</a>
<code><a href="source-class-DB.html#178-196" title="Go to source code">fetchValue</a>( <span>string|mysqli_result <var>$query</var></span> )</code>
<code><a href="source-class-DB.html#202-220" title="Go to source code">fetchValue</a>( <span>string|mysqli_result <var>$query</var></span> )</code>

<div class="description short">

Expand Down Expand Up @@ -474,7 +526,7 @@ <h4>Example</h4>

<td class="name"><div>
<a class="anchor" href="#_save">#</a>
<code><a href="source-class-DB.html#199-239" title="Go to source code">save</a>( <span>string <var>$table</var></span>, <span>array <var>$values</var> = <span class="php-keyword1">array</span>()</span>, <span>boolean <var>$update</var> = <span class="php-keyword1">true</span></span> )</code>
<code><a href="source-class-DB.html#223-263" title="Go to source code">save</a>( <span>string <var>$table</var></span>, <span>array <var>$values</var> = <span class="php-keyword1">array</span>()</span>, <span>boolean <var>$update</var> = <span class="php-keyword1">true</span></span> )</code>

<div class="description short">

Expand Down Expand Up @@ -528,7 +580,7 @@ <h4>Example</h4>

<td class="name"><div>
<a class="anchor" href="#_quote">#</a>
<code><a href="source-class-DB.html#242-257" title="Go to source code">quote</a>( <span>mixed <var>$value</var></span>, <span>string <var>$empty</var> = <span class="php-quote">'NULL'</span></span> )</code>
<code><a href="source-class-DB.html#266-281" title="Go to source code">quote</a>( <span>mixed <var>$value</var></span>, <span>string <var>$empty</var> = <span class="php-quote">'NULL'</span></span> )</code>

<div class="description short">

Expand Down Expand Up @@ -572,7 +624,7 @@ <h4>Returns</h4>

<td class="name"><div>
<a class="anchor" href="#_backquote">#</a>
<code><a href="source-class-DB.html#259-268" title="Go to source code">backquote</a>( <span>string <var>$field</var></span> )</code>
<code><a href="source-class-DB.html#283-292" title="Go to source code">backquote</a>( <span>string <var>$field</var></span> )</code>

<div class="description short">

Expand Down Expand Up @@ -614,7 +666,7 @@ <h4>Returns</h4>

<td class="name"><div>
<a class="anchor" href="#_bind">#</a>
<code><a href="source-class-DB.html#270-292" title="Go to source code">bind</a>( <span>string <var>$query</var></span>, <span>mixed <var>$params</var> = <span class="php-keyword1">array</span>()</span> )</code>
<code><a href="source-class-DB.html#294-316" title="Go to source code">bind</a>( <span>string <var>$query</var></span>, <span>mixed <var>$params</var> = <span class="php-keyword1">array</span>()</span> )</code>

<div class="description short">

Expand Down

0 comments on commit 7169b8c

Please sign in to comment.