addressalign-toparrow-leftarrow-rightbackbellblockcalendarcameraccwcheckchevron-downchevron-leftchevron-rightchevron-small-downchevron-small-leftchevron-small-rightchevron-small-upchevron-upcircle-with-checkcircle-with-crosscircle-with-pluscrossdots-three-verticaleditemptyheartexporteye-with-lineeyefacebookfolderfullheartglobegmailgooglegroupshelp-with-circleimageimagesinstagramFill 1linklocation-pinm-swarmSearchmailmessagesminusmoremuplabelShape 3 + Rectangle 1ShapeoutlookpersonJoin Group on CardStartprice-ribbonShapeShapeShapeShapeImported LayersImported LayersImported Layersshieldstartickettrashtriangle-downtriangle-uptwitteruserwarningyahoo

mysqli prepared query question

From: David M.
Sent on: Thursday, November 20, 2008 4:35 PM
I'm wrestling my way into the world of mysqli, especially prepared queries.  This is also my first foray into an Object Oriented approach/style.

Here's my current roadblock that I seem to be stuck on: I am trying to create a simple, 'prepared statements'-type query (SELECT Name FROM organizations WHERE ID = ?).  The only way I can get this to work is to not only use 'bind_param' (to bind a variable to the 'WHERE ID = ?' part of the query) but also use the 'bind_result' method.  If I don't do this, I get no results.

I've been messing around with this so much that I no longer have a true sample to offer.  But here's something close to what I've been doing (you'll notice a lot of book-type names of variables):

// Connect
$mysqli = new mysqli("localhost", "me", "supersecret", "thedatabase");

// Prepare query
$stmt = $mysqli->prepare("SELECT Name FROM organizations WHERE ID=?");

$stmt->bind_param("i", $TheID);

$TheID = 15;

// Execute
$stmt->execute();

// Show results (or not!) -- why doesn't this work?
$row = $stmt->fetch_row();
print_r($row);

// If I use bind_result, it works
$stmt->bind_result($NewResult);
$stmt->fetch();

print_r($NewRsult);     // Yeah!

Why might this be?
David

Our Sponsors

  • PluralSight

    PluralSight subscriptions for developer training

  • O'Reilly

    Disc Code: PCBW is good for 40% off print and 50% off ebooks and videos

  • JetBrains PhpStorm

    Occasional free licenses to raffle off at meetups

  • DynaTrace

    Find yourself, promote yourself, stay true to yourself.

Sign up

Meetup members, Log in

By clicking "Sign up" or "Sign up using Facebook", you confirm that you accept our Terms of Service & Privacy Policy