How to return a row count - php

I'm having trouble getting the row counts back from a MySQL query in PHP.
A link to the test page and the PHP code is as follows:
<?php
$con=* TESTED AND WORKING *
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$retDiv = "";
$counter = 0;
$result = mysqli_query($con,"SELECT * FROM `store` ORDER BY `num` DESC ");
$num_rows = mysql_num_rows($result);
while($row = mysqli_fetch_array($result))
{
if ($counter>=5)
{
break;
}
else
{
$retDiv = "<div id=\"hs" . $row['num'] . "\" class=\"store-item " . $row['Artist'] . "\">\n";
$retDiv = $retDiv . "<div id=\"". $row['Release'] . "-title\" class=\"album-title\">\n";
$retDiv = $retDiv . "<h1 align=\"center\" style=\"text-transform:uppercase;\">" . $row['Release'] . "</h1></br>\n";
$retDiv = $retDiv . "<h3 align=\"center\">" . $row['ReleaseName'] . "</h3>\n";
$retDiv = $retDiv . "</div>\n";
$retDiv = $retDiv . "<div id=\"" . $row['Release'] . "-description\" class=\"description\">\n";
$retDiv = $retDiv . "<table width=\"100%\">\n";
$retDiv = $retDiv . "<td width=\"50%\" style=\"padding-left:57px;padding-right:57px;padding-top:57px;padding-bottom:20px;\">\n";
$retDiv = $retDiv . "<a id=\"" . $row['Release'] . "DivLink\" href=\"javascript:;\" onmousedown=\"toggleDivStore('" . $row['Release'] . "-expand','" . $row['Release'] . "DivLink','" . $row['Release'] . "','" . $row['Release'] . "b','" . $row['Release'] . "')\"><img name=\"" . $row['Release'] . "-art\" src = \"images/" . $row['Release'] . ".jpg\" class=\"album-art\" onmouseover=\"this.src='images/" . $row['Release'] . "b.jpg'\" onmouseout=\"this.src='images/" . $row['Release'] . ".jpg'\"></a>\n";
$retDiv = $retDiv . "<td width=\"50%\" style=\"padding:20px;padding-top:0px;padding-bottom:0px;padding-left:0px;\">\n";
$retDiv = $retDiv . "<p style=\"\">" . $row['Description'] . "</p>\n";
$retDiv = $retDiv . "</td>\n";
$retDiv = $retDiv . "</table>\n";
$retDiv = $retDiv . "</div>\n";
$retDiv = $retDiv . "<div class=\"album-expand\" id=\"RELEASE-expand\" style=\"display:none;\">\n";
$retDiv = $retDiv . "</div>\n";
$retDiv = $retDiv . "<div =\"" . $row['Release'] . "PaypalContainer\" class=\"album-paypal-container\">\n";
$retDiv = $retDiv . $row['Paypal1'];
$retDiv = $retDiv . "</div>\n";
$retDiv = $retDiv . "</div>\n";
echo $retDiv;
}
$counter = $counter+1;
}
echo "<div id=\"retNum\"><script type=\"text/javascript\">numReleases =" . $num_rows . ";numPages=Math.ceil(numReleases/5)-1;</SCRIPT></div>";
mysqli_close($con);
How to return the row count as $num_rows to the retNum Javascript?

You're mixing mysqli (note the i) and mysql (note the LACK of an i). The two libraries and function bodies are NOT interoperable:
$num_rows = mysql_num_rows($result);
^--- missing an `i` here

When you are using mysqli_query() method to get the result set from the database, the number of rows from the result can be obtained by calling mysqli_num_rows() method which takes the mysqli_result set as the argument and do not use mysql_num_rows(). Check mysqli_num_rows()

Instead of using PHP logic to break out, why not add "LIMIT 5" to your sql query?

Related

wpdb->insert producing duplicates

