How to store SQL query result in a variable using PHP?

camnesia

I want to store the first query result in a variable and use the first query result as a value in the second query.

Currently the second query works on its own. I am just having difficulties getting the first one to work.

$var = array();$idorigin = $conn->prepare("SELECT id_stop FROM stops WHERE
stopname = '$value1'");
$idorigin->execute()
$var[] = $idorigin['id_Stop'];
echo $var;

$stmt = $conn->prepare("SELECT id_stop, scheduletime FROM schedule 
WHERE scheduletime >= '$time' AND id_stop = '8'
LIMIT 6;");


$stmt->execute();
echo $stmt;
Gabriel Bourgault

You could use something like this for your first statement :

$req = $conn->prepare("SELECT id_stop FROM stops WHERE stopname = :value");
$req->execute(array('value' => $value1));
$result = $req->fetchAll();
$req->closeCursor();
var_dump($result);
  1. First you prepare your query
  2. Execute it with the corresponding parameters
  3. Fetch all records from the DB and store the result in any variable (here $result)
  4. Close connection with server, which allows to run other queries after
  5. Display the content of your variable with var_dump

Hope this helps

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Why is PHP using a lot of memory to store a query result

From Dev

How to store query result (a single document) into a variable?

From Dev

Cannot store result into variable SQL

From Dev

How to save the result of a SQL query into a variable in VBA?

From Dev

How to store result of stored procedure in a variable using SQL Server

From Dev

How do I store a mongodb query result in a variable?

From Dev

Store a sql query result in pentaho variable

From Dev

Store the first result value into a variable from sql query in php

From Dev

How to store the value of a SQL box into a PHP variable

From Dev

How to use a $result variable with table object in SQL query using mySQLi

From Dev

Store the whole query result in variable using postgresql Stored procedure

From Dev

How to create session for sql query result in php

From Dev

using switch statement in php with sql query result

From Dev

Store the result of a Dynamic Query in a variable

From Dev

How to store SQL Query result in table column

From Dev

How to store a query result into a variable in mysql and then use it another query and echo result?

From Dev

JSP,mysql - How to get and store variable from a fresh query result

From Dev

Store the first result value into a variable from sql query in php

From Dev

How to calculate the Result Number using query SQL

From Dev

How to store and process result of a query in PHP array?

From Dev

How to store the result of a Firebase query to a variable from a promise?

From Dev

how to store the query result into a variable in mysql

From Dev

How to store the result of dynamic SQL in a variable?

From Dev

How to store query result in a variable in MySql stored Procedure

From Dev

store MySQL SELECT result in php variable using Prepared statements

From Dev

how to put a result of query in variable with PHP

From Dev

How to store the result of a SQL statement as a variable and use the result in an SSIS Expression?

From Dev

How to store SensorThings query result into a variable in JavaScript/jQuery

From Dev

How to store a query result in a variable

Related Related

  1. 1

    Why is PHP using a lot of memory to store a query result

  2. 2

    How to store query result (a single document) into a variable?

  3. 3

    Cannot store result into variable SQL

  4. 4

    How to save the result of a SQL query into a variable in VBA?

  5. 5

    How to store result of stored procedure in a variable using SQL Server

  6. 6

    How do I store a mongodb query result in a variable?

  7. 7

    Store a sql query result in pentaho variable

  8. 8

    Store the first result value into a variable from sql query in php

  9. 9

    How to store the value of a SQL box into a PHP variable

  10. 10

    How to use a $result variable with table object in SQL query using mySQLi

  11. 11

    Store the whole query result in variable using postgresql Stored procedure

  12. 12

    How to create session for sql query result in php

  13. 13

    using switch statement in php with sql query result

  14. 14

    Store the result of a Dynamic Query in a variable

  15. 15

    How to store SQL Query result in table column

  16. 16

    How to store a query result into a variable in mysql and then use it another query and echo result?

  17. 17

    JSP,mysql - How to get and store variable from a fresh query result

  18. 18

    Store the first result value into a variable from sql query in php

  19. 19

    How to calculate the Result Number using query SQL

  20. 20

    How to store and process result of a query in PHP array?

  21. 21

    How to store the result of a Firebase query to a variable from a promise?

  22. 22

    how to store the query result into a variable in mysql

  23. 23

    How to store the result of dynamic SQL in a variable?

  24. 24

    How to store query result in a variable in MySql stored Procedure

  25. 25

    store MySQL SELECT result in php variable using Prepared statements

  26. 26

    how to put a result of query in variable with PHP

  27. 27

    How to store the result of a SQL statement as a variable and use the result in an SSIS Expression?

  28. 28

    How to store SensorThings query result into a variable in JavaScript/jQuery

  29. 29

    How to store a query result in a variable

HotTag

Archive