Error in php select pdo

<?php
 include ("./inc/header.inc.php");
$userid="";
$dblinks = DB::query('SELECT url, title FROM links WHERE user_id!=:userid ORDER BY id DESC', array(':userid'=>$userid));
$links = "";
$links_t = "";
foreach ($dblinks as $u) {
    $links .= $u['url'];
    //$links .= $
}
    foreach ($dblinks as $t) {
            $links_t .= $t['title']."<hr /></br />";

}
?>

<div class="links">
<?php echo $links;
echo $links_t;
?>
 </div>

Open in new window


The output is coming as all the urls one by one and the title one by one. I want url and title to be together then a <hr> then next url and title and so on. any help
sanchit guptaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mukesh YadavFull Stack DeveloperCommented:
Try this ;)

For that you need a single loop:

foreach ($dblinks as $u) {
    $links .= $u['url'] . ' ' . $t['title']."<hr /></br />";
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Peos JohnPHPCommented:
You do not need to have two foreach loops for this already $dblinks will have both

foreach ($dblinks as $u) {
    $links .= $u['url'].''.$u['title'].'<hr>' ;
    //$links .= $
}

Open in new window


If you can what is the html output you are trying to achieve, it will be better to understand and construct that.
0
Ray PaseurCommented:
Some good "getting started" examples for PHP and MySQL when using PDO are available in this article
https://www.experts-exchange.com/articles/11177/PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html
0
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

sanchit guptaAuthor Commented:
i also want to make a link to the url both from title and url itself
0
Peos JohnPHPCommented:
i also want to make a link to the url both from title and url itself

Can you post a sample output you are looking for?
0
Ray PaseurCommented:
Hmm... If you're new to PHP and want to learn the language, this article can help.  You should have a working knowledge of HTML before you start with PHP.
https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html

Try something like this.  I can't test it because I don't have your database, but I think it's at least close to right in principle.
<?php // demo/temp_sanchit.php
/**
 * https://www.experts-exchange.com/questions/29007039/Error-in-php-select-pdo.html
 */
error_reporting(E_ALL);

include ("./inc/header.inc.php");

$userid="";
$dblinks = DB::query('SELECT url, title FROM links WHERE user_id!=:userid ORDER BY id DESC', array(':userid'=>$userid));

$html = NULL;
foreach ($dblinks as $row)
{
    $out
    = '<a href="'
    . $row['url']
    . '">'
    . $row['url']
    . '</a>'
    . ' '
    . '<a href="'
    . $row['url']
    . '">'
    . $row['title']
    . '</a>'
    . '<hr>'
    . PHP_EOL
    ;
    $html .= $out;
}

echo '<div class="links">';
echo $html;
echo '</div>';

Open in new window

0
sanchit guptaAuthor Commented:
@PeosJohn this particular thing
0
Ray PaseurCommented:
stale question
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.