dsoxcj7276 2014-01-13 22:48
浏览 67
已采纳

mysqli函数返回没有值

ok i have a mysqli function that draws a users stat from a database. the datbase is set up as

username attack defense strength
xxxx 10 10 10

the problem i am running into is the function is returning nothing, the page is blank. my connection to the database present and session username is stored so i can figure out why its not working.

config.php

 <?php
 $dbhost = 'xxx'; 
 $dbuser = 'xxx';
 $dbpass = 'xxx';
 $dbname = 'xxx';
 $dberror1 = 'could not connect to database';
 $dberror2 = 'could not find selected table';
 ?>

stat.php

<?php
function getLevel($stat) {
require_once 'includes/config.php';

$mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname);

$stmt = $mysqli->prepare("SELECT ? FROM players WHERE username = ?");

if($stmt) {
 $stmt->bind_param('ss', $stat, $_SESSION['username']);
 if($stmt->execute()) {
      $stmt->bind_result($result);
      $stmt->fetch();
       }
  }
  return $result;
  }  

?>

and the file im using to test displaying the function

<?php
include 'includes/login-check.php';
include 'includes/config.php';
include 'includes/stat.php';

echo getLevel('attack');
?>
  • 写回答

1条回答 默认 最新

  • dongqucheng3851 2014-01-13 22:50
    关注

    You cannot bind table- or column names in your prepared statement, just values.

    You would have to check the $stat value against a white-list and insert it directly in the sql statement.

    By the way, you can tell mysqli to throw exceptions so that you don't have to check the return value of every database call manually. Just add this before you open your database connection:

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

报告相同问题?

悬赏问题

  • ¥15 luckysheet
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误