0

My code is very messy, but I am trying to make a forum on my website.

<?php 
    $conn = mysqli_connect("localhost", "root", "");
    if($conn->connect_error){
        die("Connection Failed");
    }
    mysqli_select_db($conn, 'forum');
    $get= "select * from forum";
    $runq = mysqli_query($conn, $get);
    while($fetch_value=mysqli_fetch_array($runq)){
        $get_usr_name=$fetch_value['username'];
        $get_body = $fetch_value['info'];
        }
        $result = $conn->query($get);
        if(is_null($get_body)){

        }
        else{
        echo "<div id='inputform1'>";
        for($row = 1; $row < $result->num_rows; $row++){

            $dbhost = "localhost";
            $dbuser = "root";
            $dbpass = "";
            $dbname = "forum";
            $connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
            if(mysqli_connect_errno()){
                die("database connection failed: ") .
                    mysqli_connect_errno() .
                    " (" . mysqli_connect_errno() . ")";

            }
            $result1 = mysqli_query($connection, "select info from forum where id='$row';");
            $result = mysqli_query($connection, "select username from forum where id='$row';");
        echo "<strong class=\"after_username\">by: {$result}</strong><br><br><p id=\"bodyforum\">{$result1}<br><br></p>";
        }
        echo "</div>";

    }
    ?>

I cannot seem to post the username and password; it keeps saying:

Object of class mysqli_result could not be converted to string

I have tried many different "tactics" but none of them seem to work. Can someone help me?

chris85
  • 23,846
  • 7
  • 34
  • 51
garrett
  • 31
  • 4
  • Which line is throwing the error? Why run the same query twice? – chris85 Mar 04 '16 at 02:13
  • chris85 is correct: You can use `select info, username from forum where id='$id'"` to avoid two queries. However, `$id` should be taken from the query result; you're assuming all IDs for rows your query returns will return start at 1 and increment regularly. This is not likely to be the case. – kungphu Mar 04 '16 at 02:31

2 Answers2

1

The returned value of a mysqli query is a result object. It's not just a set of rows; it also contains information on the query, success or failure, error messages, and other things. You need to use the result object's methods explicitly to access the rows returned by your query.

kungphu
  • 4,592
  • 3
  • 28
  • 37
1

I think the problem is from this line

echo "<strong class=\"after_username\">by: {$result}</strong><br><br><p id=\"bodyforum\">{$result1}<br><br></p>";

The $result and $result1 are not strings. You need to loop it up and then access the records.

chris85
  • 23,846
  • 7
  • 34
  • 51
Inducesmile
  • 2,475
  • 1
  • 17
  • 19