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.
If you need a single scalar value, no column name is ever needed:
$Number1 = $stmt->fetchColumn();
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.
If you need a single row with multiple values, then use
fetch()
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
notecho
<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
In PHP, How to Call a Function Interpolated in String
Laravel Many to Many Self Referencing Table Only Works One Way
Laravel Orm from Self Referencing Table Get N Level Hierarchy JSON
iOS Push Notification Does Not Work When Using Crontab Scheduler
How Does {} Affect a MySQL Query in PHP
Root Path Doesn't Work with PHP Include
Get the Metadata of an Order Item in Woocommerce 3
Array_Push() VS. $Array[] = .... Which Is Fastest
How to Determine Whether It's a Mobile Device with PHP
Post Arrays Not Showing Unchecked Checkboxes
Enabling Cors in Codeigniter ( Restserver by @Chriskacerguis )
PHP & SQL Injection - Utf8 Poc