<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>PHP Tutorials By Kloplop321 &#187; create database</title>
	<atom:link href="http://kloplop321.com/php-tutorials/index.php/tag/create-database/feed/" rel="self" type="application/rss+xml" />
	<link>http://kloplop321.com/php-tutorials</link>
	<description>PHP video tutorials, for everyone.</description>
	<lastBuildDate>Sun, 20 Mar 2011 19:03:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2</generator>
		<item>
		<title>PHP Tutorial: Databases in general</title>
		<link>http://kloplop321.com/php-tutorials/index.php/2010/02/14/php-tutorial-databases-in-general/</link>
		<comments>http://kloplop321.com/php-tutorials/index.php/2010/02/14/php-tutorial-databases-in-general/#comments</comments>
		<pubDate>Sun, 14 Feb 2010 08:00:16 +0000</pubDate>
		<dc:creator>Kloplop321</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHP Tutorials]]></category>
		<category><![CDATA[PHPMyAdmin]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[create database]]></category>
		<category><![CDATA[create table]]></category>
		<category><![CDATA[insert]]></category>
		<category><![CDATA[sqlite]]></category>
		<category><![CDATA[update]]></category>

		<guid isPermaLink="false">http://kloplop321.com/php-tutorials/?p=174</guid>
		<description><![CDATA[Developers often use the handy features included with databases to create solutions in PHP for what ever needs to be satisfied. Databases have functions like quickly matching data to Identifications, searching large sums of data, holding user information and much more that developers can utilize. There are many ways to use a database, or in [...]]]></description>
			<content:encoded><![CDATA[<p>Developers often use the handy features included with databases to create solutions in PHP for what ever needs to be satisfied. Databases have functions like quickly matching data to Identifications, searching large sums of data, holding user information and much more that developers can utilize. There are many ways to use a database, or in this case, to interface with a database. Most databases use a rough standard of a <a title="SQL full description" href="http://en.wikipedia.org/wiki/SQL" target="_blank">Structured Querying Language</a>, which is like code that the SQL server or application interprets. <span id="more-174"></span>In PHP, developers have the following options to interface with a database:</p>
<ul>
<li><a href="http://www.php.net/manual/en/book.dbase.php">dBase</a></li>
<li><a href="http://www.php.net/manual/en/book.dbplus.php">DB++</a></li>
<li><a href="http://www.php.net/manual/en/book.fbsql.php">FrontBase</a></li>
<li><a href="http://www.php.net/manual/en/book.filepro.php">filePro</a></li>
<li><a href="http://www.php.net/manual/en/book.ibase.php">Firebird/InterBase</a></li>
<li><a href="http://www.php.net/manual/en/book.ifx.php">Informix</a></li>
<li><a href="http://www.php.net/manual/en/book.ibm-db2.php">IBM DB2</a></li>
<li><a href="http://www.php.net/manual/en/book.ingres.php">Ingres</a></li>
<li><a href="http://www.php.net/manual/en/book.maxdb.php">MaxDB</a></li>
<li><a href="http://www.php.net/manual/en/book.mongo.php">Mongo</a></li>
<li><a href="http://www.php.net/manual/en/book.msql.php">mSQL</a></li>
<li><a href="http://www.php.net/manual/en/book.mssql.php">Mssql</a></li>
<li><a href="http://www.php.net/manual/en/book.mysql.php">MySQL</a></li>
<li><a href="http://www.php.net/manual/en/book.mysqli.php">Mysqli</a></li>
<li><a href="http://www.php.net/manual/en/book.mysqlnd.php">Mysqlnd</a></li>
<li><a href="http://www.php.net/manual/en/book.oci8.php">OCI8</a></li>
<li><a href="http://www.php.net/manual/en/book.ovrimos.php">Ovrimos SQL</a></li>
<li><a href="http://www.php.net/manual/en/book.paradox.php">Paradox</a></li>
<li><a href="http://www.php.net/manual/en/book.pgsql.php">PostgreSQL</a></li>
<li><a href="http://www.php.net/manual/en/book.sqlite.php">SQLite</a></li>
<li><a href="http://www.php.net/manual/en/book.sqlite3.php">SQLite3</a></li>
<li><a href="http://www.php.net/manual/en/book.sybase.php">Sybase</a></li>
<li><a href="http://www.php.net/manual/en/book.tokyo-tyrant.php">tokyo_ tyrant</a></li>
</ul>
<p>Some of these methods use the same database type, such as MySQL and Mysqli. However I will only be focusing on the most common methods, which incude MySQL, SQLite, and OBCD.</p>
<p>Most PHP developers use MySQL due to its support, popularity, cross-platform, and universal capabilities. It is not proprietary like Microsoft Access Databases, and it has a really handy tool called PHPMyAdmin.</p>
<p>SQL looks like this</p>
<p style="padding-left: 30px;">INSERT INTO My_table<br />
(field1, field2, field3)<br />
VALUES<br />
(&#8216;test&#8217;, &#8216;N&#8217;, NULL);</p>
<p>In PHP, we can wrap this in double quotes like so</p>
<pre>$sql = "INSERT INTO My_table
 (field1, field2, field3)
 VALUES
 ('test', 'N', NULL);";</pre>
<p>and then execute it with what ever database method we choose.</p>
<blockquote><p>MySQL:<br />
mysql_query($sql);<br />
SQLite:<br />
sqlite_query($db,$sql);<br />
OBCD:<br />
odbc_exec($db, $sql);</p></blockquote>
<p>As I have only had personal experience with two of the above, I will not cover OBCD any more than the above.</p>
<p>Back to MySQL and SQLite:<br />
Both MySQL and SQLite require an initiation at the beginning of each script execution. In MySQL&#8217;s case an example would be:</p>
<blockquote>
<div>
<div><code> &lt;?php<br />
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');<br />
if (!$link) {<br />
die('Could not connect: ' . mysql_error());<br />
}<br />
echo 'Connected successfully';<br />
mysql_close($link);<br />
?&gt; </code></div>
</div>
</blockquote>
<div>The mysql_connect function is what initiates MySQL for use in future code execution. The $link variable in this example can be used in almost all mysql_* functions in the case of multiple connections (Which I do not recommend). If a developer wants to manually close the connection, they may do so by using mysql_close() which terminates the connection and frees the MySQL server&#8217;s resources.</div>
<div>For SQLite, we are using a local file instead of a server. Essentially, PHP, or rather the SQLite plugin for PHP handles the SQL execution and data manipulation. This is a great solution if a developer wants to make backups easily through more direct measures. Also, if a developer is in a situation like I am in with my school&#8217;s server, then this is a great solution. If you don&#8217;t care to understand my situation, skip to the next paragraph. My situation was where the Web Hoster (will remain un-named, for they do not deserve to be advertised) had it so there was no &#8220;localhost&#8221; mysql connection, but rather a single server dedicated for MySQL execution. Great idea in theory if you want to make use of idle hardware, but in reality it is horrid. My pages were stuck with a 2(rare) to 40 second (most common in the middle) delay because it had to wait to connect to the central server. This obviously would not do, so I tried to find a file-based solution without having to make my own with CSV and other hacked-up jobs. The result: instant execution, no noticeable delay.</div>
<div>SQLite, as aforementioned, uses a file as the database. SQLite has both its pros and cons.</div>
<div>Pros:</div>
<div>
<ul>
<li>Fast</li>
<li>Simple</li>
<li>Not heavy on server load</li>
<li>Easy to copy</li>
</ul>
</div>
<div>Cons:</div>
<div>
<ul>
<li>People from the &#8220;other side&#8221; can download it if you put it in a public folder</li>
<li>It can be erased without a trace</li>
<li>it isn&#8217;t as flexible as MySQL</li>
</ul>
</div>
<div>I prefer to MySQL because of PHPMyAdmin, but I can deal with SQLite thanks to this conversion code I came up with (Not bi-directional, only MySQL to SQLite)</div>
<blockquote>
<div>&lt;?php<br />
$link = mysql_connect(&#8216;localhost&#8217;, &#8216;USER&#8217;, &#8216;PASS&#8217;);<br />
mysql_selectdb(&#8220;DB_NAME&#8221;);<br />
$name = &#8220;TABLE_NAME&#8221;;<br />
$sql = &#8220;SELECT *<br />
FROM `$name` LIMIT 1&#8243;;<br />
$res = mysql_query($sql);<br />
$row = mysql_fetch_array($res);<br />
$cols = mysql_num_fields($res);<br />
$sql = &#8220;SELECT *<br />
FROM `$name`&#8221;;<br />
$res = mysql_query($sql);<br />
$rowsdone = 0;<br />
if ($db = sqlite_open(&#8216;SQLITE_FILE_LOCATION&#8217;, 0666, $sqliteerror)) {<br />
$res = mysql_query($sql);<br />
while($row = mysql_fetch_array($res)){<br />
$sqli = &#8220;INSERT INTO $name VALUES(&#8220;;<br />
for($x = 0; $x &lt; $cols; $x++){<br />
$sqli .= &#8220;&#8216;&#8221;.sqlite_escape_string($row[$x]).&#8221;&#8216;&#8221;;<br />
if($x != $cols -1){<br />
$sqli .= &#8221; , &#8220;;<br />
}<br />
}<br />
$sqli .= &#8220;);&#8221;;<br />
$result = sqlite_query($db,$sqli);<br />
if(sqlite_last_error($db) != 1){<br />
echo sqlite_error_string(sqlite_last_error($db)).&#8221;\n&#8221;;<br />
//echo strip_tags($sqli);<br />
echo &#8220;&lt;br&gt;\n &#8220;;<br />
}<br />
}<br />
} else {<br />
die ($sqliteerror);<br />
}<br />
?&gt;</div>
</blockquote>
<div>The above script is only for transferring rows not for adding the tables to SQLite.</div>
<div>As seen above, in order to &#8220;connect&#8221; or open the SQLite Database, we use the line:</div>
<blockquote>
<div>if ($db = sqlite_open(&#8216;SQLITE_FILE_LOCATION&#8217;, 0666, $sqliteerror)) {</div>
</blockquote>
<div>0666 being the permissions code (no reference to satanic anything).</div>
<div>One note is that in MySQL, a developer would use mysql_real_escape_string to prepare strings for data entry, however in SQLite, a developer would use sqlite_escape_string (not the missing &#8220;real_&#8221; as there is no function named that).</div>
<div>A developer can use MySQL SQL in SQLite with a few adaptations:</div>
<div>A developer may not:</div>
<div>
<ul>
<li>use ` in the SQL</li>
<li>use fieldnames that also are keywords like order, by, count, etc&#8230;</li>
</ul>
<p>(I doubt that my suggestion is full and complete)</p>
</div>
<div>My method is to make everything through PHPMyAdmin, export it without comments, and use the table creation section in SQLite.</div>
<div>SQLite functions and MySQL functions are nearly interchangeable with just a text replacement and SQL checking, so I will from now on only cover MySQL (with possible exceptions).</div>
<div>A developer can make a database in MySQL with &#8220;<code>CREATE  DATABASE  `DB_NAME_HERE` ;" and then in php select that database for future use through</code></div>
<blockquote>
<div>mysql_selectdb(&#8220;<code>DB_NAME_HERE</code>&#8220;);</div>
</blockquote>
<div>Next, if a developer so desire, he may create a table with SQL like the following</div>
<blockquote>
<div><code>CREATE  TABLE  `</code><code>DB_NAME_HERE</code><code>`.`tablename` (</code></p>
<div>`id` INT( 8  )  UNSIGNED NOT  NULL  AUTO_INCREMENT ,<br />
`description` TEXT NOT  NULL ,<br />
PRIMARY  KEY (  `id`  ) ,<br />
INDEX (  `id`  )</div>
</div>
<div>) ENGINE  =  MYISAM ;</div>
</blockquote>
<div>Now that this intangible developer has a table, he can put information in like so:</div>
<blockquote>
<div><code>INSERT  INTO  `</code><code>DB_NAME_HERE</code><code>`.`tablename` (</code></p>
<div>`id` ,<br />
`description`</div>
<p>)<br />
VALUES (</p>
<div>NULL ,  &#8216;Beans do not taste like fruit.&#8217;</div>
</div>
<div>);</div>
</blockquote>
<div>A developer can use NULL as a value when the field has a default value or has an auto_increment property (like above).</div>
<div>Because I have set the id to be a primary key and index, there can be no duplicate keys(or identifications) in this table. Any developer who tries to force it will get an error.</div>
<div>If a developer is having an error in his code, he may use</div>
<blockquote>
<div><code>echo </code><code>mysql_error</code><code>();</code></div>
</blockquote>
<div>to find out what is wrong. In SQLite&#8217;s case he would need to use</div>
<blockquote>
<div>echo sqlite_error_string(sqlite_last_error($db));</div>
</blockquote>
<div>to find out what is wrong. In SQLite it isn&#8217;t as descriptive, so it takes more effort to find out what the issue is.</div>
<div>Next, in order to get information, a developer can do</div>
<blockquote>
<div>$sql = &#8220;SELECT  * FROM `TABLE_NAME`&#8221;;</div>
<div>$result = mysql_query($sql);</div>
</blockquote>
<div>and there are various ways to get the information like</div>
<blockquote>
<div>while($row = mysql_fetch_array($result)){</div>
<div>while($row = mysql_fetch_assoc($result)){</div>
</blockquote>
<div>Either may be used, but I prefer array to assoc.</div>
<div>Next, the developer can get the information row by row by using the variable $row in this case</div>
<blockquote>
<div>while($row = mysql_fetch_array($result)){</div>
<div>print_r($row);</div>
<div>}</div>
</blockquote>
<div>The above code will print out the array that the mysql_fetch_array function returns for every row that is in the $result.</div>
<div>A Developer may also update a row by referring to an identification in the SQL as such</div>
<blockquote>
<div><code>UPDATE  `DB_NAME`.`tablename`  SET  `description`  =   'Beans do not taste  like fruit. Yet they are supposed to be magical...' WHERE  `tablename`.`id`  =1;</code></div>
</blockquote>
<div>MySQL can also be compounded in terms of having multiple inserts (not selects) and updates, but they must be separated by a &#8220;;&#8221; and usually a new line.</div>
<div></div>
<div>If a developer would like to know if his current php interpreter has database connection functionality, use phpinfo(); and go to the page in the browser and search for &#8220;sql&#8221;. In my case, I have &#8220;&#8211;with-mysql&#8221; and &#8220;&#8211;with-sqlite&#8221; in my &#8220;Configure Command&#8221; section near the top.</div>
<div></div>
<div>This is only an introduction to the core and most useful features of Databases using Sequential Querying Languages. Further reading can be done at <a href="http://www.mysql.com/" target="_blank">mysql.com</a> <a href="http://php.net/manual/en/book.mysql.php">php mysql</a> <a href="http://www.sqlite.org/">sqlite.org</a> and <a href="http://php.net/manual/en/book.sqlite.php">php sqlite</a>.</div>
]]></content:encoded>
			<wfw:commentRss>http://kloplop321.com/php-tutorials/index.php/2010/02/14/php-tutorial-databases-in-general/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

