Sunday, 15 February 2015

Passing a single column value returned by a MySQL query to PHP script -



Passing a single column value returned by a MySQL query to PHP script -

i trying adapt next code mysql query below, can't work out how so. query should homecoming 1 row, , want extract info column 'code' in row , pass variable php script.

/* prepare statement */ if ($stmt = $mysqli->prepare("select code clients name='jane'")) { $stmt->execute(); /* bind variables prepared statement */ $stmt->bind_result($code); /* fetch values */ while ($stmt->fetch()) { printf($code); } /* close statement */ $stmt->close(); }

i think you've been working based on http://php.net/manual/en/mysqli.prepare.php. however, should follow on binding params, too:

/* prepare statement */ if ($stmt = $mysqli->prepare("select code clients name=?")) { /* bind query parameters prepared statement */ $stmt->bind_param("s", 'jane'); $stmt->execute(); /* bind result variables prepared statement */ $stmt->bind_result($code); /* fetch values */ if ($stmt->fetch()) { printf('code: %s', $code); } else { print("client code not found"); } /* close statement */ $stmt->close(); }

you're preparing statements here, should bind params separately.

php mysql prepared-statement

No comments:

Post a Comment