search
HomeComputer TutorialsComputer KnowledgeWrite a program in PHP to connect to the database and output all records of the table

Write a program in PHP to connect to the database and output all records of the table

PHP writing program can connect to the database and output all the records in the table to set the data

mysql_connect('localhost','root','') or die("Connection error"); //Link database

mysql_select_db('database') or die("Fail to select database.");//Select database

$sql="select * from `mytable`";

$result=mysql_query($sql);

echo '

';

while($row=mysql_fetch_array($result))

{

echo '

';

foreach($row as $rw){

echo "

";

}

echo '

';

}

echo '

".$rw."
';

?>

How does native php output the last executed sql of mysql

method one:

$link = mysql_connect("192.168.1.1", "myuser", "mypass"); --Connect to the database

if(FALSE == $link)

{

echo "Connect database failed!";

}

$query =

"select * from a order by id";

mysql_select_db('test', $link);--Select database

$result = mysql_query($query);--Execute query statement

if(FALSE == $result)

{

echo "Querry failed!";

}

$i = 0;

$j = 0;

while($i {

$meta_c=0;

if($meta_c=mysql_fetch_row($result))--Get the result set of each row

{

while($j {

echo $meta_c[$j;

}

echo "

";

} // while;

$j=0;

}

mysql_free_result($result);--Release the result set

mysql_close($link);--Close the connection

?>

Method Two:

$link = mysql_connect("192.168.1.1", "myuser", "mypass"); --Connect to the database

if(FALSE == $link)

{

echo "Connect database failed!

";

}

mysql_select_db('test', $link);--Select database

$result = mysql_query("select * from a order by id");--Execute the query statement

if(FALSE == $result)

{

echo "Querry failed!";

}

while($row = mysql_fetch_array($result, MYSQL_NUM))--Get the value in the array, because the result set is stored in a two-dimensional array

{

foreach ($row as $col_value)--take values ​​one by one

{

echo " $col_value ";

}

}

mysql_free_result($result);--Release the result set

mysql_close($link);)--Close the connection

?>

The above is the detailed content of Write a program in PHP to connect to the database and output all records of the table. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:Excel办公网. If there is any infringement, please contact admin@php.cn delete
Difference Between RAID Recovery and Hard Drive RecoveryDifference Between RAID Recovery and Hard Drive RecoveryApr 17, 2025 am 12:50 AM

Data recovery is always a heated topic. To successfully restore data from your device, you should know how it stores data. You can learn the difference between RAID recovery and hard drive recovery from this php.cn post.

Microsoft Word Deleting Text Windows 10/11? Fix It Now!Microsoft Word Deleting Text Windows 10/11? Fix It Now!Apr 17, 2025 am 12:48 AM

Do you run into Word text disappearing while typing? Some letters or even paragraphs might disappear from your document. What’s wrong with it? In this post from php.cn Website, we will take a closer look at the Word automatically deleting text issue

How to Fix the LC-208 Error Code on Overwatch 2? Here Is a Guide! - MiniToolHow to Fix the LC-208 Error Code on Overwatch 2? Here Is a Guide! - MiniToolApr 17, 2025 am 12:47 AM

When you are trying to log into Overwatch 2, you may receive the LC-208 error message and it prevents your game from connecting to the game servers. This post from php.cn introduces how to fix the LC-208 Overwatch error.

Google Drive Disappeared From File Explorer - Easy ApprochesGoogle Drive Disappeared From File Explorer - Easy ApprochesApr 17, 2025 am 12:46 AM

Generally, you can quickly access and edit Google Drive files from File Explorer. However, sometimes you may encounter the issue of “Google Drive disappeared from File Explorer”. Here this post on php.cn tells you how to get Google Drive to show up i

How to Fix Windows 7 Welcome Screen Slow? Tips Are Here!How to Fix Windows 7 Welcome Screen Slow? Tips Are Here!Apr 17, 2025 am 12:45 AM

Why is my computer on the welcome screen for so long? How to fix Windows 7 welcome screen slow? If you still run Windows 7 on your PC and meet this issue, you are in the right place and multiple solutions will be given here by php.cn.

How to Open Credential Manager in Windows 11? 6 Ways for You! - MiniToolHow to Open Credential Manager in Windows 11? 6 Ways for You! - MiniToolApr 17, 2025 am 12:44 AM

Want to use the built-in password management tool – Credential Manager to manage your login credentials for web and apps? How can you open Credential Manager in Windows 11? In this post, php.cn collected multiple ways to access this utility, and let’

How to Enable/Disable Periodic Scanning on Microsoft Defender - MiniToolHow to Enable/Disable Periodic Scanning on Microsoft Defender - MiniToolApr 17, 2025 am 12:43 AM

Microsft Defender Antivirus has a feature called Periodic scanning, which can be enabled when you have installed another antivirus product on a Windows 11/10 device. Now, this post from php.cn teaches you how to enable/disable Periodic scanning on Wi

Smart Techniques for Excel Hyperlinks Not Working on WinSmart Techniques for Excel Hyperlinks Not Working on WinApr 17, 2025 am 12:42 AM

Have you ever encountered the issue of “Excel hyperlinks not working”? Do you know how to deal with it? In this post from php.cn, you can get several feasible solutions to get rid of this problem. And you will know a professional data recovery tool t

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft