-3

I got this comment viewer script. And I'm requesting help to find an issue why it's not working. PHP doesn't show any errors. I don't know what wrong with it.

My MySQL databases looks like this:

comments { authorid, content, _when, }

users { id, firstname, name, nickname, password }.

echo "<div id='comments'>";

$sql_comments = "SELECT * FROM table.comments";
$comments = $conn->query($sql_comments);

$sql_2 = "SELECT id, firstname, name, nickname, password, type FROM users.users";
$conn2_2 = $conn->query($sql_2);
$row6 = $conn2_2 -> fetch_array();


while( $row5 = $comments -> fetch_array() && $id_users = $row6["id"] && 
    $firstname_users = $row6["firstname"] && $name_users = $row6["name"]) {



if( $id_users == $row5["authorid"] ) {
    echo "<div class=\"infoComments\">";
    echo "<div class=\"commentsImg\"><img src=\"imgs/randomProfileImgs/$ranNum.png\" id=\"profileImg\" /></div>";
    echo "<div class=\"commentsInfo\">" . $firstname_users . "<br />" . $name_users . "</div>";
    echo "<div class=\"commantsContent\">" . $row5["content"] . "</div>";
    echo "</div>";
};

};

echo "</div>";

Any ideas?

Jerry Sky
  • 11
  • 1
  • 6
  • define "not working". the answer is probably your users table not having a `type` column and you're trying to call it out on line 6. on line 7, $conn2_2 probably === false because of the bad sql query, and then on line 8, the call to fetch_array() fails because $conn2_2 is a boolean and not an object. all of which you would have figured out had you turned on error reporting. – castis Jan 27 '15 at 23:02
  • This is the same as if you went to a doctor's place, said you're not feeling good and then you took your shoes off. Then the doctor looks at you with a blank stare at his face, much like we are now. You can't just type that some code is not working. How the hell is it not working? Describe the *symptoms*, describe desired functionality, describe what you're getting and finally - try to help yourself by inspecting logs before dumping some spaghetti code on SO. – N.B. Jan 27 '15 at 23:07
  • What is `table.comments` ? Or `users.users` ? Are you running different databases or something? – Darren Jan 27 '15 at 23:08
  • 1
    You have a database called `table`, and in that database you have a table called `comments`? You have another database called `users`, and in that database you have a table called `users`? No, I didn't think so! – Strawberry Jan 27 '15 at 23:09
  • Have you tried [enabling exceptions](http://stackoverflow.com/questions/14578243/turning-query-errors-to-exceptions-in-mysqli) in `mysqli`? That makes errors impossible to ignore. – tadman Jan 27 '15 at 23:14

1 Answers1

0

You state that your users table contains the following columns.

id, firstname, name, nickname, and password

You then proceed to put together a query that attempts to pull the type column out.

// bad sql query, tries to get 'type' but that column doesnt exist
$sql_2 = "SELECT id, firstname, name, nickname, password, type FROM users.users";

// query() will return false because theres a problem
$conn2_2 = $conn->query($sql_2);

// this is now a boolean because of the above issue
$row6 = $conn2_2 -> fetch_array();

What you should put after the query() call is this

if (!$conn2_2) {
    printf("Errormessage: %s\n", $conn->error);
    exit();
}

To avoid this type of thing in the future, do yourself a huge favor and make sure php can tell you when theres a problem by putting this at the start of your scripts.

error_reporting(-1);
ini_set('display_errors', 'On');

Or, you may want to make sure that the user you're connecting to mysql with has access to the databases table, AND users

castis
  • 8,154
  • 4
  • 41
  • 63