0

I'm working on an assignment for school which involves using PHP to generate a table of SQL results. I'm having a problem converting the results of my query. This is my error message:

Catchable fatal error: Object of class mysqli_result could not be converted to string

Here is the relevant part of my code:

$q = "SELECT pages.pageid,pages.pagetitle,pagetype.pagetypename,stylesheets.stylename
FROM pages
INNER JOIN stylesheets
ON pages.styleid=stylesheets.styleid
INNER JOIN pagetype
ON pages.pagetypeid=pagetype.pagetypeid
ORDER by pages.".$sortBy." ".$sortDirection;
print("<p>Query: ".$q."</p>\n");


$r = mysqli_query($db_link,$q);
$n = mysqli_num_rows($r);
print("<p>$r</p>");

My only theory is that maybe since I'm using an inner join with multiple tables there's something additional I need to do? I ran the query the code displayed in the MySQL server and it worked fine and returned the right results.

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
user3084366
  • 1,103
  • 2
  • 9
  • 12

3 Answers3

2

The problem is that $r is an object, not a string. You want to print the value of $n, not $r. Change your print statement to the following to fix the error:

print("<p>$n</p>");

It's unclear from the question details whether you're asking how to print number of rows, or how to print the actual results of the query.

If you're trying to do the latter: mysqli_query() returns a mysqli_result object. The returned result resource should be passed to mysqli_fetch_array(), mysqli_fetch_assoc() or other functions for dealing with result tables, to access the returned data.

The basic structure would look like:

$r = mysqli_query($db_link,$q) or die(mysqli_error($db_link);
$n = mysqli_num_rows($r);

while ($row = mysqli_fetch_assoc($r)) {
    // use $row['key']
}

Also, I suggest you use more descriptive variable names in your code -- it makes your code maintainable and easy to read.

You should also be more careful about constructing your query strings. If $sortBy is coming from user input, it is a vector for an SQL injection attack.

Tim Seguine
  • 2,887
  • 25
  • 38
Amal Murali
  • 75,622
  • 18
  • 128
  • 150
1

Part of your confusion is that you are using lots of 1 character strings that—in my honest opinion—simply cause confusion & don’t really benefit coding nowadays. Use real words for strings & make your life—and debugging process—easier. Also your query & results seem to have not been parsed. Here is my take on your code using an object oriented style of handling:

$query = "SELECT pages.pageid,pages.pagetitle,pagetype.pagetypename,stylesheets.stylename
FROM pages
INNER JOIN stylesheets
ON pages.styleid=stylesheets.styleid
INNER JOIN pagetype
ON pages.pagetypeid=pagetype.pagetypeid
ORDER by pages.".$sortBy." ".$sortDirection;

print("<p>Query: ".$query."</p>\n");

$result = $db_link->query($query);
$number_of_rows = $result->num_rows($result);
while($row = $result->fetch_assoc()){
    print("<p>$row</p>");
}
Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
  • For consistency with the OP's post, you may want to write the `mysqli` commands in the procedural style rather than the OOP style. – War10ck Dec 09 '13 at 20:50
  • Perhaps. But in my experience, the reason new PHP programers use the procedural style is because that is what is given as an example on PHP manual pages. In real world use, object oriented formatting is more common & there are more answers/techniques out there that use object oriented style than procedural. – Giacomo1968 Dec 09 '13 at 21:12
1

mysqli_query() returns a result set resource, which is not the same as a set of results. You need to retrieve the data with mysql_fetch_array() or similar.

$r = mysqli_query($db_link,$q) or die(mysqli_error($db_link);
$n = mysqli_num_rows($r);

while ($row = mysqli_fetch_array($r)) {
 var_dump($row);       // replace this with your own code to format the output
}