Forum Moderators: open

Message Too Old, No Replies

Using PHP to pull different values from DB

         

chrisguk

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

10+ Year Member



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.

chrisguk

8:15 am on Jun 30, 2016 (gmt 0)

10+ Year Member



bump

LifeinAsia

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

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Can you provide some sample raw data and what you want the final output to look like?

Andy Langton

5:10 pm on Jun 30, 2016 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



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 } ?>