2

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

2 Answers 2

3

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;
}
Sign up to request clarification or add additional context in comments.

Comments

2

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/

1 Comment

Thank you for the help! I finally made the code with the help of both of you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.