PHP 对象到字符串

发布于 2024-11-07 02:03:19 字数 806 浏览 0 评论 0原文

我花了 2 个小时在 Stack Overflow 上搜索了有关这个问题的其他几篇文章,它们似乎比我需要的更复杂。我尝试执行 __toString 函数,但我认为我做得不正确。

我正在尝试获取 $personid 的 sql 语句并将其转换为字符串,以便我可以在其他 sql 方法中使用它。

$personid = $this->db->query('SELECT person_id FROM person');

$fullname = $this->db->query("SELECT person_fname, person_lname FROM person
                          WHERE person_id = '".$personid."';");

$sport = $this->db->query("SELECT sport_name FROM person_sport_rel A1
                        INNER JOIN sport A2 ON A1.sport_id = A2.sport_id
                        WHERE person_Id = '".$personid."';");

正如其他帖子有问题一样..我收到的错误是

Object of class CI_DB_mysql_result could not be converted to string

我尝试在 PHP 网站上推荐的 CI_DB_mysql_result 类中添加构造函数和 __toString 但无济于事

I spent 2 hours searching through the other few posts at Stack Overflow concerning this issue and they seemed more complicated than what I needed. I attempted doing the __toString function but I don't think I did it correctly.

I am trying to take the sql statement for $personid and convert it to a string so I can use it in the other sql methods.

$personid = $this->db->query('SELECT person_id FROM person');

$fullname = $this->db->query("SELECT person_fname, person_lname FROM person
                          WHERE person_id = '".$personid."';");

$sport = $this->db->query("SELECT sport_name FROM person_sport_rel A1
                        INNER JOIN sport A2 ON A1.sport_id = A2.sport_id
                        WHERE person_Id = '".$personid."';");

Just as the other posts had the problem.. the error I receive is

Object of class CI_DB_mysql_result could not be converted to string

I tried adding the constructor and the __toString in the CI_DB_mysql_result class that was recommended on the PHP site but to no avail

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

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

发布评论

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

评论(2

偏爱自由 2024-11-14 02:03:19

看起来好像您正在使用 Codeigniter,要获取查询结果,请检查此网站: http://www.codeignitor.com/user_guide/database/results.html

从该页面:

$query = $this->db->query("YOUR QUERY");

foreach ($query->result() as $row)
{
   echo $row->title;
   echo $row->name;
   echo $row->body;
}

It looks as though you are using Codeigniter, to fetch the results of a query, check this website: http://www.codeignitor.com/user_guide/database/results.html

From that page:

$query = $this->db->query("YOUR QUERY");

foreach ($query->result() as $row)
{
   echo $row->title;
   echo $row->name;
   echo $row->body;
}
可可 2024-11-14 02:03:19

当您运行此查询时,您可能会返回一堆 id:

$personidsQuery = $this->db->query('SELECT person_id FROM person');

因此,当您想在新查询中重复使用它时,这可能更有趣:

$sql = "SELECT person_fname, person_lname FROM person WHERE person_id = ?";
foreach ($personidsQuery->result() as $row)
{
    $namesQuery = $this->db->query($sql, array($row->person_id));
}

CI 用户指南在这方面提供了很好的帮助:http://www.codeignitor.com/user_guide/database/

You are probably returning a bunch of id's when you're running this query:

$personidsQuery = $this->db->query('SELECT person_id FROM person');

so when you want to re-use it in new queries this is probably more interesting:

$sql = "SELECT person_fname, person_lname FROM person WHERE person_id = ?";
foreach ($personidsQuery->result() as $row)
{
    $namesQuery = $this->db->query($sql, array($row->person_id));
}

The CI userguide is a good help in this : http://www.codeignitor.com/user_guide/database/

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