php & pdo & mysql countrows returning 0 - php

I have now spent several hours without getting anywhere.
I want to display number of rows in my query and here here is my code:
<?php
$user_id = 1;
$statement = $dbConn->query("select badoo_interest.*, badoo_category.icon_class from badoo_interest, badoo_user_interest, badoo_category where badoo_user_interest.interest_id = badoo_interest.id and badoo_user_interest.user_id = :id_user_profile and badoo_category.id = badoo_interest.category_id");
$statement->execute(array(':id_user_profile'=> $id_user_profile));
$rResult = $statement->fetchAll();
echo count($rResult);
echo "<h2>Interesi</h2>";
while( $row = $statement->fetch(PDO::FETCH_ASSOC) ){
?>
<div class="interest" id = "int<?php echo $row['id'];?>">
<span class = "intr-ico <?php echo $row['icon_class'];?>"></span>
<?php echo $row['name'];?>
</div>
<?php
}
?>
And i getting number 10 but now im not getting any interests displayed.
If you use this code now:
<?php
//$user_id = 1;
$statement = $dbConn->query("select badoo_interest.*, badoo_category.icon_class from badoo_interest, badoo_user_interest, badoo_category where badoo_user_interest.interest_id = badoo_interest.id and badoo_user_interest.user_id = :id_user_profile and badoo_category.id = badoo_interest.category_id");
$statement->execute(array(':id_user_profile'=> $id_user_profile));
echo "<p>" . $statement->rowCount(). " interests:</p>";
while( $row = $statement->fetch(PDO::FETCH_ASSOC) ){
?>
<div class="interest" id = "int<?php echo $row['id'];?>">
<span class = "intr-ico <?php echo $row['icon_class'];?>"></span>
<?php echo $row['name'];?>
</div>
<?php
}
?>
And now im getting all the interests displayed but it counts: 0 interests.
Any ideas who i can count number of interests?

If you read the documentation for PDOStatement::rowCount you will see this line
If the last SQL statement executed by the associated PDOStatement was
a SELECT statement, some databases may return the number of rows
returned by that statement. However, this behaviour is not
guaranteed for all databases and should not be relied on for portable
applications.
The work around is to count() function from php like you did. If that did not work then use COUNT(*) as result_count. then when you fetch read that value.
And i getting number 10 but now im not getting any interests displayed
problem
The reason why the result aren't displaying is because you fetched all the row already so you cannot use fetch() again.
solution
Use the array returned by fetchAll() instead and use a foreach instead
$rows = $statement->fetchAll();
echo "<p>" . count($rows). " interests:</p>";
foreach ($rows as $row){
//echo $row['name']
}

