I am trying to dynamically create html content based on the results of a mysql query on my personal web project. I had thought I had found the solution, but then realized I was mixing PHP apis and I shouldn't do that. For a few other pages, I learned about prepared statements and got through re-writting a lot of my php handlers. But I'm not sure how to replicate something like this...
<?php
$teacherid = 1;
$servername = "localhost";
$username = "xxxx";
$password = "xxxx";
$dbname = "xxxx";
$conn = new mysqli($servername, $username, $password, $dbname);
if($conn->connection_error){
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT ClassID, className FROM Classes WHERE teacherID = '" . $teacherid . "' ;";
$result = $conn->query($sql);
while($row = $result->fetch_assoc()){
echo ("<a class='button href=\"class.php?id=".$row["ClassID"]."\">".$row["className"."</a>");
}
Appache gives me the error result:
Object of class mysqli_result could not be converted to string in /var/../../page.php
on this line of code
$sql = "SELECT ClassID, className FROM Classes WHERE teacherID = '" . $teacherid . "' ;";
vardump($teacherid) produces the following:
object(mysqli_result)#1 (5){["current_field"]}=>NULL ["field_count"]=>NULL ["lengths"]=>NULL ["num_rows"]=>NULL ["type"]=>NULL}
It appears that my session variable was somehow dropped somewhere in my website... before tracking that down I wanted to see this method for creating content work. I changed $teacherid to this...
$teacherid = 1;
I also changed $sql to
$sql = "SELECT ClassID, className FROM Classes WHERE teacherID = " . $teacherid . " ;";
Now, no buttons are being created, but I am also not receiving any error from Appache. The exact same query used directly on sql will return 1 valid result, however.
A solution to the session var was also found here: Notice: Unknown: Skipping numeric key 1 in Unknown on line 0