Viewed   55 times

Im not trying to use a loop. I just one one value from one column from one row. I got what I want with the following code but there has to be an easier way using PDO.

try {
        $conn = new PDO('mysql:host=localhost;dbname=advlou_test', 'advlou_wh', 'advlou_wh');
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    } catch(PDOException $e) {
        echo 'ERROR: ' . $e->getMessage();
    }

$userid = 1;

$username = $conn->query("SELECT name FROM `login_users` WHERE username='$userid'");
$username2 = $username->fetch();
$username3 = $username2['name'];

echo $username3;

This just looks like too many lines to get one value from the database. :

 Answers

2

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

Friday, August 19, 2022
5

The following works for me:

<?php

$pdo = new PDO("mysql:host=localhost;dbname=test", "root", "pass");
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

$stmt = $pdo->prepare("INSERT INTO `null_test` (`can_be_null`) VALUES (:null)");
$stmt->bindValue(":null", null, PDO::PARAM_NULL);

$stmt->execute();

Pass in PHP's null, with type of PDO::PARAM_NULL. Also, make sure your prepare emulation is set to false. That might help.

Sunday, August 7, 2022
1

lastInsertId() is a method of the PDO class, not the PDOStatement class.

This should work:

$groupID = $dbo->lastInsertId();
Friday, November 18, 2022
 
2

To get the lowest row per group you could use following

SELECT a.*
FROM historical_data a 
LEFT JOIN historical_data b ON a.name = b.name 
AND a.low > b.low
WHERE b.name IS NULL
AND DATE(a.date) >= '2017-12-19' AND DATE(a.date) <= '2017-12-25' 
AND a.name = 'omisego'

or

SELECT a.*
FROM historical_data a 
JOIN (
    SELECT name,MIN(low) low
    FROM historical_data
    GROUP BY name
) b USING(name,low)
WHERE  DATE(a.date) >= '2017-12-19' AND DATE(a.date) <= '2017-12-25' 
AND a.name = 'omisego'

DEMO

For last 30 day of 7 days or n days you could write above query as

SELECT a.*, DATE(a.`date`)
FROM historical_data2 a 
LEFT JOIN historical_data2 b ON a.name = b.name 
AND DATE(b.`date`) >= CURRENT_DATE() - INTERVAL 30 DAY 
AND DATE(b.`date`) <= CURRENT_DATE()
AND a.low > b.low
WHERE b.name IS NULL
AND a.name = 'omisego'
AND DATE(a.`date`) >= CURRENT_DATE() - INTERVAL 30 DAY
AND DATE(a.`date`) <= CURRENT_DATE()
;

DEMO

But note it may return more than one records where low value is same, to choose 1 row among these you have specify another criteria to on different attribute

Sunday, September 18, 2022
 
4

This for mysql workbench version 6.0 and for exporting a schema.

Select tab MySQL Model
Select File->Export->Forward Engineer SQL Create

Place a file name to be exported in the Output SQL Script File, choose your options, next

Export MySQL table Objects,  
filter tables 

Then a file is created which you can import to your database and it creates schemas if not exists, creates tables if not exists.

In the case of models, you need to pay, many bucks, for a tool that creates schemas, tables, indexes, cascading, and all stuff associated to an existing data model.

Tuesday, October 4, 2022
 
filu82
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :