Only store one result from PDO (SQL) in a variable [duplicate]

1 day ago 3
ARTICLE AD BOX

Just fetch. only gets one row. So no foreach loop needed :D

$row = $stmt->fetch();

example

$id = 4; $stmt = $dbh->prepare("SELECT figure FROM mytable WHERE id=? LIMIT 1"); $stmt->execute([$id]); $row = $stmt->fetch(PDO::FETCH_ASSOC); $figure = $row["figure"];

By the way, if you are selecting just one column, you can get its value right with fetch. Useful for getting values like count(*) etc:

$figure = $stmt->fetchColumn();

Your Common Sense's user avatar

answered Mar 28, 2011 at 8:56

mjspier's user avatar

6 Comments

Most optimized if you use "LIMIT 1" in your prepare statement.

2011-03-28T09:06:43.837Z+00:00

superbly, please give complete example. Not just a single line that has to go somewhere in the example above.

2014-06-25T06:28:04.347Z+00:00

@andrebruton $dbh = new PDO(" --- connection string --- "); $stmt = $dbh->prepare("SELECT name FROM mytable WHERE id=4 LIMIT 1"); $stmt->execute(); $row = $stmt->fetch();

2014-06-25T09:16:24.517Z+00:00

fetch(PDO::FETCH_ASSOC) will cut the returned array in half if you only need a key-value array.

2015-08-29T16:31:03.503Z+00:00

Do you really need to LIMIT 1 all the time though? Knowing that you're selecting a UNIQUE value comes to mind.

2017-02-01T03:10:25.97Z+00:00

$DBH = new PDO( "connection string goes here" ); $STH - $DBH -> prepare( "select figure from table1 ORDER BY x LIMIT 1" ); $STH -> execute(); $result = $STH -> fetch(); echo $result ["figure"]; $DBH = null;

You can use fetch and LIMIT together. LIMIT has the effect that the database returns only one entry so PHP has to handle very less data. With fetch you get the first (and only) result entry from the database reponse.

You can do more optimizing by setting the fetching type, see http://www.php.net/manual/de/pdostatement.fetch.php. If you access it only via column names you need to numbered array.

Be aware of the ORDER clause. Use ORDER or WHERE to get the needed row. Otherwise you will get the first row in the table alle the time.

answered Mar 28, 2011 at 8:55

strauberry's user avatar

Did you try:

$DBH = new PDO( "connection string goes here" ); $row = $DBH->query( "select figure from table1" )->fetch(); echo $row["figure"]; $DBH = null;

answered Feb 13, 2015 at 11:14

Bé Khỏe Bé Pro's user avatar

1 Comment

You could actually go further and do $row = $DBH->query($query)->fetch()['figure']. If fetch returns false because there are no results, PHP will silently ignore the invalid key reference. If something goes wrong with query, depending on how you've got your error handling set up, it will either throw an Exception (desired, in my opinion) or you'll get an Invalid object method reference "fetch" on "false".... error, a form of which you would have gotten anyway because the query obviously failed.

2016-11-01T17:40:43.68Z+00:00

You could try this for a database SELECT query based on user input using PDO:

$param = $_GET['username']; $query=$dbh->prepare("SELECT secret FROM users WHERE username=:param"); $query->bindParam(':param', $param); $query->execute(); $result = $query -> fetch(); print_r($result);

answered Jun 12, 2014 at 5:16

user3162468's user avatar

1 Comment

I think you are just having a bad day bro, or ur just looking to standout your profile. My answer corroborates with the same scenario using a variable for the SELECT function with a PDO query. I will delete the sql injection comment from my answer just to make your day (and mine).

2014-06-12T05:51:16.283Z+00:00

how about using limit 0,1 for mysql optimisation

and about your code:

$DBH = new PDO( "connection string goes here" ); $STH - $DBH -> prepare( "select figure from table1" ); $STH -> execute(); $result = $STH ->fetch(PDO::FETCH_ASSOC) echo $result["figure"]; $DBH = null;

answered Mar 28, 2011 at 8:55

KoolKabin's user avatar

Thanks to Steven's suggestion to use fetchColumn, here's my recommendation to cut short one line from your code.

$DBH = new PDO( "connection string goes here" ); $STH = $DBH->query( "select figure from table1" ); $result = $STH->fetchColumn(); echo $result; $DBH = null;

N'Bayramberdiyev's user avatar

answered Sep 2, 2013 at 4:36

jhloke's user avatar

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.

Read Entire Article