Welcome to WebmasterWorld Guest from 54.82.10.219

Forum Moderators: open

Message Too Old, No Replies

Using PHP to pull different values from DB

     
2:36 pm on Jun 29, 2016 (gmt 0)

Junior Member from CZ 

5+ Year Member

joined:Jan 29, 2010
posts: 46
votes: 0


Hi,

I am trying to learn how to use PHP code to pull values from a mysql DB: This is what I have so far:

$querya = ("SELECT * FROM day_pack LIMIT 1");
if (!$stmta = $con -> prepare($querya)) {
// prepare failed
echo "<pre>Prepare failed:\n";
print_r($con -> errorInfo());
echo "</pre>";
} else {
if (!$stmta -> execute(array($querya))) {
// execute failed
echo "<pre>Execute failed:\n";
print_r($stmta -> errorInfo());
echo "</pre>";
} else {
// query ran without any errors, you can test if it returned any rows and use them here

}
}

while ($rowa = $stmta -> fetch()) {
$pack = $rowa['pack'];
$size = $rowa['size'];
$colour = $row['colour'];

}


Then in column One of the table I have this:



[b]Column 1[/b]
<?php print "<li>". $pack ."</li>" ?>
<?php print "<li>". $size ."</li>" ?>
<?php print "<li>". $colour ."</li>" ?>



In Principle this works but I have 3 other columns in the table and want different values in the output according to the row ID I believe. To look like this:



<div class="wrap line-ver4">
<article class="col-1 indent">
<h4>Plan details</h4>
<ul class="info-list1">
<?php print "<li>". $pack ."</li>" ?>
<?php print "<li>". $size ."</li>" ?>
<?php print "<li>". $colour ."</li>" ?>
</ul>
</article>
<article class="col-2 indent">
<h4 class="aligncenter">Home</h4>
<ul class="info-list1 alt">
<?php print "<li>". $pack ."</li>" ?>
<?php print "<li>". $size ."</li>" ?>
<?php print "<li>". $colour ."</li>" ?>
</ul>
</article>
<article class="col-3 indent">
<h4 class="aligncenter">Home +</h4>
<ul class="info-list1 alt">
<?php print "<li>". $pack ."</li>" ?>
<?php print "<li>". $size ."</li>" ?>
<?php print "<li>". $colour ."</li>" ?>
</ul>


Hope this makes sense and someone can advise me on how to achieve this.
8:15 am on June 30, 2016 (gmt 0)

Junior Member from CZ 

5+ Year Member

joined:Jan 29, 2010
posts: 46
votes: 0


bump
3:11 pm on June 30, 2016 (gmt 0)

Moderator from US 

WebmasterWorld Administrator lifeinasia is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Dec 10, 2005
posts:5791
votes: 150


Can you provide some sample raw data and what you want the final output to look like?
5:10 pm on June 30, 2016 (gmt 0)

Senior Member from GB 

WebmasterWorld Senior Member andy_langton is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Jan 27, 2003
posts:3332
votes: 140


You should presumably fetch all of the matching results? It looks like you're grabbing a single row at the moment. E.g.

<?php
/* get all matching results */
$rows = $stmta -> fetchAll(PDO::FETCH_ASSOC);
/* loop through the matching results */
foreach ($rows as $row) {
?>
<tr><td><?php echo $row->fieldname1; ?></td><td><?php echo $row->fieldname2; ?></td><td><?php echo $row->fieldname3; ?></td></tr>
<?php } ?>
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members