gpt4 book ai didi

php - 获取表中具有特定名称的所有项目名称值

转载 作者:行者123 更新时间:2023-11-29 06:32:44 25 4
gpt4 key购买 nike

因此,我正在为我和我的 friend 创建一个网站,以便我们可以相互合作开展项目(如 Google 文档),并且我正在尝试创建一个东西,以便我们可以选择我们想要开展的项目。目前我无法从 MySQL 表中获取项目名称。

这是我试图获取项目名称的内容

$projects = mysqli_query($link, "SELECT * FROM table WHERE name='". $username ."'");
$projects2 = mysqli_fetch_assoc($projects);
$projects3 = $projects2["projectname"];

这是一个MySQL表的例子

+---------+------------------+------------------+------------------+------------
-+
| name | htmltext | csstext | jstext | projectname
|
+---------+------------------+------------------+------------------+------------
-+
| cow9000 | testing is cool! | testing is cool! | testing is cool! | test
|
| cow9000 | testing is cool! | testing is cool! | testing is cool! | test2
|
+---------+------------------+------------------+------------------+------------

如您所见,所有者拥有 2 个文档,但是当我尝试打印 $project3 时,它只给我“测试”。我将如何更改我的代码以获取所有项目名称值?

抱歉,如果这让我感到困惑,我很难用语言表达。另外,请指出我的代码中的错误,因为我只有几天的 PHP 和 MySQL 经验(但我发现 PHP 和 MySQL 对我来说非常非常容易。)

最佳答案

您已经拥有所有这些值。您只需像 projectname 一样访问它们,方法是将该列的名称用作 $projects2 数组中的键:

$projects2 = mysqli_fetch_assoc($projects);
echo $projects2["name"]; // cow9000
echo $projects2["htmltext"]; // testing is cool!
echo $projects2["csstext"]; // testing is cool!
echo $projects2["jstext"]; // testing is cool!

如果你想要第二行,你需要使用一个循环:

// Prints each row in the order they were retrieved 
while($projects2 = mysqli_fetch_assoc($projects)) {
echo $projects2["projectname"]; // test and then test2
}

关于php - 获取表中具有特定名称的所有项目名称值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26921295/

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