jskidd3
jskidd3

Reputation: 4783

Use bind_result to push into array

Is it possible to use bind_result and bind all of the columns into an array? The reason I ask is because I execute the code on lots of different tables and don't want to have to put each column in when the amount changes.

$stmt = $mysqli->prepare("SELECT * FROM " . $company . " WHERE `id` = ?");
$stmt->bind_param('s', $route);

$route = $_GET['routeid'];

$stmt->execute();
$stmt->bind_result($test);

Currently that's what my PHP looks like. Clearly it's wrong because you must bind them to something, I just need to know how to put the data into an array and bind it to that (hoping you can understand that poor explanation).

Thanks

PS. I've looked at the related answer and it only confused me. If someone could explain in a little more detail I'd appreciate it.

Upvotes: 0

Views: 626

Answers (2)

Jon
Jon

Reputation: 437634

Why do you specifically want to use bind_result to do this when a more natural alternative already exists?

$stmt->execute();
$result = $stmt->get_result();

while ($row = $result->fetch_assoc()) {
    // do something with $row
}

If fetch_assoc is not exactly what you want, other variations include fetch_row (numeric indexing) and fetch_object.

Upvotes: 4

givemesnacks
givemesnacks

Reputation: 336

$stmt->bind_result($test['col1'], $test['col2']);

Continue adding $test['colX'] for however many columns you have.

Upvotes: 0

Related Questions