[TUT] Return values from a MySQL Table using PHP [PHP Survival #1]

Status
Not open for further replies.

Jonty

Active Member
Aug 7, 2010
194
65
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:

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.
 

Kryptos

prjRev.com
Jul 21, 2010
2,205
1,252
Very simple, I actually use this very often. Example:
Code:
$query = mysql_query("SELECT * FROM users ORDER by id DESC");

while($row = mysql_fetch_array($query, mysql_assoc)) {
echo $row['username'];
echo $row['passaword']; }
 

Jonty

Active Member
Aug 7, 2010
194
65
why would you want to order your users, oh, are you displaying them in some sort of "admin cp -> list of users" thing? :D
 

Kryptos

prjRev.com
Jul 21, 2010
2,205
1,252
Nah, it was really an example. It could be used for that, I use it to display news titles, etc tbh. ;D
 
Nov 23, 2010
307
2
very nice tut ;)! i remember when i was noob and learning those , anyway instead of writing all that :
PHP:
-- 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');
you can write only :
PHP:
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 ;
INSERT INTO `table1` VALUES (1, 'o look content lul');
and it does have the same function!

also , make things more specific , on what does that do , how it works etc. because that tut is copy/pasting , anyway thanks for sharing!!
 
Status
Not open for further replies.

Users who are viewing this thread

Top