Online computer courses, code, programming tutorial and sidebar information for monitoring Canadian S&P/TSX index. Build friendship and networking. Welcome to visit my blogs often!!! I also have two other sites: YouTube Channel and Google site.
Adsense
Popular Posts
- PHPWind-- A PHP forum script applcaition in China
- How to blend adsense inside your post?
- Formatting my post
- Notepad++ - Add C++ compiler
- Install PHPMailer 5.2.4 and use smtp gmail
- Wayback Machine - see archived versions of web pages across time
- Set up a child account and set screen time limit in Windows 8
- phpexcel toggle expand and hide column in EXCEL and summary
- Install PHP ibm_db2 extension in Linux (redHat)
- PHP: add a download as pdf file button in report page
Thursday, May 30, 2013
Get data from MySQL in PHP
To get data from MySQl is a base for PHP programming.
First we create a table in XAMPP: (go to http://localhost/phpmyadmin/
or http://localhost/xampp if you installed XAMPP)
CREATE TABLE income
(
username VARCHAR(20) NOT NULL,
income_type CHAR(1) NOT NULL,
income DECIMAL(18,2)NOT NULL
) ENGINE = MYISAM;
Then insert some data:
INSERT INTO income(username,income_type,income) VALUES
('j1','A','2000.0' ),
('j2','B','2050.5' ),
('j3','A', '300.0'),
('j4','B', '400.0');
Now we fetch this 2d array data using PHP:
<?php
//Connect to MySQL server
$conn = @mysql_connect("localhost","root","") || die("MySQL connection error");
//Connect to database test
@mysql_select_db("test") || die("test database not exist!");
$income_data = Array();
$sql = 'SELECT * FROM income';
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result)){
// Assign MySQL result to a multi-dimensional array
$income_data[] = $row;
}
//Extract data from the multi-dimensional array
echo '<h2>';
echo 'username, income_type, income <br />';
foreach($income_data as $my_data){
foreach($my_data as $key=>$data) echo $data.', ';
echo '<br />';
}
echo '</h2>';
?>
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment