Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

PHP 

PHP Echo Mysql Data from 2 tables BY newest data

I asked stackoverflow but so far no one responded :(

So I am trying to echo the 10 newest data from 2 different tables. Ive tried

Code
$stmt60 = $db->prepare("SELECT * FROM 54x2 ORDER BY date_time DESC LIMIT 5");
$stmt60->execute();
$stmt61 = $db->prepare("SELECT * FROM dd ORDER BY date_time DESC LIMIT 5");
$stmt61->execute();
$row60 = $stmt60->fetch();
$row61 = $stmt61->fetch();

$var = array($row60['date_time'], $row62['date_time'], $row64['date_time'], $row66['date_time'], $row68['date_time'], $row70['date_time'], $row72['date_time'], $row74['date_time'], $row76['date_time'], $row78['date_time']);
$var7 = array($row61['date_time'], $row63['date_time'], $row65['date_time'], $row67['date_time'], $row69['date_time'], $row71['date_time'], $row73['date_time'], $row75['date_time'], $row77['date_time'], $row79['date_time']);

did that about 10 times with offsets increasing by 1 then added them to arrays then did like a 100 if statements checking whcih one is newer but ive tried for hours doing that and I couldn't get it to work. Is there an easier way of doing this without making 100-200 if statements?
I think you need for each loop there

Sent from mobile
Proud to be 100.000 user on HF

[Image: 4tAQ4Gf.png]
Please change your variable names. You can't just keep them like that.

And yes, you need to do a for/while/foreach loop.
OP try this:
PHP Code

<?php
$stmt60 
= $db->prepare("SELECT * FROM 54x2 ORDER BY date_time DESC LIMIT 5");
$stmt60->execute();
$row60 = $stmt60->fetch();

$stmt61 = $db->prepare("SELECT * FROM dd ORDER BY date_time DESC LIMIT 5");
$stmt61->execute();
$row61 = $stmt61->fetch();

echo 
"<u>Datas from 54x2:</u> <br/> <br />";
foreach(
$row60 as $item1){
    echo 
$item1['date_time']. '<br />';
}

echo 
"<u>Datas from dd:</u><br /> <br />";
foreach(
$row61 as $item2){
    echo 
$item2['date_time']. '<br />';
} 
[Image: vibesignature.gif]


“Whatever Your Mind Can Conceive and Believe, It Can Achieve.” – Napoleon Hill
(Apr 15, 2016 04:09 PM)newQuery Wrote:  OP try this:
PHP Code

<?php
$stmt60 
= $db->prepare("SELECT * FROM 54x2 ORDER BY date_time DESC LIMIT 5");
$stmt60->execute();
$row60 = $stmt60->fetch();

$stmt61 = $db->prepare("SELECT * FROM dd ORDER BY date_time DESC LIMIT 5");
$stmt61->execute();
$row61 = $stmt61->fetch();

echo 
"<u>Datas from 54x2:</u> <br/> <br />";
foreach(
$row60 as $item1){
    echo 
$item1['date_time']. '<br />';
}

echo 
"<u>Datas from dd:</u><br /> <br />";
foreach(
$row61 as $item2){
    echo 
$item2['date_time']. '<br />';
} 

Wont that always put dd table on the bottom though
(Apr 15, 2016 04:42 PM)CodeBinary Wrote:  Wont that always put dd table on the bottom though

Yes, all the datas from 54x2 would be at the top and all the datas from dd would be under the 54x2's datas
[Image: vibesignature.gif]


“Whatever Your Mind Can Conceive and Believe, It Can Achieve.” – Napoleon Hill
(Apr 15, 2016 04:46 PM)newQuery Wrote:  Yes, all the datas from 54x2 would be at the top and all the datas from dd would be under the 54x2's datas

Oh im trying to put which ever one is newer on the top :/
PHP Code

<?php 
$sql 
= "SELECT 1.date_time, 2.date_time FROM 54x2 AS 1, dd AS 2 ORDER BY date_time DESC LIMIT 5";

$stmt = $db->prepare($sql);
$stmt->execute();

$row = $stmt->fetchAll();

foreach(
$row as $item){
      echo 
$item['date_time']. '<br />';
} 

Did this from mobile, let me know if it's working
[Image: vibesignature.gif]


“Whatever Your Mind Can Conceive and Believe, It Can Achieve.” – Napoleon Hill
(Apr 15, 2016 05:26 PM)newQuery Wrote:  
PHP Code

<?php 
$sql 
= "SELECT 1.date_time, 2.date_time FROM 54x2 AS 1, dd AS 2 ORDER BY date_time DESC LIMIT 5";

$stmt = $db->prepare($sql);
$stmt->execute();

$row = $stmt->fetchAll();

foreach(
$row as $item){
      echo 
$item['date_time']. '<br />';
} 

Did this from mobile, let me know if it's working

Got an error:

Code
Fatal error: Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1, dd AS 2 ORDER BY date_time DESC LIMIT 5' at line 1 in C:\wamp\www\runewin\home.php on line 200


Possibly Related Threads...
Thread / Author Replies Views Last Post
[PHP] by killaxiaJun 24, 2021 02:42 PM 16 455 Jun 24, 2021 02:42 PM
Last Post: BuyingSpade
[PHP] by blackcoder00Feb 17, 2019 04:13 PM 1 164 Feb 17, 2019 04:13 PM
Last Post: Abruptum
[PHP] by EffexionJan 23, 2019 12:10 PM 6 273 Jan 23, 2019 12:10 PM
Last Post: Effexion