MySQL Forums
Forum List  »  Microsoft SQL Server

Selecting data From mysql
Posted by: Mark Den
Date: February 18, 2012 07:15AM

Syntax

// Select all columns from all rows.
"SELECT * FROM table_name";
or
// Select some column from all rows.
"SELECT column_name1, column_name2 FROM table_name";
or
// Select all coulumns from one row.
"SELECT * FROM table_name WHERE column_name=' value in column '";

Overview

In this tutorial create 1 file
1. select.php

Step
1. Create table "test_mysql" in database "test".
2. Create file select.php.
3. test it!

If you don't want looping rows in mysql, replace
while($rows=mysql_fetch_array($result)){
........
<?php
}
mysql_close();
?>

replace with this
$rows=mysql_fetch_array($result);
.........
<?php
mysql_close();
?>


Create table "test_mysql"


CREATE TABLE `test_mysql` (
`id` int(4) NOT NULL auto_increment,
`name` varchar(65) NOT NULL default '',
`lastname` varchar(65) NOT NULL default '',
`email` varchar(65) NOT NULL default '',
PRIMARY KEY (`id`)
) TYPE=MyISAM AUTO_INCREMENT=7 ;

--
-- Dumping data for table `test_mysql`
--

INSERT INTO `test_mysql` VALUES (1, 'Billly', 'Blueton', 'bb5@phpeasystep.com');
INSERT INTO `test_mysql` VALUES (2, 'Jame', 'Campbell', 'jame@somewhere.com');
INSERT INTO `test_mysql` VALUES (3, 'Mark', 'Jackson', 'mark@phpeasystep.com');
INSERT INTO `test_mysql` VALUES (4, 'Linda', 'Travor', 'lin65@phpeasystep.com');
INSERT INTO `test_mysql` VALUES (5, 'Joey', 'Ford', 'fordloi@somewhere.com');
INSERT INTO `test_mysql` VALUES (6, 'Sidney', 'Gibson', 'gibson@phpeasystep.com');
############### Code

<?php

$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="test_mysql"; // Table name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// Retrieve data from database
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);

// Start looping rows in mysql database.
while($rows=mysql_fetch_array($result)){
?>
<table width="400" border="1" cellspacing="0" cellpadding="3">
<tr>
<td width="10%"><? echo $rows['id']; ?></td>
<td width="30%"><? echo $rows['name']; ?></td>
<td width="30%"><? echo $rows['lastname']; ?></td>
<td width="30%"><? echo $rows['email']; ?></td>
</tr>
</table>

<?
// close while loop
}

// close connection
mysql_close();
?>



Edited 1 time(s). Last edit at 04/10/2012 03:56AM by Mark Den.

Options: ReplyQuote


Subject
Written By
Posted
Selecting data From mysql
February 18, 2012 07:15AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.