I am trying to make a button on a page that prints out data from the database and then you can press 2 different buttons, one that deletes them from the database and the other one inserts it into another table in the database and deletes the data from the database, but it keeps inserting it twice into the new table and I have no clue why, this here prints out the data and session variables + buttons:
if(!isset($_POST['orderby'])) {
foreach ($requests as $row) {
echo "<div class='requests'>" . "<li class='refunds'>" . "Palauttajan nimi: ".
$row['customer_name'] . "</br>" ."Palautettavat tuotteet: ".$row['product_name']."<br> "."Määrä: ".
$row['product_qty'] . " "
. "<br>Kommentti: " . $row['comment'] . "<br> " . "Hinta: " . $row['refund_total'] . "€ " .
"<br>" . "Päivämäärä: " . $row['request_date'] . " " .
"<a class='right' href='admin-page?deleteid=" . $row['request_id'] . "'>Hylkää</a></li>" .
"<li class='refundaccepts'><a href='admin-page?acceptid=" . $row['request_id']
. "'>Hyväksy</a></li>" . "</div>";
$_SESSION['custname'] = $row['customer_name'];
$_SESSION['prodname'] = $row['product_name'];
}
} else {
foreach ($pergele as $row) {
echo "<div class='requests'>" . "<li class='refunds2'>" . "Palauttajan nimi: ".
$row['customer_name'] . "</br>" ."Palautettavat tuotteet: ".$row['product_name']."<br> "."Määrä: ".
$row['product_qty'] . " "
. "<br>Kommentti: " . $row['comment'] . "<br> " . "Hinta: " . $row['refund_total'] . "€ " .
"<br>" . "Päivämäärä: " . $row['request_date'] . " " .
"<a class='right' href='admin-page?deleteid=" . $row['request_id'] . "'>Hylkää</a></li>" .
"<li class='refundaccepts'><a href='admin-page?acceptid=" . $row['request_id']
. "'>Hyväksy</a></li>" . "</div>";
$_SESSION['custname'] = $row['customer_name'];
$_SESSION['prodname'] = $row['product_name'];
}
}
and this should insert it into the database once and delete the data from the old table:
if(isset($_GET['acceptid'])) {
$accept = $_GET['acceptid'];
$custname = $_SESSION['custname'];
$prodname = $_SESSION['prodname'];
/* Query to do whatever here */
$wpdb->insert("wp_acceptedrequests", [
"customer_name" => "$custname",
"name_product" => "$prodname",
"date" => date("Y/m/d/G:i:sa") ,
]);
$wpdb->query("DELETE FROM wp_refundrequests WHERE request_id = $accept");
}
What makes them insert twice and how do I prevent it from doing that?
I just ran into a similar situation where $wpdb inserts where being duplicated.
In my case it was happening if I was authenticated and browser inspector was open.

Border between rows in calling sql database

