0

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

Community
  • 1
  • 1
Christopher
  • 790
  • 12
  • 30
  • You are already using mysqli? – PeeHaa Dec 01 '15 at 22:43
  • @PeeHaa I was under the assumption I was not, due to the fact that I am getting the error "mysqli_result could not be converted to string" on the while statement... – Christopher Dec 01 '15 at 22:44
  • [Short, Self Contained, Correct (Compilable), Example](http://sscce.org/) – PeeHaa Dec 01 '15 at 22:48
  • @PeeHaa This example has already been stripped down. I am not sure how creating another example of similar expression would enable you to understand my intentions. Perhaps you could explain what it is about my question that you do not understand. – Christopher Dec 01 '15 at 22:54
  • It's missing information (variables, error messages, your actual code, etc). We don't do debugging. We certainly don't do debugging based on just a code dump without any info. – PeeHaa Dec 01 '15 at 22:57
  • 1
    Understandable, I communicate below that I did indeed miss my binding of $teacherid when I made this post. I also indicated to you the error message I am seeing and to where about this error is occuring. I will edit this post to fufill these informational needs. Stay posted. – Christopher Dec 01 '15 at 23:00
  • 1
    What does `var_dump($teacherid)` tell you? – PeeHaa Dec 01 '15 at 23:08
  • It appears that the var_dump is showing NULL results... perhaps the php session is being lost somewhere or somehow. – Christopher Dec 01 '15 at 23:16
  • @Christopher you are trying to put object into a query and it expect string – Standej Dec 01 '15 at 23:17
  • @Standej yes, perhaps I am losing the session... I will try a static test query to see if this is indeed the case. – Christopher Dec 01 '15 at 23:19
  • 1
    @Christopher Can you post code how you generating `$teacherid` pls – Standej Dec 01 '15 at 23:34
  • @Standej This is now shown in my original example. – Christopher Dec 01 '15 at 23:35
  • 1
    Okay @Christopher it does not generate you any button because again you have syntax error on your echo. try to echo 'Success'; in loop or use my echo example `echo "".$row["className"]."";` – Standej Dec 01 '15 at 23:40

1 Answers1

1

You have some syntax mistakes so I need to write you before anything and maybe is this what you need

while($row = $result->fetch_assoc()){
    echo "<a class=\"button\" href=\"class.php?id=".$row["ClassID"]."\">".$row["className"]."</a>";
}

Also $teacherid in your query is not defined anywhere

Edit: Again you have syntax error in your echo

echo ("<a class='button href=\"class.php?id=".$row["ClassID"]."\">".$row["className"."</a>");

You missed to close brackets for row['ClassName'];

Standej
  • 749
  • 1
  • 4
  • 11
  • Yes, Sorry. I am dev on a device without an internet connection, so I have to re-write the code line by line. $teacherid is defined in my code... sorry – Christopher Dec 01 '15 at 22:46
  • No worries. About echo using brackets is valid way just be aware to use "" properly. And also can you give me more detials what else is not working on the code. @Christopher – Standej Dec 01 '15 at 22:49
  • Well in my Appache logs I am getting the "mysqli_result could not be converted to string" on the line for my while statement. So no buttons are actually being generated as a result. In fact, the sql query is not even sent to mysql because the PHP itself is failing. – Christopher Dec 01 '15 at 22:52
  • I am sorry, I misread the line count... there error is acutally occuring where I bind my sql statement to $sql... I will look to see if there is a syntax error there – Christopher Dec 01 '15 at 22:57
  • Can you post this code also? Since Im looking to have more info so i can help @Christopher – Standej Dec 01 '15 at 22:58
  • Be aware that somewhere you using query result (which is object) as a string so pay more attention on this – Standej Dec 01 '15 at 23:00
  • Can you write also query that generates your `$teacherid` value – Standej Dec 01 '15 at 23:19
  • It would appear that even when $teacherid is set to a valid number, the method to create buttons is not working. So I have $teacherid set to = 1 ... in the Classes table there is a row where teacherID=1 .. Once again not sure where to go from here... I also fixed a problem where $teacherid was surrounded with ' ' in my sql query. $teacherid is an int, so I removed those quotes. – Christopher Dec 01 '15 at 23:25
  • Where do you setting `$teacherid=1;` If you do just in front of $query then I dont see reason to not work, but if you puting this value in some table then again you extracting this result from db and again instead of geting string from it you putting query result to new query. I will need more code to see so I can realize what is going on – Standej Dec 01 '15 at 23:30
  • I feel so dumb.. I had actually been trying with your code, but in the process of writting it out by hand again, I missed a single " ... thank you. I will still need to find why my session is closing, but thank you very much for your dedication here. – Christopher Dec 01 '15 at 23:47