3

i wanna fetch just one row from my table using PHP mysqli. it uses prepared statement, but i can't do that and my code shows nothing. what should i do?

<?php
require_once 'inc/db.inc.php';

$post_id = $_GET['id'];

$q = "SELECT post_id, post_name, post_title, post_content, post_author, post_short_des, post_tag, post_date FROM posts WHERE post_id=? LIMIT 1";
$stmt = $conn->prepare($q);
$stmt->bind_param('i', $post_id);
$stmt->execute();
$stmt->bind_result($post_id, $post_name, $post_title, $post_content, $post_author, $post_short_des, $post_tag, $post_date);
$stmt->fetch();

print_r($stmt);
6
  • Note: The object-oriented interface to mysqli is significantly less verbose, making code easier to read and audit, and is not easily confused with the obsolete mysql_query interface. Before you get too invested in the procedural style it’s worth switching over. Example: $db = new mysqli(…) and $db->prepare("…") The procedural interface is an artifact from the PHP 4 era when mysqli API was introduced and should not be used in new code. Commented Jun 13, 2018 at 16:49
  • yes but i'm new in php and i have no experience in programming. so i'm scared of OOP(!) and i just don't understand it! Commented Jun 13, 2018 at 16:53
  • 2
    Five second crash course on "OOP": f($x, $y) procedural style becomes $x->f(y) in object-oriented form, the first argument is the object and how you call the function. The main thrust of OO is that you have functions that operate on an "object" instead of on arbitrary parameters. Think of an object as a container of related variables and functions that you can pass around ust as you would a simple variable. Commented Jun 13, 2018 at 16:56
  • It's worth noting that PDO is a lot cleaner in terms of interface, so if you're not too committed to mysqli so far it's worth checking it out. The named placeholders feature is a big deal, plus you can pass an associative array to execute() which makes the code a lot less fussy. Even better is an ORM like Doctrine, Propel, RedBeanPHP, or Eloquent which do a lot more than just run queries. Commented Jun 13, 2018 at 16:58
  • How many columns are there in your database? You need to bind and fetch them Commented Jun 13, 2018 at 16:59

1 Answer 1

2

You need to bind the results, then fetch them. Since you have LIMIT 1, there's no need to loop anything, as you will only fetch one row anyways.

Note that instead of SELECT *, we now select one column, in this example we select from the column content (change this as it appears in your database table). It's important that the number of selected columns matches the variables you bind in mysqli_stmt_bind_result(). You can select more than one column, just separate by commas.

$post_id = $_GET['id'];

$q = "SELECT content FROM posts WHERE post_id=? LIMIT 1";
$stmt = mysqli_prepare($dbc, $q);
mysqli_stmt_bind_param($stmt, 'i', $post_id);
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $content);
mysqli_stmt_fetch($stmt);

if (mysqli_stmt_num_rows($stmt)) {
    echo $content;
} else {
    echo "No data";
}

I wrote this from my phone, so there might be some mistakes. The documentation shows good examples though.

Sign up to request clarification or add additional context in comments.

4 Comments

thank you. but what is $content ? i mean we didn't define it any where...
i've edit the code and changed it to oop thanks to @tadman . and bind result but print_r is not showing any result yet!
ahaaa. print_r wont work like mysql query. thanks my code works.
$content gets defined in mysqli_stmt_bind_result(). That function is not like most, as this actually defines a variable. The variable holds the value of the column that was fetched.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.