I'm not able to do borders between rows and I'm not sure why
Please help me.
<?php
$con = mysql_connect("localhost","root","");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("mysql", $con);
$result = mysql_query("SELECT * FROM `cloud team` WHERE `Vendor` = 'Juniper' ");
echo "<table align='center' bgcolor='#F9F0F0' border='#190707' cellspacing='#190707'>
<tr>
<th><font color='red'>Juniper</font></th>
</tr>";
while($row = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td align='center' style='vertical-align:text-top' width='500px'>";
echo "<a ='send.php'>" . " " . $row['Vendor'] . " " . $row['MGMT IP'] . " " . $row['Version'] . " " . $row['GUI User'] . " " . $row['GUI Pass'] . " " . $row['Notes'] . "</a></td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
the output of this code looks like this :
And I want borders between rows ..
Any help?
If you are looking for a border between the rows in the result, not rows in the html table, then just change your line from this
echo "<a ='send.php'>" . " " . $row['Vendor'] . " " . $row['MGMT IP'] . " " . $row['Version'] . " " . $row['GUI User'] . " " . $row['GUI Pass'] . " " . $row['Notes'] . "</a></td>";
to this
echo "<a ='send.php'>" . " </td.<td> " . $row['Vendor'] . " </td.<td> " . $row['MGMT IP'] . " </td.<td> " . $row['Version'] . " </td.<td> " . $row['GUI User'] . "</td.<td> " . $row['GUI Pass'] . "</td.<td> " . $row['Notes'] . "</a></td>";
And you really should use PDO or mysqli instead of mysql for the connection, as Strawberry kindly pointed out.

PHP pull from two MySQL tables, where multiple rows in table two

I have two tables that looks like this:
Put it in images since i dont know how to draw a table here.
My problem is that i can't seem to make a query or anything in my php that will allow me to load the report and the 5 images, so that i can display the images where i want to on the page. As i do it now it loads the report five times and one image in each report.
The edited code after #Terminus suggestions
$sql = "
SELECT *
FROM fangstrapporter AS f, rapportbilleder AS r
WHERE f.id=".htmlspecialchars($_GET["id"])." AND r.id=f.id";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
echo "<b>Overskrift:</b> " . $row["overskrift"] . "<br><br>" .
"<b>Sted:</b> " . $row["sted"] . "<br><br>" .
"<b>Fangstdato:</b> " . $row["fangstdato"] . "<br><br>" .
"<b>Agn:</b> " . $row["agn"] . "<br><br>" .
"<b>Kategori:</b> " . $row["kategori"] . "<br><br>" .
"<b>Art:</b> " . $row["art"] . "<br><br>" .
"<b>Vægt:</b> " . $row["vaegt"] . "<br><br>" .
"<b>Længde:</b> " . $row["laengde"] . "<br><br>";
do {
echo "<a href='" . $row["image_path"] . "'><img src='" . $row["image_thumb_path"] . "'></a><br><br>";
} while($row = $result->fetch_assoc());
echo $row["beskrivelse"]."<br>";
} else {
echo "0 results";
}
Can anyone help me do this? I have been searching on google for four days now, without any success.
Do as #Kenney suggested and remove the part where you echo the report from the loop.
$sql = "
SELECT *
FROM fangstrapporter AS f, rapportbilleder AS r
WHERE f.id=".htmlspecialchars($_GET["id"])." AND r.id=f.id";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
echo "<b>Overskrift:</b> " . $row["overskrift"] . "<br><br>" .
"<b>Sted:</b> " . $row["sted"] . "<br><br>" .
"<b>Fangstdato:</b> " . $row["fangstdato"] . "<br><br>" .
"<b>Agn:</b> " . $row["agn"] . "<br><br>" .
"<b>Kategori:</b> " . $row["kategori"] . "<br><br>" .
"<b>Art:</b> " . $row["art"] . "<br><br>" .
"<b>Vægt:</b> " . $row["vaegt"] . "<br><br>" .
"<b>Længde:</b> " . $row["laengde"] . "<br><br>";
$beskrivelse = $row["beskrivelse"];
do {
echo "<a href='" . $row["image_path"] . "'><img src='" . $row["image_thumb_path"] . "'></a><br><br>";
} while($row = $result->fetch_assoc());
echo $beskrivelse . "<br>";
} else {
echo "0 results";
}

Generated array values changing after While statements

