开发者

PHP Object to String

I spent 2 hours searching through the other few posts at Stack Overflow concerning this开发者_JAVA技巧 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


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;
}


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/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