-1

I have used prepare, bind_param, and execute statements to query my database and get only the first column. In php, how would I store the 1st returned value as a string?

In this example of returned values, I would want to store value 1 as a string.

  • value 1
  • value 2
  • value 3

Actual code:

  //connect to database

  $var1 = a;
  $var2 = b;
  $var3 = c;
  $var4 = d;
  $var5 = e;


  $stmt = $mysqli->prepare("SELECT id FROM id_list WHERE variable1 = ? && variable2 = ? && variable3 = ? && variable4 = ? && variable5 = ?");
  $stmt->bind_param('sssss', $var1, $var2, $var3, $var4, $var5);

  $stmt->execute();
  //How would I get the result here?
  $stmt->bind_result($result);
  echo $result;
p1083997
  • 11
  • 4
  • where do you try to "store" the returned value from database? in a variable? Please clarify. Thanks. – Dody Apr 19 '17 at 03:40
  • ```reset($myDBResults);``` ? – Treast Apr 19 '17 at 03:45
  • @Dody I want to store it as a variable – p1083997 Apr 19 '17 at 04:05
  • It'd really help if you put in actual code that describes your problem rather than this vague overview of the situation. – tadman Apr 19 '17 at 04:07
  • Hint: Just like `bind_param`, which you're using properly, there's [`bind_result`](http://php.net/manual/en/mysqli-stmt.bind-result.php). Another thing to note is it's traditionally `AND` in SQL, and MySQL's `&&` is highly non-standard. – tadman Apr 19 '17 at 05:05
  • @tadman So if I do `$stmt->bind_result($result);` and call `$result[0]`, it will give me the first returned value? – p1083997 Apr 19 '17 at 05:07
  • Read the documentation and associated examples more closely. It works a lot like `bind_param`, so if you're selecting just `id` then you will get a simple value back. It's not an array. – tadman Apr 19 '17 at 05:12
  • @tadman Based on the documentation, I called `$result`, but it's returning blank. What could be wrong? Thank you for helping me with what must seem like basic questions. – p1083997 Apr 19 '17 at 05:17
  • Edit your question with your attempt, that way the mistake will be more obvious. – tadman Apr 19 '17 at 05:17
  • The code is incomplete could you please show all the code? specially the part where you retrieve the result from the data base. – Teocci Apr 19 '17 at 05:20
  • @Teocci I'm asking how to retrieve the result from the database. This is my full code. – p1083997 Apr 19 '17 at 05:22

2 Answers2

1

This code will complete your code and retrieve all the values that you need.

You can refer to the PHP Documentation for more info

Then you can replace the while with $result[0];

NOTE: don't forget to replace the values for the database connection. Your right values: 'my_user', 'my_password' and 'my_database'.

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

    /* check connection */
    if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }

    $stmt = $mysqli->prepare("SELECT id FROM id_list 
            WHERE 
            variable1 = ? && 
            variable2 = ? && 
            variable3 = ? && 
            variable4 = ? && 
            variable5 = ?");
    $stmt->bind_param('sssss', $var1, $var2, $var3, $var4, $var5);

    $var1 = a;
    $var2 = b;
    $var3 = c;
    $var4 = d;
    $var5 = e;

    /* execute prepared statement */
    $stmt->execute();

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

    echo 'Results:' . PHP_EOL;
    while($stmt->fetch()){
        echo $result . PHP_EOL;
    }

    $stmt->close();

    /* close connection */
    $mysqli->close();
?>
Teocci
  • 7,189
  • 1
  • 50
  • 48
0

If you can put some code example it could help. Anyway I'm supposing that you're getting db results into an array. So for example $myDBresults should be an array. Then you can save it into a variable as a string in this way:

$myVar = (string)$myDBresults[0]; //cast to string first element.

Community
  • 1
  • 1
Dody
  • 608
  • 7
  • 19