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.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • use var_dump to check what $sql really is after the query. Some error may have happen in the SQL statement. – TwilightSun Jan 13 '14 at 01:06
  • Try using the following code, and report back with the error... http://pastebin.com/xNynwP3c – Justin E Jan 13 '14 at 01:06
  • Call to a member function fetch_assoc() on a non-object in C:\xampp\htdocs\includes\stat.php on line 8 – user3078244 Jan 13 '14 at 01:09
  • You did not use the code at pastebin, or you would have seen a mysqli exception thrown. – Justin E Jan 13 '14 at 01:09
  • The problem is here: `$mysqli->query("SELECT $stat FROM players WHERE username = '".$_SESSION['username']."'");` Try to guess! – Cilan Jan 13 '14 at 01:10
  • i copy and pasted it so im pretty sure i used it – user3078244 Jan 13 '14 at 01:10
  • There is an error in your query. Check the duplicate question for this, and try their suggestion. `$result = $this->database->query($query); if (!$result) { throw new Exception("Database Error [{$this->database->errno}] {$this->database->error}"); }` – Justin E Jan 13 '14 at 01:11
  • I don't think you have a column name `$stat`, try `$mysqli->query("SELECT * FROM players WHERE username = '".$_SESSION['username']."'");` – Cilan Jan 13 '14 at 01:12
  • $stat = getstat('what ever column i put') this code worked perfect in old mysql – user3078244 Jan 13 '14 at 01:14
  • Consider PDO, it is a bit easier to use, and prepare statements with. – Justin E Jan 13 '14 at 01:15
  • 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);` – jeroen Jan 13 '14 at 01:17

2 Answers2

1

Change that in your getStat function.

$result = $row[$stat];
Uğur Özpınar
  • 1,033
  • 7
  • 16
  • 1
    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. – Justin E Jan 13 '14 at 01:08
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);
}
Justin E
  • 1,252
  • 16
  • 30