1

Title is a little hard to understand, so basically I'm making a Pastebin clone and am attempting to do a kind of viewmember.php?id=1213 thing for viewing pastes. However, I can't figure it out at all. I've done a lot of research, and after finally understanding what I had to do (or so I thought) I made this up and don't know why it isn't working.

<?php
require 'connection.php';
    $getid = $_GET["id"];
    $sql = "SELECT * FROM pasteinfo WHERE id = ?";
    $stmt = $con->prepare($sql);
    $stmt->bind_param("i", $getid);
    echo $stmt;

?>

I'm probably just stupid. Thanks for the help.

3
  • You need to execute the statement to actually get any results. Commented Oct 5, 2016 at 4:30
  • How do I do that? dont crucify me please Commented Oct 5, 2016 at 4:35
  • ...and you won't be able to echo the results like that. Commented Oct 5, 2016 at 4:35

2 Answers 2

1

You need to run the command to execute the query.

$sql = "SELECT field1, field2 FROM pasteinfo WHERE id = ?"; // Specify fields in query
$stmt->bind_param("i", $getid);  /* bind parameters for markers */
$stmt->execute();  /* execute query */
$stmt->bind_result($field1, $field2); /* bind result variables */
$stmt->fetch();    /* fetch value */
echo "Field 1:".$field1;
echo "Field 2:".$field2;

Reference: Example #1 mysqli::prepare() example

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

4 Comments

Thanks! How do I echo the result?
In order to echo the result, it's the best if you specify the fields in SELECT instead of mentioning SELECT *
$result = $stmt->execute(); foreach($result as $val){ echo $val->item_name; }
Don't really see what you mean Object Manipulator with the fields. I'm not trying to make a search box or anything, just allow them to access pastes through the URL like the example in the OP. Maybe I'm stupid, not sure, but where do the fields fit in?
0
// save result in a variable and then run a loop and echo 
$result = $stmt->execute(); 

foreach($result as $val){ 
    echo $val->item_name; 
}

5 Comments

Trying to use that in my code but it isn't working. This is how it looks with it: <?php require 'connection.php'; $getid = $_GET["id"]; $sql = "SELECT * FROM pasteinfo WHERE id = ?"; $stmt = $con->prepare($sql); $stmt->bind_param("i", $getid); $result = $stmt->execute(); // save result in a variable and then run a loop and echo foreach($result as $val){ echo $val->item_name; } ?>
remove this // save result in a variable and then run a loop and echo
@Rafij nothing's happened
run this line echo " <pre>" ; print_r($result);
@Rafij the web page displays '1'

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.