gpt4 book ai didi

php从mysql中提取数据

转载 作者:行者123 更新时间:2023-11-29 14:31:48 25 4
gpt4 key购买 nike

我有一个包含以下行的 mysql 表:

id_cont  suma_lun   month           year
--------------------------------------------
FL28 2133 March 2012
FL28 2144 April 2012
FL28 2155 May 2012
FL28 2166 June 2012

如何提取每个 id_cont 的 suma_lun、月份和年份?这样我就得到这样的输出:

ID:           Month:  Monthly Sum:        Year:
----------------------------------------------
FL28 March 2133 2012
April 2144 2012
May 2155 2012
June 2166 2012

这是我当前的代码:

$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link) die ('Could not connect to database: '.mysql_error());
mysql_select_db(DB_DATABASE,$link);
$sql="SELECT * FROM test WHERE id_cont = '$cur'";
$result=mysql_query($sql);

while ($row=mysql_fetch_array($result)) {
$a=$row["id_cont"];
$b=$row["suma_lun"];
$c=$row["month"];
$d=$row["year"];

}

我回显表格中的数据谢谢!

最佳答案

使用查询

SELECT
id_cont, month, suma_lun, year
FROM
`your_table`
ORDER BY
id_cont

你的输出将是

id_cont  suma_lun   month           year
--------------------------------------------
FL28 2133 March 2012
FL28 2144 April 2012
FL28 2155 May 2012
FL29 2166 June 2012
FL29 2226 July 2012
FL29 2353 Aug 2012

然后在 PHP 中你可以获得想要的输出

<?php

$id=0;

while($row=mysql_fetch_array($records))
{
if($row['id_cont']) !=id)
{
echo" $row[id_cont] ";
id=$row['id_cont'];
}
echo "$row[month]";
echo "$row[suma_lun]";
echo "$row[year]";
}

?>

关于php从mysql中提取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9871103/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com