0

ok i have been using the mysql format for quite a while but i figured its time to transfer my site over to the new mysqli format now rather then later. im trying to re write my functions to draw stats from the database but i am getting all sorts of errors.

I have looked at countless mysqli tuts but none are very descriptive and im not quite understanding the 2 parameter rule ect. below i posted my function and includes. if anyone can tell m what im doing wrong i would appreciate it.

Fatal error: Call to a member function fetch_assoc() on a non-object in C:\xampp\htdocs\includes\stat.php on line 7

stat.php

function getStat($stat) {
require_once 'includes/config.php';     
$mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
$sql = $mysqli->query("SELECT $stat FROM players WHERE  username = '".$_SESSION['username']."'");
$row = $sql->fetch_assoc();
$result = $row['$stat'];
return $result;
}
?>

and here is the test page i am using to try and get this working.

test.php

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

echo getStat('name');


?>

ow and please dont start posting a bunch of comments bashing on the code cause as stated before ive never used mysqli so ide rather read constructive criticism then flames like half the post around here.

11
  • use var_dump to check what $sql really is after the query. Some error may have happen in the SQL statement. Commented Jan 13, 2014 at 1:06
  • Try using the following code, and report back with the error... pastebin.com/xNynwP3c Commented Jan 13, 2014 at 1:06
  • Call to a member function fetch_assoc() on a non-object in C:\xampp\htdocs\includes\stat.php on line 8 Commented Jan 13, 2014 at 1:09
  • You did not use the code at pastebin, or you would have seen a mysqli exception thrown. Commented Jan 13, 2014 at 1:09
  • 1
    You should tell mysqli to throw errors so that you get an unhandled exception error when something goes wrong. Put this before you open your db connection: mysqli_report(MYSQLI_REPORT_STRICT); Commented Jan 13, 2014 at 1:17

2 Answers 2

1

Change that in your getStat function.

$result = $row[$stat];
Sign up to request clarification or add additional context in comments.

1 Comment

The undefined function is before that line... $row = $sql->fetch_assoc(); is throwing an error...not $result = $row['$stat'], althought that will potentially be an issue as well.
0

Adapt the following code to best suit your current code...

if ($stmt = mysqli_prepare($link, "SELECT $stat FROM players WHERE  username = ?")) {

    /* bind parameters for markers */
    mysqli_stmt_bind_param($stmt, "s", $_SESSION['username']);

    /* execute query */
    mysqli_stmt_execute($stmt);

    /* bind result variables */
    mysqli_stmt_bind_result($stmt, $district);

    /* fetch value */
    mysqli_stmt_fetch($stmt);

    printf("%s is the current username.");

    /* close statement */
    mysqli_stmt_close($stmt);
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.