douqian1296 2015-12-01 22:36
浏览 44
已采纳

使用sqli php动态生成html

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

  • 写回答

1条回答 默认 最新

  • dongxi8297 2015-12-01 22:44
    关注

    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'];

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 iqoo11 如何下载安装工程模式
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?