Snipt.net is closing on December 31st, 2016

For more information, please visit this blog post.

snipt

Ctrl+h for KB shortcuts

PHP

Php PDO Conection

1
2
3
4
5
6
try {
    $conn = new PDO('mysql:host=localhost;dbname=myDatabase', $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
    echo 'ERROR: ' . $e->getMessage();
}
https://snipt.net/embed/e5be5948f78613a2948a45daaac3804d/
https://snipt.net/raw/e5be5948f78613a2948a45daaac3804d/
e5be5948f78613a2948a45daaac3804d
php
PHP
6
2016-12-09T13:05:33
True
False
False
/api/public/snipt/79312/
php-pdo-conection
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="x">try {</span> </span><span id="L-2"><a name="L-2"></a><span class="x"> $conn = new PDO(&#39;mysql:host=localhost;dbname=myDatabase&#39;, $username, $password);</span> </span><span id="L-3"><a name="L-3"></a><span class="x"> $conn-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);</span> </span><span id="L-4"><a name="L-4"></a><span class="x">} catch(PDOException $e) {</span> </span><span id="L-5"><a name="L-5"></a><span class="x"> echo &#39;ERROR: &#39; . $e-&gt;getMessage();</span> </span><span id="L-6"><a name="L-6"></a><span class="x">}</span> </span></pre></div> </td></tr></table>
PDO, PHP

PHP

PDO: Update

1
2
3
4
5
<?php
$stmt = $db->prepare("UPDATE table SET name=? WHERE id=?");
$stmt->execute(array($name, $id));
$affected_rows = $stmt->rowCount();
?>
https://snipt.net/embed/a8a541866727d2e51793c352170cbb5c/
https://snipt.net/raw/a8a541866727d2e51793c352170cbb5c/
a8a541866727d2e51793c352170cbb5c
php
PHP
5
2016-12-09T09:17:01
True
False
False
Jan 11, 2013 at 04:24 AM
/api/public/snipt/53145/
pdo-update
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="cp">&lt;?php</span> </span><span id="L-2"><a name="L-2"></a><span class="nv">$stmt</span> <span class="o">=</span> <span class="nv">$db</span><span class="o">-&gt;</span><span class="na">prepare</span><span class="p">(</span><span class="s2">&quot;UPDATE table SET name=? WHERE id=?&quot;</span><span class="p">);</span> </span><span id="L-3"><a name="L-3"></a><span class="nv">$stmt</span><span class="o">-&gt;</span><span class="na">execute</span><span class="p">(</span><span class="k">array</span><span class="p">(</span><span class="nv">$name</span><span class="p">,</span> <span class="nv">$id</span><span class="p">));</span> </span><span id="L-4"><a name="L-4"></a><span class="nv">$affected_rows</span> <span class="o">=</span> <span class="nv">$stmt</span><span class="o">-&gt;</span><span class="na">rowCount</span><span class="p">();</span> </span><span id="L-5"><a name="L-5"></a><span class="cp">?&gt;</span><span class="x"></span> </span></pre></div> </td></tr></table>
"prepared statements", PDO, Update

PHP

PDO: Delete

1
2
3
4
5
6
<?php
$stmt = $db->prepare("DELETE FROM table WHERE id=:id");
$stmt->bindValue(':id', $id, PDO::PARAM_STR);
$stmt->execute();
$affected_rows = $stmt->rowCount();
?>
https://snipt.net/embed/d3266a244df1f38eac02526f4a93d918/
https://snipt.net/raw/d3266a244df1f38eac02526f4a93d918/
d3266a244df1f38eac02526f4a93d918
php
PHP
6
2016-12-04T14:00:07
True
False
False
/api/public/snipt/53144/
pdo-delete
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="cp">&lt;?php</span> </span><span id="L-2"><a name="L-2"></a><span class="nv">$stmt</span> <span class="o">=</span> <span class="nv">$db</span><span class="o">-&gt;</span><span class="na">prepare</span><span class="p">(</span><span class="s2">&quot;DELETE FROM table WHERE id=:id&quot;</span><span class="p">);</span> </span><span id="L-3"><a name="L-3"></a><span class="nv">$stmt</span><span class="o">-&gt;</span><span class="na">bindValue</span><span class="p">(</span><span class="s1">&#39;:id&#39;</span><span class="p">,</span> <span class="nv">$id</span><span class="p">,</span> <span class="nx">PDO</span><span class="o">::</span><span class="na">PARAM_STR</span><span class="p">);</span> </span><span id="L-4"><a name="L-4"></a><span class="nv">$stmt</span><span class="o">-&gt;</span><span class="na">execute</span><span class="p">();</span> </span><span id="L-5"><a name="L-5"></a><span class="nv">$affected_rows</span> <span class="o">=</span> <span class="nv">$stmt</span><span class="o">-&gt;</span><span class="na">rowCount</span><span class="p">();</span> </span><span id="L-6"><a name="L-6"></a><span class="cp">?&gt;</span><span class="x"></span> </span></pre></div> </td></tr></table>
"prepared statements", PDO, delete

PHP

PDO: Insert

1
2
3
4
5
6
7
<?php
/* Prepared Statements for INSERT */

$stmt = $db->prepare("INSERT INTO table(field1,field2,field3,field4,field5) VALUES(:field1,:field2,:field3,:field4,:field5)");
$stmt->execute(array(':field1' => $field1, ':field2' => $field2, ':field3' => $field3, ':field4' => $field4, ':field5' => $field5));
$affected_rows = $stmt->rowCount();
?>
https://snipt.net/embed/14f5696754b9d3ef56930e5293602e48/
https://snipt.net/raw/14f5696754b9d3ef56930e5293602e48/
14f5696754b9d3ef56930e5293602e48
php
PHP
7
2016-12-08T06:16:26
True
False
False
Jan 11, 2013 at 04:22 AM
/api/public/snipt/53143/
pdo-insert
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a> <a href="#L-7">7</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="cp">&lt;?php</span> </span><span id="L-2"><a name="L-2"></a><span class="cm">/* Prepared Statements for INSERT */</span> </span><span id="L-3"><a name="L-3"></a> </span><span id="L-4"><a name="L-4"></a><span class="nv">$stmt</span> <span class="o">=</span> <span class="nv">$db</span><span class="o">-&gt;</span><span class="na">prepare</span><span class="p">(</span><span class="s2">&quot;INSERT INTO table(field1,field2,field3,field4,field5) VALUES(:field1,:field2,:field3,:field4,:field5)&quot;</span><span class="p">);</span> </span><span id="L-5"><a name="L-5"></a><span class="nv">$stmt</span><span class="o">-&gt;</span><span class="na">execute</span><span class="p">(</span><span class="k">array</span><span class="p">(</span><span class="s1">&#39;:field1&#39;</span> <span class="o">=&gt;</span> <span class="nv">$field1</span><span class="p">,</span> <span class="s1">&#39;:field2&#39;</span> <span class="o">=&gt;</span> <span class="nv">$field2</span><span class="p">,</span> <span class="s1">&#39;:field3&#39;</span> <span class="o">=&gt;</span> <span class="nv">$field3</span><span class="p">,</span> <span class="s1">&#39;:field4&#39;</span> <span class="o">=&gt;</span> <span class="nv">$field4</span><span class="p">,</span> <span class="s1">&#39;:field5&#39;</span> <span class="o">=&gt;</span> <span class="nv">$field5</span><span class="p">));</span> </span><span id="L-6"><a name="L-6"></a><span class="nv">$affected_rows</span> <span class="o">=</span> <span class="nv">$stmt</span><span class="o">-&gt;</span><span class="na">rowCount</span><span class="p">();</span> </span><span id="L-7"><a name="L-7"></a><span class="cp">?&gt;</span><span class="x"></span> </span></pre></div> </td></tr></table>
"prepared statements", PDO, insert, placeholders
Copyrighted, illegal, or inappropriate content? Email [email protected].