Lesson: Return values from a MySQL Table using PHP
PHP Survival #1
-------------------------------
So first of all, you're going to want to create a .php document. This can be achieved by either editing your registry on windows, creating a file on Linux and adding the php extension, or Finder > New file on OSX, and also adding the php extension.
Another way to achieve this on windows is to create a .txt and rename it or save it as index.php
Now you're going to want to create a quick MySQL database, you can design your own, or you can use my premade one:
Execute the above code if you wish to use my premade (recommended)
--------------------------
Part 2: The PHP
--------------------------
Okay - so with your made PHP, put the following in:
What the above code does is connect to your database.
Okay, so now that your connecting to the database, we can start to bring in some code to show the contents of the table.
That should display the contents of the database, in format like:
If this has worked, congratulations!
If not - post below your problems and i'll help you to fix them.
PHP Survival #1
-------------------------------
So first of all, you're going to want to create a .php document. This can be achieved by either editing your registry on windows, creating a file on Linux and adding the php extension, or Finder > New file on OSX, and also adding the php extension.
Another way to achieve this on windows is to create a .txt and rename it or save it as index.php
Now you're going to want to create a quick MySQL database, you can design your own, or you can use my premade one:
Code:
-- phpMyAdmin SQL Dump
-- version 2.10.1
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Dec 15, 2010 at 08:56 AM
-- Server version: 5.0.41
-- PHP Version: 5.2.2
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
--
-- Database: `premade`
--
-- --------------------------------------------------------
--
-- Table structure for table `table1`
--
CREATE TABLE `table1` (
`id` int(3) NOT NULL auto_increment,
`content` text collate latin1_general_ci NOT NULL,
UNIQUE KEY `id` (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=2 ;
--
-- Dumping data for table `table1`
--
INSERT INTO `table1` VALUES (1, 'o look content lul');
Execute the above code if you wish to use my premade (recommended)
--------------------------
Part 2: The PHP
--------------------------
Okay - so with your made PHP, put the following in:
PHP:
$host = "localhost";
$pass = "";
$db = "premade";
$user = "root";
$con = mysql_connect($host, $user, $pass);
$db = mysql_select_db($db);
What the above code does is connect to your database.
Okay, so now that your connecting to the database, we can start to bring in some code to show the contents of the table.
PHP:
$display = mysql_query("SELECT * FROM table1 ORDER by id DESC");
while($row = mysql_fetch_array($display, MYSQL_ASSOC))
{
echo $row['content'];
}
That should display the contents of the database, in format like:
Code:
o look content lul
If this has worked, congratulations!
If not - post below your problems and i'll help you to fix them.