Error in php select pdo

sanchit gupta
sanchit gupta used Ask the Experts™
on
<?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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Full Stack Developer
Commented:
Try this ;)

For that you need a single loop:

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

Open in new window

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.
Most Valuable Expert 2011
Top Expert 2016

Commented:
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
OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

Author

Commented:
i also want to make a link to the url both from title and url itself
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?
Most Valuable Expert 2011
Top Expert 2016
Commented:
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

Author

Commented:
@PeosJohn this particular thing
Most Valuable Expert 2011
Top Expert 2016

Commented:
stale question

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial