Return One Value from Database with MySQL PHP Pdo

return one value from database with mysql php pdo

You could create a function for this and call that function each time you need a single value. For security reasons, avoid concatenating strings to form an SQL query. Instead, use prepared statements for the values and hardcode everything else in the SQL string. In order to get a certain column, just explicitly list it in your query. a fetchColumn() method also comes in handy for fetching a single value from the query

function getSingleValue($conn, $sql, $parameters)
{
$q = $conn->prepare($sql);
$q->execute($parameters);
return $q->fetchColumn();
}

Then you can simply do:

$name = getSingleValue($conn, "SELECT name FROM login_users WHERE id=?", [$userid]); 

and it will get you the desired value.

So you need to create that function just once, but can reuse it for different queries.

This answer has been community edited addressing security concerns

php pdo get only one value from mysql; value that equals to variable

You have to use the fetch function according to your desired result.

  1. If you need a single scalar value, no column name is ever needed:

     $Number1 = $stmt->fetchColumn();
  2. If there are many results to be returned, but only one column per row, no need for the column name again:

     $numbers = $sth->fetchAll(PDO::FETCH_COLUMN);

    It will return an array of numbers.

  3. If you need a single row with multiple values, then use fetch()

  4. If you need array of rows, use fetchAll()

Fetching single row, single column with PDO

Are you sure it's returning any rows?

$stmt->fetchColumn()

is correct way to fetch a single value, so either you probably didn't bind the :user parameter or it simply returned no rows.

Store PDO Values in PHP Variable

So you need to return it..

$st = $pdo->prepare("SELECT * FROM user_tbl WHERE user_id= :user_id");
$st->bindParam(':user_id', $user_id);
$st->execute();

$return = $st->fetch(PDO::FETCH_OBJ);

$return $return;

Then when calling your class in say index.php..

$var = new ClassName();
$callUser = $var->PublicFunctionTitle($user_id);

Then you can run things like <?php echo $callUser->status;?>

PHP PDO returning single row

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

$row  = $STH -> fetch();

example (ty northkildonan):

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

Use PDO to extract value from database

You haven't executed $stmtt, so there's no result set to fetch.

$stmtt = $dbh->prepare("SELECT auth FROM auth where id=1");
$stmtt->execute();
$code = $stmtt->fetch(PDO::FETCH_ASSOC);

Get values from database using PHP PDO and update input to checked

You were really close, you did not fetch properly:

require("config.php"); 
if(empty($_SESSION['user']['id']))
{
header("Location: index.php");
die("Redirecting to index.php");
}

$userid = $_SESSION['user']['id'];

$sql = "SELECT my_music, my_movies, my_weather, my_maps, my_news
FROM user_preferences
WHERE user_id = :userID";

$stmt = $db->prepare($sql);
$stmt->bindParam(":userID", $userid);
$stmt->execute();

$result = $stmt->fetch();
  • You bind Params on the statement object not the connection
  • You fetch on the statement as well not the connection
  • fetchAll returns a 2 dimensional array if you want to see the content use var_dump not echo

<input id="mymusic"
type="checkbox"
name="mymusic"
<?php echo ($result['my_music']==1 ? 'checked' : '');?>
/>

<input id="mymovies"
type="checkbox"
name="mymovies"
<?php echo ($result['mymovies']==1 ? 'checked' : '');?>
/>


Related Topics



Leave a reply



Submit