Quantcast
Channel: DaniWeb Solved Topics
Viewing all articles
Browse latest Browse all 576

Two queries from table at the same time

$
0
0

Hi all,

I am working on a website that requires two queries from a table at the same time. They must select a row based on the id, and then another random one not including the row just selected. I currently have:

$query = $pdo->prepare('SELECT title, content FROM cards WHERE card_id=? LIMIT 1');
$query->execute([$slugId]);
$row = $query->fetch();

to select the row based on the id. But I am unsure how to get a random id, in that how do I go about it? Do I just do something along the lines of:

$randomQuery = $pdo->('SELECT title, content FROM cards ORDER BY RAND() WHERE card_id !=? LIMIT 1');
$randomQuery->execute([$slugId]);
$randomRow = $query->fetch();

or is there another way to do it? Because isn't using $pdo twice opening two connections to the database? Is this the correct way to do it or is there another way that will only use one connection?

TIA


Viewing all articles
Browse latest Browse all 576

Trending Articles