在 PHP 中使用表

发布于 2024-11-14 17:16:50 字数 1357 浏览 3 评论 0原文

如果这个问题让任​​何人感到沮丧,我很抱歉...我确实是 PHP + MYSQL 的初学者,但我很乐意在这方面取得一些进展!

我的目标:

  1. 创建一个可以在网页中查看的表格。
  2. 能够将数据(行)添加到该表中,并且在下次加载页面时数据仍然存在。

步骤 1:创建表

<html>
<body>

<?php

// Conect to MYSQL
$con = mysql_connect("localhost", "My_Username", "My_Password") or die(mysql_error());
echo "Connected to MYSQL </br>";

mysql_select_db("My_Database") or die(mysql_error());
echo "Connected to Database";

// Create a MySQL table in the selected database (called ExampleTable)

$sql = "CREATE TABLE ExampleTable
 (
// Set the primary key (personID)
 personID int NOT NULL AUTO_INCREMENT, 
 PRIMARY KEY(personID),
 ColumnOne varchar(15)
 ColumnTwo varchar(15)
 ColumnThree varChar(30)
 )";

// Execute query
mysql_query($sql,$con);

mysql_query("INSERT INTO ExampleTable(ColumnOne, ColumnTwo, ColumnThree)
VALUES ('Data1', 'Data2', 'Data3')");

mysql_query("INSERT INTO ExampleTable(ColumnOne, ColumnTwo, ColumnThree) 
VALUES ('Data_Data1', 'Data_Data2', 'Data_Data3')");

mysql_close($con);

?>

</body>
</html>

因此,我现在创建了一个 MYSQL 表,其中包含两个三列和两行数据。我怎样才能让这个表格显示在网页上?

第 2 步:保存/检索保存的数据 有什么方法可以将数据添加到表中,以便数据永久存在吗? - 或者这是默认情况下的工作方式?

例如:假设我有一个带有按钮的表单。单击该按钮时,新行将添加到表“ExampleTable”中。下次用户访问该页面时,该表将使用新添加的数据进行更新。

非常感谢您的帮助。我知道我是初学者,还没有完全理解这些主题。任何回复将不胜感激。

I'm sorry if this question frustrates anyone ... I am truly a beginner to PHP + MYSQL but I would love to make some progress on this one!

My Goal:

  1. To create a table that I can view in a web page.
  2. To be able to add data (rows) to this table and still have the data be there the next time the page is loaded.

Step 1: Creating the table

<html>
<body>

<?php

// Conect to MYSQL
$con = mysql_connect("localhost", "My_Username", "My_Password") or die(mysql_error());
echo "Connected to MYSQL </br>";

mysql_select_db("My_Database") or die(mysql_error());
echo "Connected to Database";

// Create a MySQL table in the selected database (called ExampleTable)

$sql = "CREATE TABLE ExampleTable
 (
// Set the primary key (personID)
 personID int NOT NULL AUTO_INCREMENT, 
 PRIMARY KEY(personID),
 ColumnOne varchar(15)
 ColumnTwo varchar(15)
 ColumnThree varChar(30)
 )";

// Execute query
mysql_query($sql,$con);

mysql_query("INSERT INTO ExampleTable(ColumnOne, ColumnTwo, ColumnThree)
VALUES ('Data1', 'Data2', 'Data3')");

mysql_query("INSERT INTO ExampleTable(ColumnOne, ColumnTwo, ColumnThree) 
VALUES ('Data_Data1', 'Data_Data2', 'Data_Data3')");

mysql_close($con);

?>

</body>
</html>

So, I have now created a MYSQL table with two three columns, and two rows of data. How can I get this table to show up on a web page?

Step 2: Saving / Retrieving saved data
Is there some way that I can add data to a table, so that the data will be there permanently? - or is this how it works by default?

For example: Let's say that I have a form with a button on it. When the button is clicked a new row is added to the table 'ExampleTable'. The next time the user visits the page the table will be updated with his newly added data.

Thank you very much for any help. I understand that I am a beginner and do not fully understand these topics yet. Any responses will be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

虚拟世界 2024-11-21 17:16:50

我建议在单独的步骤中创建表,而不是从 PHP 中创建;您不希望每次刷新网页时都创建一个新表。

创建表后,您可以通过执行(在 PHP 中,使用 mysql_query)类似“SELECT * from {tablename}”的查询来从中获取数据。从 mysql 获得查询结果后,您可以使用各种 PHP 循环和 mysql 记录读取方法,以您想要的形式将查询结果输出到 PHP 脚本将服务于客户端的页面中。

I'd recommend creating the table in a separate step, not from PHP; you don't want every time your web page is refreshed to create a new table.

Once your table is created, you can get the data from it by executing (within your PHP, using mysql_query) a query like "SELECT * from {tablename}". Once you've got that query result from mysql, then you can use the various PHP looping and mysql record reading methods to output the results from your query in the form you want into the page your PHP script will be serving to your client.

野味少女 2024-11-21 17:16:50

有 SELECT 和 UPDATE 查询可以执行此操作。例如,如果您想显示表中的数据,您可以使用如下所示的查询:

SELECT * FROM ExampleTable

在 PHP 中,您可以使用这些数据,例如如下所示:

$query = mysql_query("SELECT * FROM ExampleTable");
while ($row = mysql_fetch_array($query)) {
    echo $row["ColumnOne"];
}

对于 UPDATE 查询:

UPDATE ExampleTable SET ColumnOne = 'some value'

PHP 中的用法也与 mysql_query 一起使用。您还可以在查询中使用 WHERE 条件。

There are SELECT and UPDATE queries to do this. For example if you want to show data in a table, you would use query looking like this:

SELECT * FROM ExampleTable

In PHP, you can work with these data for example like this:

$query = mysql_query("SELECT * FROM ExampleTable");
while ($row = mysql_fetch_array($query)) {
    echo $row["ColumnOne"];
}

And to the UPDATE query:

UPDATE ExampleTable SET ColumnOne = 'some value'

Usage in PHP is also with mysql_query. You can also use WHERE conditions in the query.

雾里花 2024-11-21 17:16:50

1 - 显示

<?php

// Conect to MYSQL
$con = mysql_connect("localhost", "My_Username", "My_Password") or die(mysql_error());
echo "Connected to MYSQL </br>";

mysql_select_db("My_Database") or die(mysql_error());
echo "Connected to Database";

// Get all the data from the "ExampleTable" table
$result = mysql_query("SELECT * FROM ExampleTable") or die(mysql_error());  

echo "<table border='1'>";
echo "<tr> <th>ColumnOne</th> <th>CoumnTwo</th> <th>CoumnThree</th> </tr>";

// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {

    // Print out the contents of each row into a table
    echo "<tr><td>"; 
    echo $row['ColumnOne'];
    echo "</td><td>"; 
    echo $row['ColumnTwo'];
    echo "</td><td>"; 
    echo $row['ColumnThree'];
    echo "</td></tr>"; 
} 

echo "</table>";
?>

来自以下位置的所有数据: http://www.tizag.com/mysqlTutorial/mysqlselect.php< /a>

2 - 数据存储

任何INSERT插入数据库的内容都将保留*在数据库中,直到被显式删除。

*除了少数例外 - 但您稍后会在数据库之旅中了解它们:)

1 - Displaying All Data

<?php

// Conect to MYSQL
$con = mysql_connect("localhost", "My_Username", "My_Password") or die(mysql_error());
echo "Connected to MYSQL </br>";

mysql_select_db("My_Database") or die(mysql_error());
echo "Connected to Database";

// Get all the data from the "ExampleTable" table
$result = mysql_query("SELECT * FROM ExampleTable") or die(mysql_error());  

echo "<table border='1'>";
echo "<tr> <th>ColumnOne</th> <th>CoumnTwo</th> <th>CoumnThree</th> </tr>";

// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {

    // Print out the contents of each row into a table
    echo "<tr><td>"; 
    echo $row['ColumnOne'];
    echo "</td><td>"; 
    echo $row['ColumnTwo'];
    echo "</td><td>"; 
    echo $row['ColumnThree'];
    echo "</td></tr>"; 
} 

echo "</table>";
?>

From: http://www.tizag.com/mysqlTutorial/mysqlselect.php

2 - Data Storage

Anything INSERT-ed into a database will remain* in the database until it is explicitly deleted.

*with few exceptions - but you'll learn about them a little later in your database journey :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文