Asked  1 Year ago    Answers:  5   Viewed   21 times

I am trying to output the variables that I get from the database in my query but nothing is being returned. Using MYSQLi prepared statements.

Please see code below:

$stmt = $con->prepare("SELECT first_name, last_name FROM transactions WHERE order_id = ?");
$stmt->bind_param('i', $order_id);
$stmt->execute(); 
$stmt->store_result();
$stmt->bind_result($first_name, $last_name);
$stmt->close();


// Output review live to page 
echo $first_name;

I cannot see where I am going wrong? PS I am new to prepared statements so please go easy on me!

 Answers

1

you forgetting the line to fetch the result. fetch() .

try that:

  $stmt->bind_result($first_name, $last_name);
  $stmt->fetch();  // ----- > you forget that line to fetch results.
  $stmt->close();
Thursday, April 1, 2021
 
Anax
 
5

Take out the single quotes around the ?. Your code should read:

$prep->prepare("SELECT * FROM details WHERE id REGEXP ?");
$prep->bind_param("s", $search_query);

As it stands now, you are passing in one param, but the ? in single quotes is treated as a string, not a parameter marker.

Saturday, May 29, 2021
5

I bet you are retrieving data in non-utf8 encoding: try to put mysql_query('SET CHARACTER SET utf8') before your SELECT query.

Tuesday, June 1, 2021
 
tplaner
 
3

It's possible to bind a true NULL value to the prepared statements (read this).

You can, in fact, use mysqli_bind_parameter to pass a NULL value to the database. simply create a variable and store the NULL value (see the manpage for it) to the variable and bind that. Works great for me anyway.

Thus it'll have to be something like:

<?php
    $mysqli = new mysqli('localhost', 'my_user', 'my_password', 'world');

    // person is some object you have defined earlier
    $name = $person->name();
    $age = $person->age();
    $nickname = ($person->nickname() != '') ? $person->nickname() : NULL;

    // prepare the statement
    $stmt = $mysqli->prepare("INSERT INTO Name, Age, Nickname VALUES (?, ?, ?)");

    $stmt->bind_param('sis', $name, $age, $nickname);
?>

This should insert a NULL value into the database.

Friday, June 11, 2021
 
tiny
 
4
   public void setContentView (View view)

Set the activity content to an explicit view. This view is placed directly into the activity's view hierarchy.

setContentView (View view) is a method of activity class. When activity is created you need to set the content to your activity.

onCreate(Bundle) is where you initialize your activity. Most importantly, here you will usually call setContentView(view) with a layout resource defining your UI, and using findViewById(int) to retrieve the widgets in that UI that you need to interact with programmatically.

Your implementation of onCreate() should define the user interface and possibly instantiate some class-scope variables.

Every resource like text view ,drawables when added in layout files will have an entry in R.java files The entry is automatic

Example

For activity_main in R.java

        public static final class layout {
        public static final int activity_main=0x7f030000;
        }

In your case your inflating the layout but not setting the content to the activity.

You need to set the content to your activity and then find the ids using findViewById(..).

If not you will get NullPointerException.

Saturday, July 31, 2021
 
samrap
 
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :