PHP Read Database display problem

Source: Internet
Author: User
Tags php mysql
Database PHP MySQL

I would like to ask you, my database data is as follows:-
--The structure of the table ' 3g_district '
--

CREATE TABLE IF not EXISTS ' 3g_district ' (
' d_id ' mediumint (8) unsigned not NULL auto_increment,
' D_name ' char (255) is not NULL,
' D_level ' tinyint (4) unsigned not NULL DEFAULT ' 0 ',
' D_pid ' mediumint (8) unsigned not NULL DEFAULT ' 0 ',
' D_list ' smallint (6) Not NULL DEFAULT ' 0 ',
PRIMARY KEY (' d_id '),
KEY ' upid ' (' d_pid ', ' d_list ')
) Engine=innodb DEFAULT Charset=utf8 auto_increment=45052;

--
--Dump the data in the table ' 3g_district '
--

INSERT into ' 3g_district ' (' d_id ', ' d_name ', ' d_level ', ' d_pid ', ' d_list ') VALUES
(1, ' Beijing ', 1, 0, 0),
(2, ' Tianjin ', 1, 0, 0),
(3, ' Hebei province ', 1, 0, 0),
(4, ' Shanxi Province ', 1, 0, 0),
(5, ' Inner Mongolia Autonomous Region ', 1, 0, 0),
(6, ' Liaoning Province ', 1, 0, 0),
(7, ' Jilin Province ', 1, 0, 0),
(8, ' Heilongjiang province ', 1, 0, 0),
(9, ' Shanghai City ', 1, 0, 0),
(10, ' Jiangsu Province ', 1, 0, 0),
(11, ' Zhejiang province ', 1, 0, 0),
(12, ' Anhui Province ', 1, 0, 0),
(13, ' Fujian province ', 1, 0, 0),
(14, ' Jiangxi Province ', 1, 0, 0),
(15, ' Shandong Province ', 1, 0, 0),
(16, ' Henan province ', 1, 0, 0),
(17, ' Hubei Province ', 1, 0, 0),
(18, ' Hunan Province ', 1, 0, 0),
(19, ' Guangdong Province ', 1, 0, 0),
(20, ' Guangxi Zhuang Autonomous Region ', 1, 0, 0),
(21, ' Hainan Province ', 1, 0, 0),
(22, ' Chongqing ', 1, 0, 0),
(23, ' Sichuan Province ', 1, 0, 0),


My conn.php file is as follows:


$mysql _server_name= "localhost"; Database server name
$mysql _username= "root"; Connect to database user name
$mysql _password= ""; Connect Database Password
$mysql _database= "Teafea"; The name of the database

Connecting to a database
$conn =mysql_connect ($mysql _server_name, $mysql _username, $mysql _password);

Using UTF8 encoding
mysql_query ("Set names ' UTF8 '");

SQL statement that extracts information from a table
$str = "Select D_id,d_name from 3g_district";

Execute SQL query
mysql_select_db ($mysql _database, $conn);
$res =mysql_query ($str, $conn);

Get query Results
$row =mysql_fetch_row ($res);

Navigate to the first record
Mysql_data_seek ($res, 0);

Loop out Records
while ($row =mysql_fetch_row ($res)) {

for ($i =0; $i

Echo Json_encode ("$row [$i]");
}
}

The effect I want is to get it out of the database to show the effect:
D_id:d_name,d_id:d_name,
That is 1: Beijing, 2: Tianjin, 3: Hebei Province
Such an effect
Ask you this effect conn.php file how to write it? The middle colon: and the single quotation mark, how to add to it?


Reply to discussion (solution)

Loop out Records
$res = Array ();
while ($row =mysql_fetch_row ($res)) {
$res [] = Join (': ', $row);
}
echo Join (', ', $res);

The following error has been tested:
Warning:mysql_fetch_row () expects parameter 1 to BES resource, array given in E:\wamp\www\20\conn.php on line 29

29 Rows is the while ($row =mysql_fetch_row ($res)) {This line,

How can i solve it?

$conn =mysql_connect (' localhost ', ' root ', ' Write your Database password ');
$str = "Select D_name from 3g_district"; The direct search name,id is self-growing, and you can display the ID when it is displayed.
mysql_select_db ($TR, $conn);

Warning:mysql_fetch_row () expects parameter 1 to BES resource, array given in E
Say $res is an array, how possible. Are you sure the code you posted is up-to-date?

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.