Hack Forums
[PHP] PHP Echo Mysql Data from 2 tables BY newest data - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Web Development (https://hackforums.net/forumdisplay.php?fid=183)
+---- Thread: [PHP] PHP Echo Mysql Data from 2 tables BY newest data (/showthread.php?tid=5238767)



PHP Echo Mysql Data from 2 tables BY newest data - CodeBinary - Apr 15, 2016

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?


* - gua - Apr 15, 2016

I think you need for each loop there

Sent from mobile


* - Hash G. - Apr 15, 2016

Please change your variable names. You can't just keep them like that.

And yes, you need to do a for/while/foreach loop.


* - newQuery - Apr 15, 2016

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 />';
} 



* - CodeBinary - Apr 15, 2016

(Apr 15, 2016 04:09 PM)▷▶ ᶠᶸᶜᵏ ᵧₒᵤ ◀◁ 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


* - newQuery - Apr 15, 2016

(Apr 15, 2016 04:42 PM)Dashy 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


* - CodeBinary - Apr 15, 2016

(Apr 15, 2016 04:46 PM)▷▶ ᶠᶸᶜᵏ ᵧₒᵤ ◀◁ 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 :/


* - newQuery - Apr 15, 2016

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


* - CodeBinary - Apr 16, 2016

(Apr 15, 2016 05:26 PM)▷▶ ᶠᶸᶜᵏ ᵧₒᵤ ◀◁ 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