<?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>Webcodez - The database of web programming tutorials &#187; mysql_select_db</title>
	<atom:link href="http://www.webcodez.net/tag/mysql_select_db/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.webcodez.net</link>
	<description>Archive of tutorials on php,mysql,Javascript,html,css and other coding languages as well as code-snippets.</description>
	<lastBuildDate>Tue, 18 May 2010 16:43:49 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Creating a simple Blog System &#8211; Part 1</title>
		<link>http://www.webcodez.net/php-mysql/creating-a-simple-blog-system-part-1/</link>
		<comments>http://www.webcodez.net/php-mysql/creating-a-simple-blog-system-part-1/#comments</comments>
		<pubDate>Wed, 03 Mar 2010 18:56:44 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP & MySql]]></category>
		<category><![CDATA[Blog]]></category>
		<category><![CDATA[CMS]]></category>
		<category><![CDATA[functions]]></category>
		<category><![CDATA[mysql_connect]]></category>
		<category><![CDATA[mysql_select_db]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[project]]></category>

		<guid isPermaLink="false">http://www.webcodez.net/?p=435</guid>
		<description><![CDATA[Part 1 – Overview of Functions, Database &#38; Files
Overview
In this tutorial we’ll be creating a very simple blog system. We won’t be using OOP yet in this tutorial. For creating a Blog using OOP in PHP, another more advanced tutorial will be written and posted as well. The same goes for creating a more advance [...]]]></description>
			<content:encoded><![CDATA[<h2>Part 1 – Overview of Functions, Database &amp; Files</h2>
<p><h3>Overview</h3>
<p>In this tutorial we’ll be creating a very simple blog system. We won’t be using OOP yet in this tutorial. For creating a Blog using OOP in PHP, another more advanced tutorial will be written and posted as well. The same goes for creating a more advance CMS. However in this tutorial will just be creating a simple Blog system with php functions. Functions will be created for:</p>
<ul>
<li>Connecting to Host &amp; DB</li>
<li>Adding posts</li>
<li>Deleting posts</li>
<li>Adding replies</li>
<li>Deleting replies</li>
<li>Creating categories</li>
<li>Retrieving &amp; Displaying Posts</li>
<li>Add user</li>
<li>Edit user profile</li>
<li>Display user profile</li>
<li>Search</li>
</ul>
<p>Also a simple 2 rows div layout will be created with a side-menu and main content div. </p>
<p><h3>Database</h3>
<p>Let’s start with creating the database for our simple blog. We’ll call it ‘simple_blog’. However you can call it anything you like as long as you set it correctly in the script later on. Now let’s create the tables inside this database.</p>
<p><strong>Table: posts</strong></p>
<p>The fields that need to be created:</p>
<p>[TABLE=6]</p>
<p><strong>SQL:</strong></p>
<pre name="code" class="php:nogutter">
CREATE TABLE IF NOT EXISTS `posts` (
  `id` int(250) NOT NULL AUTO_INCREMENT,
  `title` varchar(50) NOT NULL,
  `author` int(250) NOT NULL,
  `message` longtext NOT NULL,
  `timestamp` int(250) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
</pre>
<p><strong>Table: replies</strong></p>
<p>[TABLE=9]</p>
<pre name="code" class="php:nogutter">
CREATE TABLE IF NOT EXISTS `replies` (
  `id` int(250) NOT NULL AUTO_INCREMENT,
  `postid` int(250) NOT NULL,
  `author` int(250) NOT NULL,
  `message` mediumtext NOT NULL,
  `timestamp` int(250) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
</pre>
<p><strong>Table: categories</strong></p>
<p>[TABLE=10]</p>
<pre name="code" class="php:nogutter">
CREATE TABLE IF NOT EXISTS `categories` (
  `id` int(250) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
</pre>
<p><strong>Table: members</strong></p>
<p>[TABLE=11]</p>
<pre name="code" class="php:nogutter">
CREATE TABLE IF NOT EXISTS `members` (
  `id` int(250) NOT NULL AUTO_INCREMENT,
  `username` varchar(50) NOT NULL,
  `password` varchar(50) NOT NULL,
  `email` varchar(250) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
</pre>
<p><h3>Files</h3>
<p>Now we’ve created our database, so let’s start with an overview of the files we’ll be creating now.</p>
<ul>
<li>functions.php</li>
<li>config.php</li>
<li>profile.php</li>
<li>index.php</li>
<li>post.php</li>
<li>	includes/header.php</li>
<li>	includes/footer.php</li>
<li>	includes/sidebar.php</li>
<li>admin/index.php</li>
<li>admin/functions.php</li>
</ul>
<p> </p>
<p>Let’s start with creating our function to connect  to the database we just made.</p>
<p><strong>File: functions.php</strong></p>
<pre name="code" class="php:nogutter">
&lt;?php

function connect($connection) {

  $host = $connection[‘host’];
  $user = $connection[‘user’];
  $pass = $connection[‘pass’];
  $db    = $connection[‘db’];
  $conn =  mysql_connect($host, $user, $pass);

  If(!$conn)
    die(“Couldn’t connect to host.”);

  $db = mysql_select_db($db);

  If(!$db)
     die(“Couldn’t connect to database.”);

}

?&gt;
</pre>
<p>Allright so first you see we set an argrument variable ‘$connection’ for the function. This variable should be given when calling the function and should contain all host &amp; database info required to connect to the host &amp; database. As you can see inside the function it seperates the sub-variables of the $connection variable into 4 new variables. These are for the host, user, password and database (db). As these are the data required to connect to the host &amp; database and should be set in an array $connection and given to this function with sub-variable ‘host’, ‘user’, ‘pass’ and ‘db’.</p>
<p>We’ll be offering the $connection variable to the function as an array. It will use the sub-variable named ‘host’,  ‘user’, ‘pass’ and ‘db’ to try to establish a connection to the host and database. So these we’ll need to set in our config.php file. We’ll shorten the name of the variable $connection to $conn. As the name of it doesn’t really matter as long as we give it to the function ‘connect’ when calling it.</p>
<p><strong>File: config.php</strong></p>
<pre name="code" class="php:nogutter">
&lt;?php

####CONNECTION CONFIGURATION###
$conn[‘host’] = “localhost”;        // database host (name/IP)
$conn[‘user’] = “root”;                // database host username
$conn[‘pass’] = “password”;      // database host password
$conn[‘db’]    = “simple_blog”; //database name

?&gt;
</pre>
<p>With this info our function ‘connect’ should be able to establish a connection to the host &amp; database.</p>
<p>We set each sub-variable for the $conn array. So we’ve got one variable ( array ) that contains all sub-variables, all info required for establishing a connection to the database. Which our function <strong>connect</strong> will accomplish.</p>
<p>Let’s include these files to the index file already.</p>
<p><strong>File: index.php</strong></p>
<pre name="code" class="php:nogutter">
&lt;?php

include(“functions.php”);

include(“config.php”);

?&gt;
</pre>
<p>We can already use our function to connect to the host &amp; database:</p>
<p><strong>File: index.php</strong></p>
<pre name="code" class="php:nogutter">
&lt;?php

include(“functions.php”);

include(“config.php”);

connect($conn);

?&gt;
</pre>
<p>We provide the array variable $conn to the function which contains all the sub-variables data of host &amp; database ( as we set it in config.php ) required for establishing a connection.</p>
<p><h3>End of part 1</h3>
<p>That’s it so far! In this part we’ve createn the structure of the script for both files, functions &amp; database purpose. And also we’ve made our first function to establish a connection to the database &amp; host using the configurations for the  connection set in our config.php file we created. In the second part we’ll be creating a basic CSS, Div based 2 columns layout. With a side-bar menu and a main content area where all posts will be appearing. Hope to see you in the next part!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.webcodez.net/php-mysql/creating-a-simple-blog-system-part-1/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>MySql Basics &#8211; Connect</title>
		<link>http://www.webcodez.net/php-mysql/mysql-basics-connect/</link>
		<comments>http://www.webcodez.net/php-mysql/mysql-basics-connect/#comments</comments>
		<pubDate>Tue, 19 Jan 2010 11:17:17 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP & MySql]]></category>
		<category><![CDATA[basics]]></category>
		<category><![CDATA[Connect]]></category>
		<category><![CDATA[fundamentals]]></category>
		<category><![CDATA[MySql]]></category>
		<category><![CDATA[mysql_connect]]></category>
		<category><![CDATA[mysql_select_db]]></category>

		<guid isPermaLink="false">http://www.webcodez.net/?p=153</guid>
		<description><![CDATA[MySql can be used to interact with databases: insert and retrieve data. It can come very handy when you need to save a collection of the same &#8216;kind&#8217; ( such as products or forum messages, user accounts, etc. ) and which need to be able to be edited easily. To interact with the database you [...]]]></description>
			<content:encoded><![CDATA[<p>MySql can be used to interact with databases: insert and retrieve data. It can come very handy when you need to save a collection of the same &#8216;kind&#8217; ( such as products or forum messages, user accounts, etc. ) and which need to be able to be edited easily. To interact with the database you first need to connect to the host and then select the database you want to interact with. In MySql you can use the functions mysql_connect and mysql_select_db to do so.</p>
<pre name="code" class="php:nogutter">
mysql_connect("host", "username", "password");
mysql_select_db("database");
</pre>
<p>mysql_connect is used to connect to the host, which requires a username and a password. When the user for the host requires no password, you can leave out the password. The host is usually just &#8220;localhost&#8221;, depending on your host (settings). For the username you can often use &#8220;root&#8221; as default username if there aren&#8217;t any other users made for the host.</p>
<p>mysql_select_db is pretty much straight forward I guess. You make it select the database you&#8217;re going to use. You can also use the or die(&#8220;error message here&#8221;) to make it show an error when it fails to connect. There&#8217;s a standard mysql error message which can be called by using mysql_error() function.So it&#8217;d be:</p>
<pre name="code" class="php:nogutter">
mysql_connect("localhost", "root", "mypassword")or die(mysql_error());
mysql_select_db("mydatabase")or die(mysql_error());
</pre>
<p>So when any error occurs while connecting to the host/database, the mysql error message will appear containing the error occured.</p>
<p>You can also manually check whether the connection was successfully by putitng them into variables and checking the variables:</p>
<pre name="code" class="php:nogutter">
$connect = mysql_connect("localhost", "root", "mypassword");
$db      = mysql_select_db("mydatabase");

if(!$connect) {

     die("Host connection failed");

}

if(!$db) {

    die("Database connection failed");

}

?>
</pre>
<p>Now you&#8217;re ready to use the mysql functions to interact with the database, such as mysql_query, which we&#8217;ll be discussing in the next chapter.</p>
<p>Admin.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.webcodez.net/php-mysql/mysql-basics-connect/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