This is a weird issue I have came across and was wondering if anyone my have insight. Not sure if the mktime does not function as I am trying to get it to, or what may be going on.
Last night, things were working fine - the months being displayed were correct. Today, though, for some reason the values of my $aGMonV are changing somewhere after the foreach and before the while(row_* = mysqli_fetch_array* statements.
While the var_dump returns %2014-03% as the first month (which is correct) - the table that is generated returns %2013-09% as the first month. All the queries being ran are being run with %2013-09% and NOT starting at current month.
My code is below:
$aGMon = array();
for ($i = 0; $i < 20; $i++)
{ $aGMon[] = date('Y-m', mktime(0,0,0,date('n')-$i,1)); }
foreach ($aGMon as $aGMonK => $aGMonV)
{
$aGMonO = $aGMonV;
$aGMonV = " '%" . $aGMonV . "%' ";
$result_E = mysqli_query($con,"select kWh_AVG from UseElecM where Month LIKE " . $aGMonV . ";");
$result_G = mysqli_query($con,"select TotalMCF from UseGas where Month LIKE " . $aGMonV . ";");
$result_P = mysqli_query($con,"select (A.Minutes+E.Minutes_L500+E.Minutes_Free) as Minutes, (A.Texts+E.Texts) as Texts, (A.MMS+E.MMS) as MMS, (A.MBData+E.MBData) as MBData from UseSprintA A left outer join UseSprintE E on A.Bill = E.Bill where A.Bill LIKE " . $aGMonV . ";");
$result_T = mysqli_query($con,"select cast((avg(Average)) as decimal (10,1)) as ATF from CF6MCI where Date LIKE " . $aGMonV . ";");
var_dump($aGMonV);
while($row_E = mysqli_fetch_array($result_E))
while($row_G = mysqli_fetch_array($result_G))
while($row_P = mysqli_fetch_array($result_P))
while($row_T = mysqli_fetch_array($result_T))
{
echo "<td class='UUMonth'>" . ($aGMonO) . "<div class='UUMonthO'>Average temperature: " . $row_T['ATF'] . " F</div></td>";
echo "<td>" . $row_E['kWh_AVG'] . "</td>";
echo "<td>" . $row_G['TotalMCF'] . "</td>";
echo "<td>" . $row_P['Minutes'] . "</td>";
echo "<td>" . $row_P['Texts'] . "</td>";
echo "<td>" . $row_P['MMS'] . "</td>";
echo "<td>" . $row_P['MBData'] . "</td>";
echo "</tr>";
}
}
Results of the code are as follows:
Result of code
user3260912 try removing the while, try like this:
$aGMon = array();
for ($i = 0; $i < 20; $i++)
{ $aGMon[] = date('Y-m', mktime(0,0,0,date('n')-$i,1)); }
foreach ($aGMon as $aGMonK => $aGMonV)
{
$aGMonO = $aGMonV;
$aGMonV = " '%" . $aGMonV . "%' ";
$result_E = mysqli_query($con,"select kWh_AVG from UseElecM where Month LIKE " . $aGMonV . ";");
$result_G = mysqli_query($con,"select TotalMCF from UseGas where Month LIKE " . $aGMonV . ";");
$result_P = mysqli_query($con,"select (A.Minutes+E.Minutes_L500+E.Minutes_Free) as Minutes, (A.Texts+E.Texts) as Texts, (A.MMS+E.MMS) as MMS, (A.MBData+E.MBData) as MBData from UseSprintA A left outer join UseSprintE E on A.Bill = E.Bill where A.Bill LIKE " . $aGMonV . ";");
$result_T = mysqli_query($con,"select cast((avg(Average)) as decimal (10,1)) as ATF from CF6MCI where Date LIKE " . $aGMonV . ";");
//var_dump($aGMonV);
$row_E = mysqli_fetch_array($result_E);
$row_G = mysqli_fetch_array($result_G);
$row_P = mysqli_fetch_array($result_P);
$row_T = mysqli_fetch_array($result_T);
echo "<td class='UUMonth'>" . ($aGMonO) . "<div class='UUMonthO'>Average temperature: " . $row_T['ATF'] . " F</div></td>";
echo "<td>" . $row_E['kWh_AVG'] . "</td>";
echo "<td>" . $row_G['TotalMCF'] . "</td>";
echo "<td>" . $row_P['Minutes'] . "</td>";
echo "<td>" . $row_P['Texts'] . "</td>";
echo "<td>" . $row_P['MMS'] . "</td>";
echo "<td>" . $row_P['MBData'] . "</td>";
echo "</tr>";
}

Echo image from folder together with database information

I'm doing a tutorial where you save an uploaded image to a folder on your computer while saving information about who uploaded it, title of image and time of upload, to a database.
When someone enter the page with the uploaded image, a small text with information that is stored in the database, will appear right above the image(thumb sized). When you click on the thumbnail fancybox will kick in and you will get the original size of the image.
Though, my problem is that the image itself gets echoed each time someone uploads an image.
So if three people upload you get a total of 9 thumbnails. If 4 people upload it will be 16 images etc. They do not copy itself in the folder. Check screenshot please.
http://s122.photobucket.com/user/KcMello/media/Ska3080rmavbild2014-02-14kl110753_zpsc19caae9.png.html
What I need help with is, that if someone could check through the code below and tell me what im doing wrong. I think it's glob but im not sure.
Thanks!
yours sincerely,
Winterwind
<?php
$dbcon = mysqli_connect("localhost","user1","test1","tutorial");
$selectall = "SELECT * FROM store";
$result = mysqli_query($dbcon, $selectall);
while($row = mysqli_fetch_array($result)){
$information = ' Titel: ' . $row['titel'] . ' Uppladdare: ' . $row['uppladdare'] . ' Filnamn: ' . $row['filname'] . ' Datum: ' . $row['date'];
echo "<strong>Titel: </strong>" . $row['titel'] . "<br>";
echo "<strong>Uppladdare: </strong>" . $row['uppladdare'] . "<br>";
echo "<strong>Filnamn/bild: </strong>" . $row['filname'] . "<br>";
echo "<strong>Datum: </strong>" . $row['date'] . "<br>";
echo "<br>";
foreach(glob("bilder/thumb_*.jpg") as $filename){
$original = substr($filename, 13);
echo "<a class='fancybox' rel='massoravbilder' href='bilder/$original'> <img src='$filename' alt='$information' /></a>" . "<br>";
}
}
?>
Update of code:
<?php
$dbcon = mysqli_connect("localhost","user1","test1","tutorial");
$selectall = "SELECT * FROM store";
$result = mysqli_query($dbcon, $selectall);
while($row = mysqli_fetch_array($result)){
$information = ' Titel: ' . $row['titel'] . ' Uppladdare: ' . $row['uppladdare'] . ' Filnamn: ' . $row['filname'] . ' Datum: ' . $row['date'];
echo "<strong>Titel: </strong>" . $row['titel'] . "<br>";
echo "<strong>Uppladdare: </strong>" . $row['uppladdare'] . "<br>";
echo "<strong>Filnamn/bild: </strong>" . $row['filname'] . "<br>";
echo "<strong>Datum: </strong>" . $row['date'] . "<br>";
echo "<br>";
error_reporting(0);
$original = substr($filename, 13);
echo "<a class='fancybox' rel='massoravbilder' href='bilder/$original'> <img src='bilder/thumb_" . $row['filname'] . "' alt='$information' /></a>" . "<br>";
}
?>
The finished code:
<?php
$dbcon = mysqli_connect("localhost","user1","test1","tutorial");
$selectall = "SELECT * FROM store";
$result = mysqli_query($dbcon, $selectall);
while($row = mysqli_fetch_array($result)){
$information = ' Titel: ' . $row['titel'] . ' Uppladdare: ' . $row['uppladdare'] . ' Filnamn: ' . $row['filname'] . ' Datum: ' . $row['date'];
echo "<strong>Titel: </strong>" . $row['titel'] . "<br>";
echo "<strong>Uppladdare: </strong>" . $row['uppladdare'] . "<br>";
echo "<strong>Filnamn/bild: </strong>" . $row['filname'] . "<br>";
echo "<strong>Datum: </strong>" . $row['date'] . "<br>";
echo "<br>";
error_reporting(0);
$original = $row['filname'];
echo "<a class='fancybox' rel='massoravbilder' href='bilder/$original'> <img src='bilder/thumb_" . $row['filname'] . "' alt='$information' /></a>" . "<br>";
}
?>
Many thanks to Jojo for helping me out!:)
Your Problem is that you are showing all images in folder "bilder" where the name starts with "thumb_" without checking if the image actually belongs to the row from your database table. Assuming that your naming pattern for the thumbnails is like this:
bilder/thumb_ . $row['filename'] . '.jpg
You could update your code to something like this (untested):
while($row = mysqli_fetch_array($result)){
$information = ' Titel: ' . $row['titel'] . ' Uppladdare: ' . $row['uppladdare'] . ' Filnamn: ' . $row['filname'] . ' Datum: ' . $row['date'];
echo "<strong>Titel: </strong>" . $row['titel'] . "<br>";
echo "<strong>Uppladdare: </strong>" . $row['uppladdare'] . "<br>";
echo "<strong>Filnamn/bild: </strong>" . $row['filname'] . "<br>";
echo "<strong>Datum: </strong>" . $row['date'] . "<br>";
echo "<br>";
echo "<a class='fancybox' rel='massoravbilder' href='bilder/$original'> <img src='bilder/thumb_" . $row['filename'] . "' alt='$information' /></a>" . "<br>";
}
A better idea IMO is to store the thumbnail-path in your database as well and use it to render your thumbnail-link.

Categories