Okay I have a list being populated and echoed out on a page.
$sql = "SELECT * FROM `game_toe` WHERE `owner`='$mech_units'";
$mydata = mysql_query($sql);
while($record = mysql_fetch_array($mydata)){
echo "<td>" . $record['units'] . "</td>";
Now the results fluctuate depending on the number of 'mech_units' there are. What I need is to display how many are being displayed in the list. Any suggestions?
you can use built in function mysql_num_rows($mydata). This will give you the total number of records that are fetched.
First of all, I would suggest using mysqli.
You could declare a variable which increases by one every time you echo a 'mech_unit'.
$sql = "SELECT * FROM `game_toe` WHERE `owner`='$mech_units'";
$mydata = mysql_query($sql);
$i = 0;
while($record = mysql_fetch_array($mydata)){
$i++;
echo "<td>" . $record['units'] . "</td>";
}
echo "There are " . $i . " mech_units.";
Another option would be to use the mysql_num_rows() function.
Related
Okay I have list being populated and echoed out on a page
$sql = "SELECT * FROM `game_toe` WHERE `owner`='$mech_units'";
$mydata = mysql_query($sql);
while($record = mysql_fetch_array($mydata)){
echo "<td>" . $record['upkeep'] . "</td>";
Now the results fluctuate depending on the number of 'mech_units' there are. What I need is to display the sum of the Upkeep of the units being displayed. Any suggestions?
You can use array_sum function
$sql = "SELECT * FROM `game_toe` WHERE `owner`='$mech_units'";
$mydata = mysql_query($sql);
$upkeep = []; // Empty array to store all retreived values
while($row = mysql_fetch_array($mydata)){
echo "<td>" . $row['upkeep'] . "</td>";
$upkeep[] = $row['upkeep']; // Fill array with your values
}
echo array_sum($upkeep); // Now just coun array sum from temporary array
EDIT: I made some edit to fit your exact needs.
I am using this
PHP Ajax example from w3schools.This is working fine but it's not an efficient solution when the record list will be huge.So i want to add pagination to handle large records.How can i add pagination (Ajax pagination so that i can avoid page reloading) with this?
Here is my code:
<?php
$q = intval($_GET['q']);
$con = mysqli_connect('localhost','peter','abc123','my_db');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"ajax_demo");
$sql="SELECT * FROM user WHERE type_no = '".$q."'";
$result = mysqli_query($con,$sql);
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
<th>Job</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "<td>" . $row['Age'] . "</td>";
echo "<td>" . $row['Hometown'] . "</td>";
echo "<td>" . $row['Job'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
What I normally do is use the LIMIT feature in mysql. When I look at your example I would assume that the sql statement would only return one result.
Lets assume you had a table called user with many users if you had a query like this:
SELECT * FROM user
You would get all the users back in one query. This is not practical for most uses. The most elegant solution in my opinion is the LIMIT feature.
For example if I wanted to see results in a range:
SELECT * FROM user LIMIT 0,50 #return first 50 results - records 0 to 49
SELECT * FROM user LIMIT 50,50 #return the next 50 results - records 50 to 99
LIMIT syntax looks like this LIMIT <offset>,<count> Where offset is the starting result (starts at 0) and count is the number of results you want back.
So lets assume you had pages size of 50 and and were on the 5th page you would want:
LIMIT 100,25
Handling this at the database level is the simplest and quickest method.
I'm struggling to reference the MySQL data simultaneously by both column name and individual row number.
The code sample below does the following;
Successfully
prints the correct data to a table when looping through the whole table, referencing columns by name OR number.
when referencing individual rows in the database the correct columns by the column number
But I can't get it to successfully reference individual rows (by number) and columns (by name) at the same time.
Apologies if this is a ridiculous question, I'm new to PHP and MySQL.
I think my description is abysmal, so maybe this will help. Of the four options available (column either by name or number, and rows either individually or looping through a group) it works like this;
Column by number, each row in a loop. Yes.
Column by name, each row in a loop. Yes.
Column by number, individual row. Yes.
Column by name, individual row. NO! (as shown in the last line of code, a is a column in my database)
Does anyone please have any advice on what I'm doing wrong?
Code:
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM Persons");
echo "<table border='1'>
<tr>
<th>Firstname</th>
<t
h>Lastname</th>
<th>test</th>
<th>order</th>
</tr>";
$ind = 0;
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['firstname'] . "</td>";
echo "<td>" . $row['lastname'] . "</td>";
echo "<td>" . ($row['age'] * 1) . "</td>";
echo "<td>" . ($row['a']) . "</td>";
echo "<td>" . $ind. "</td>";
echo "</tr>";
$ind += 1;
}
echo "</table>";
$sql="SELECT * FROM Persons ORDER BY PID";
if ($result=mysqli_query($con,$sql))
{
// Prints single entry AP
print "<br>";
$rowNumber = $id;
mysqli_data_seek($result, $rowNumber);//start row is whatever you need to be the first row
$row = mysqli_fetch_row($result);
print "<br>";
//-----------------------------------------------------
$mathString = $row[1];
$description = $row[4];
echo "math string:" . $mathString . "<br>";
echo "a is said to equal:" . $description . " - " . $row['a'];
I have the code bellow, it's ok but I want to be able to use for example the 4th value extracted from the database, use it alone, not put all of them in a list, I want to be able to use the values from database individually. How do I echo them?
Edit: I was thinking to simplify things, to be able to add the values from database into one array and then extract the value I need from the array (for example the 4th - ordered by "order_id"). But how?
Right now I can only create a list with all the values one after the other..
(Sorry, I am new to this). Thank you for all your help..
<?php
include '../../h.inc.php';
$con = mysql_connect($db['host'],$db['user'],$db['passwd']);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("database", $con);
$result = mysql_query("SELECT * FROM options WHERE Name LIKE 'x_swift%' ORDER BY order_id");
echo "<table border='1'>
<tr>
<th>Values</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
// echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . $row['VALUE'] . "</td>";
echo "</tr>";
$array = array(mysql_fetch_array($strict));
}
echo "</table>";
mysql_close($con);
?>
To select the value in the value column of the row where order_id is 4, use this SQL:
$query = 'select value from options where order_id = 4';
Then you can access this result in many ways. One is to get the entire result row (which in this case is just one cell) as an associative array:
if ($result = mysql_query($query)) {
$row = mysql_fetch_assoc($result);
echo 'value = ' . $row['value'];
}
You can also get the value directly:
if ($result = mysql_query($query)) {
echo 'value = ' . mysql_result($result, 'value');
}
It would just be a query like...
$result = mysql_query("SELECT * FROM options WHERE ID = 3");
mysql_fetch_row($result);
Unless Im misunderstanding you....let me know
But you really should use PDO, instead of deprecated mysql_* functions
http://php.net/manual/en/book.pdo.php
I am trying to add 3 combo boxes which all display the exact same information that comes from my MySQL db. It seems like the code I wrote makes the entire page wait until all 3 combo boxes are populated, before continuing.
<?
$query = "Select * from tblWriters order by surname";
for ($i = 1; $i <= 3; $i++) {
$result = mysql_query($query);
echo "<tr><td>Writer".$i." *</td><td>";
echo "<select name='txtWriter".$i."' style='width: 200px;'>";
echo "<option value ='' selected='selected'></option>";
while ($row = mysql_fetch_array($result))
{
echo "<option value ='" . $row['id'] . "'> " . $row['surname'] . ", " . $row['name'] . "</option>";
}
echo "</select><td></tr>";
}
?>
I would like to optimize this piece of code, so the query will not be executed 3 times, as I believe this is where the page slows down.
If I put
$result = mysql_query($query);
outside of the for loop, the 2nd and 3rd combo box do not populate. I tried looking into resetting the pointer of the result, but I can't seem to figure out how that works.
Also, is there a way I can reuse the while loop, so I don't have to execute it 3 times?
Can someone point me in the right direction?
I'm pretty new to PHP and trying to learn on my own. Any help would be much appreciated. Thanks!
If you move your mysql_query() out of the loop again, you can reset your mysql-result-pointer by using mysql_data_seek() at the beginning or end of your loop.
This will result in:
mysql_query($query);
for($i=1;$i<=3;$i++);
{
mysql_data_seek(0); // reset datapointer
// output querydata
}
I'm obliged however to point out that the mysql-extension is deprecated by now and you should use mysqli or pdo for new projects and code.
Cache the query result in an array, then generate your markup:
$query = "Select * from tblWriters order by surname";
$result = mysql_query($query);
$data = array();
while ($row = mysql_fetch_array($result))
{
$data[] = $row;
}
for ($i = 1; $i <= 3; $i++) {
echo "<tr><td>Writer".$i." *</td><td>";
echo "<select name='txtWriter".$i."' style='width: 200px;'>";
echo "<option value ='' selected='selected'></option>";
foreach ($data as $row) {
echo "<option value ='" . $row['id'] . "'> " . $row['surname'] .
", " . $row['name'] . "</option>";
}
echo "</select><td></tr>";
}