<?php
//$user_id = 1;
$statement = $dbConn->query("select badoo_interest.*, badoo_category.icon_class from badoo_interest, badoo_user_interest, badoo_category where badoo_user_interest.interest_id = badoo_interest.id and badoo_user_interest.user_id = :id_user_profile and badoo_category.id = badoo_interest.category_id");
$statement->execute(array(':id_user_profile'=> $id_user_profile));
echo "<p>" . $statement->rowCount(). " interests:</p>";
$counter = 0; //declare counter
while( $row = $statement->fetch(PDO::FETCH_ASSOC) ){
$counter++; //increment counter
?>
<div class="interest" id = "int<?php echo $row['id'];?>">
<span class = "intr-ico <?php echo $row['icon_class'];?>"></span>
<?php echo $row['name'];?>
</div>
<?php
}
?>
[NOTE: Edit by #DinoAmino removed the emphasis characters around the two uses of $counter, which caused the code to not compile for the question asker]

I solved my problem like this:
Here is the updated solving version. Thank you all for the input:
<?php
$user_id = 1;
$statement = $dbConn->query("select badoo_interest.*, badoo_category.icon_class from badoo_interest, badoo_user_interest, badoo_category where badoo_user_interest.interest_id = badoo_interest.id and badoo_user_interest.user_id = :id_user_profile and badoo_category.id = badoo_interest.category_id");
$statement->execute(array(':id_user_profile'=> $id_user_profile));
$rows = $statement->fetchAll();
if(count($rows) >= '1') {
?>
<div class="your-interests">
<?php
echo "<p>" . count($rows). " interests:</p>";
foreach ($rows as $row){
?>
<div class="interest" id = "int<?php echo $row['id'];?>">
<span class = "intr-ico <?php echo $row['icon_class'];?>"></span>
<?php echo $row['name'];?>
</div>
<?php
}
?>
</div>
<?php } ?>

Related

multiple echo statements in one echo?

I am relatively new to PHP and want to know a more efficient way of echo'ing the following in less steps or even just one statement.
$sql = "SELECT * FROM comments";
$result = OpenCon()->query($sql);
while( $row = $result->fetch_assoc()) {
echo "<div class='card'><p class: card-text>";
echo $row['uid']."<br>";
echo $row['date']."<br>";
echo $row['message']."<br><br>";
echo "</p></div>";
As #droopsnoot has commented on above, you should try doing the next thing here.
while( $row = $result->fetch_assoc()) {
echo "<div class='card'><p class: card-text>". $row['uid']."<br>" . $row['date']."<br>".$row['message']."<br><br>". "</p></div>";
}
It should work perfectly.
Another option is to close php tag (?>) and use html markup with <?=$var?>:
<?php
$sql = "SELECT * FROM comments";
$result = OpenCon()->query($sql);
while( $row = $result->fetch_assoc()) {?>
<div class="card">
<p class="card-text">
<?=$row['uid']?><br>
<?=$row['date']?><br>
<?=$row['message']?><br><br>
</p>
</div>
<?php
} // end while

PHP Display Data In Horizontal Style From MySql

I am trying to display all my site members info with avatar and some information in members page of my WordPress site.For this, I have written following code.It is displaying data with one single user in a row.What I am trying is to display multiple users like 4 to 5 members horizontally in a single row then start 2nd row similar like this https://stackoverflow.com/users
Following is my code.how can achieve this
<?php
global $wpdb;
$result = $wpdb->get_results( "SELECT id,display_name as pt,user_registered as re FROM wp_users group by id"); /*mulitple row results can be pulled from the database with get_results function and outputs an object which is stored in $result */
foreach($result as $row)
{
echo '<table><tr>';
echo '<td>'.get_avatar( $row->id,40 );
echo '</td><td>'.$row->id." ".$row->pt. "<br>" .$row->re. "</td></tr>
</table>";
}
?>
I prefer to solve the problem by using an count and define a fixed number of columns.
<?php
global $wpdb;
$result = $wpdb->get_results( "SELECT id,display_name as pt,user_registered as re FROM wp_users group by id"); /*mulitple row results can be pulled from the database with get_results function and outputs an object which is stored in $result */
$count = count($result);
$columns = 5;
echo '<table><tr>';
foreach($result as $i => $row) {
echo '<td>' . get_avatar( $row->id,40 ) . '</td>';
echo '<td>' . $row->id . ' ' . $row->pt . '<br>' . $row->re . '</td>';
$i++;
if($i != $count && $i >= $columns && $i % $columns == 0)
echo '</tr><tr>';
}
echo '</tr></table>';
?>
Try with this,
<?php
global $wpdb;
$result = $wpdb->get_results( "SELECT id,display_name as pt,user_registered as re FROM wp_users group by id");
?>
<div class="container">
<?php foreach($result as $row) { ?>
<div class="row">
<div class="col-lg-3">
<?php get_avatar( $row->id,40 ); ?><br>
<?php echo $row->id." ".$row->pt;?><br>
<?php echo $row->re;?>
</div>
</div>
<?php } ?>
</div>

mysqli data fetch using explode no result getting

From table, i am fetching $rows['colorVariants']; but its value i am getting like:-
[JEADV9Q23ESG25HY,JEADV9Q2NFRNYV5Q,JEADV9Q2PNBTXGNX,JEADV9Q2XKWPXWSX,JEADY8ABWH9XNF4B,JEADZWEBDHWRJ2NQ,JEADZWEBRWZ4B4VS,JEADZWEBXDCGSYCF,JEAEYYX95YYC4DRA]
Then i used substr to remove square bracket.
Explode this value and again need to fetch it it the same table for similar products, but i am not getting any result.
<?php
$table = table1; // Table Name
$mysqli = mysqli connection // Mysqli Connection here
$result = $mysqli->query( "SELECT * FROM $table_name WHERE `id` = '$q' ");
while ( $rows = $result->fetch_assoc() ) {
$rows['colorVariants'] = [JEADV9Q23ESG25HY,JEADV9Q2NFRNYV5Q,JEADV9Q2PNBTXGNX,JEADV9Q2XKWPXWSX,JEADY8ABWH9XNF4B,JEADZWEBDHWRJ2NQ,JEADZWEBRWZ4B4VS,JEADZWEBXDCGSYCF,JEAEYYX95YYC4DRA] // Result Copy Sample
// substr using for remove square bracket
$colorVariants = substr($rows['colorVariants'], 1);
$newColorVariants = substr($colorVariants, 0, '-1');
$finalColorVariants = explode(',', $newColorVariants);
}
?>
<?php
for($i = 0; $i < count($finalColorVariants); $i++) {
$color = $finalColorVariants[$i];
$color = $mysqli->real_escape_string($color);
$colorVariants = $mysqli->query( "SELECT id,store,title,imageUrlStr,mrp,price,productBrand FROM $table_name WHERE `productId` = '".$color."' ");
if ($colorVariants) {
while ($rows = $colorVariants->fetch_assoc()) {
$id1 = $rows['id'];
$store1 = $rows['store'];
$title1 = $rows['title'];
$imageUrlStr1 = $rows['imageUrlStr'];
$mrp1 = $rows['mrp'];
$price1 = $rows['price'];
$productBrand1 = $rows['productBrand'];
}
?>
<div class="cloth-inner-similar-box">
<div class="cloth-inner-similar-boxin">
<img src="<?php echo $imageUrlStr1; ?>" />
</div>
<div class="cloth-inner-similar-boxin-offer">
<div class="cloth-inner-similar-boxin-offer-in">
<p>30% OFF</p>
</div>
</div>
<div class="cloth-inner-similar-boxin-head">
<?php echo $title1; ?>
</div>
<div class="cloth-inner-similar-price border-solid-bottom border-solid-top">
Best Buy # <?php echo $price1; ?>/-<?php echo $productBrand1; ?>
</div>
</div>
<?php
} }
$mysqli->close();
?>

PHP MySQL display data by id from database - freedom placement

I would like to have the freedom to place a row entry from my database wherever i' prefer in the page. Right now, the php code that I use is as follows (it is clean working code):
<html><head></head>
<body>
<?php
$db = mysql_connect("xxx","xxx","xxx") or die("Database Error");
mysql_select_db("caisafety",$db);
$id = $_GET['id'];
$id = mysql_real_escape_string($id);
$query = "SELECT * FROM `cert_rr` WHERE `id`='" . $id . "'";
$result = mysql_query($query);
echo $row['id']; while($row = mysql_fetch_array( $result )) {
echo "<br><br>";
echo $row['basic3'];
echo $row['basic2'];
echo $row['basic1'];
}
?>
</body>
</html>
I call id through the browser Eg. http://site.com/getid.php?id=10 . But I do not have the freedom to place my row entry within my html. For eg. like this:
<table><tr>
<td align="center">BASIC INFO 1: <?php echo $row['basic1']; ?></td>
<td align="center">BASIC INFO 2: <?php echo $row['basic2']; ?></td>
</tr></table>
I can place html within echo PHP tags but then I have to clean up my html and thats a lot of work. Retaining HTML formatting would be preferred. Any help or guidelines on this would be much appreciated.
<?php
$db = mysql_connect("xxx","xxx","xxx") or die("Database Error");
mysql_select_db("caisafety",$db);
$id = $_GET['id'];
$id = mysql_real_escape_string($id);
$query = "SELECT * FROM `cert_rr` WHERE `id`='" . $id . "'";
$result = mysql_query($query);
//you need to retrieve every row and save to an array for later access
for($rows = array(); $tmp = mysql_fetch_array($result);)
{
$rows[] = $tmp;
}
//now you can use the $rows array where every you want e.g. with the code from Zhube
?>
....
<table><?php foreach($rows as $r):
<td><?php echo $r['id'] ?></td><?php endforeach ?>
</table>
By
while($row = mysql_fetch_array( $result )) {
echo "<br><br>";
echo $row['basic3'];
echo $row['basic2'];
echo $row['basic1'];
}
you save only the last row
Instead of having your HTML tags as part of the PHP variable, do something like this instead:
<table><?php foreach($row as $r):?>
<td><?php echo $r['id'] ?></td><?php endforeach ?>
</table>

Concatenating a MySQL query and text

I have the code
$link = "group.php?=";
$fulllink;
while($row = mysql_fetch_array($result))
{
$other = $link.$row;
echo $row;
echo " $row[mygroup] ";
echo "</br>";
}
which I would like to link to each group's group.php page (such as group.php?=samplegroup). However, mysql_fetch_array returns an array, which I am unable to concatenate to the $link variable. What should I do?
You just have to access the array:
$other = $link.$row['mygroup'];
(or whatever the array keys are)
Your code a little bit nicer:
<?php
// other code
$link = "group.php?=";
?>
<?php while(($row = mysql_fetch_array($result))): ?>
<a href="<?php echo $link, $row['mygroup']; ?>">
<?php echo $row['mygroup']; ?>
</a>
</br>
<?php endwhile; ?>

Categories