good day guys i have my code here to get age from date of birth stored in my database.
$connect=mysql_connect("localhost","root","");
mysql_select_db("mydb",$connect);
$query = "select date_format(now(), '%Y') - date_format(dob, '%Y') - (date_format(now(), '00-%m-%d') < date_format(dob, '00-%m-%d'))
as age from data";
$result = mysql_query($query);
$values = mysql_fetch_assoc($result);
$age = $values['age'];
$query="select * from data";
$result=mysql_query($query);
if(mysql_num_rows($result)>0){
echo "<table align='center' border='1'>";
echo "<tr>";
echo "<th>id</th>";
echo "<th>Last Name</th>";
echo "<th>First Name</th>";
echo "<th>Age</th>";
echo "</tr>";
while($row=mysql_fetch_array($result)){
echo "<tr>";
echo "<td>".$row['id']."</td>";
echo "<td>".$row['lname']."</td>";
echo "<td>".$row['fname']."</td>";
echo "<td>".$age."</td>";
echo "</tr>";
}
echo "</table>";
}
all data is fetch but the only problem is the age of the first row is the same as the rest of my data. what wrong with my code? anybody help please thank you.
You are printing pre-fetched age from values array.
Instead read from current row and use to display.
Change your SQL query as below:
$query = "select id, lname, fname,
date_format(now(), '%Y') - date_format(dob, '%Y')
- ( date_format(now(), '00-%m-%d')
< date_format(dob, '00-%m-%d'))
as age
from data";
Change:
echo "<td>".$age."</td>";
To:
echo "<td>".$row['age']."</td>";
The problem is you are fetching the age for single time, get the dob in while loop and then calculate the age and print the age in while loop itself.
You are getting age once and using it for rest of your data.
Try something like this:
<?php
$connect = mysql_connect("localhost", "root", "");
mysql_select_db("mydb", $connect);
$query = "select * from data";
$result = mysql_query($query);
if (mysql_num_rows($result) > 0) {
echo "<table align='center' border='1'>";
echo "<tr>";
echo "<th>id</th>";
echo "<th>Last Name</th>";
echo "<th>First Name</th>";
echo "<th>Age</th>";
echo "</tr>";
while ($row = mysql_fetch_array($result)) {
$query1 = "select date_format(now(), '%Y') - date_format(dob, '%Y') - (date_format(now(), '00-%m-%d') < date_format(dob, '00-%m-%d'))
as age from data where id = $row[id]";
$result1 = mysql_query($query);
$values1 = mysql_fetch_assoc($result);
$age = $values1['age'];
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['lname'] . "</td>";
echo "<td>" . $row['fname'] . "</td>";
echo "<td>" . $age . "</td>";
echo "</tr>";
}
echo "</table>";
}
Considering your age formula is right, try this query.
// query will give all records and age for each record.
$query="SELECT a.*, (SELECT TIMESTAMPDIFF(YEAR,dob,NOW()) FROM data AS b WHERE a.id = b.id) AS age FROM data AS a";
// rather than getting two result sets, you have age in the same record now, access it as a field.
$result=mysql_query($query);
if(mysql_num_rows($result)>0){
echo "<table align='center' border='1'>";
echo "<tr>";
echo "<th>id</th>";
echo "<th>Last Name</th>";
echo "<th>First Name</th>";
echo "<th>Age</th>";
echo "</tr>";
while($row=mysql_fetch_array($result)){
echo "<tr>";
echo "<td>".$row['id']."</td>";
echo "<td>".$row['lname']."</td>";
echo "<td>".$row['fname']."</td>";
echo "<td>".$row['age']."</td>"; // get age from db results
echo "</tr>";
}
echo "</table>";
}
Related
I have 2 tables: users and transactions
In users I have: userid, name, email
And in transactions I have: id, idsender, idreceiver
I want to make a log-table showing all the transactions and I want them to be displayed like: Transaction ID - SENDER'S NAME - RECEIVER'S NAME
I tried like this but it doesn't seem to work, at "Receiver" it doesn't show anything .. :
echo "<table>";
echo "<tr>";
echo "<th>Transaction ID</th>";
echo "<th>Sender</th>";
echo "<th>Receiver</th>";
echo "</tr>";
$resultuser = mysqli_query($conn, "SELECT * FROM users");
$rowuser=mysqli_fetch_array($resultuser);
$resulttrans = mysqli_query($conn, "SELECT * FROM transactions");
$rowtrans=mysqli_fetch_array($resulttrans);
$ress=mysqli_query($conn, "SELECT * FROM transactions");
while($row=mysqli_fetch_array($ress)){
echo "<tr>";
echo "<td>".$row['id']."</td>";
echo "<td>"." same as receiver"."</td>";
$receiver=mysqli_query($conn, "SELECT name FROM users WHERE userid='" . $rowtrans['idreceiver'] . "'");
$receivername = mysqli_fetch_array($receiver);
echo "<td>". $receivername ."</td>";
echo "</tr>";
}
Don't use two queries. Join the two tables in one query:
$ress = mysqli_query("SELECT t.id, u1.name AS sendername, u2.name AS receivername
FROM transactions AS t
JOIN users AS u1 ON u1.userid = t.idsender
JOIN users AS u2 ON u2.userid = t.idreceiver");
while ($row = mysqli_fetch_assoc($ress)) {
echo "<tr>";
echo "<td>".$row['id']."</td>";
echo "<td>" . $row['sendername'] ."</td>";
echo "<td>" . $row['receivername'] . "</td>";
echo "</tr>";
}
You are echoing from fetched array in $receivername = mysqli_fetch_array($receiver);
you should do echo "<td>". $receivername['name'] ."</td>";
Note:
Fetched array from mysqli_fetch_array() can be echoed using indexes by number $receivername[0] or by string $receivername['name'], that is where you are missing here.
hi i already can make search. now how i want to put that data based on their role, based on this picture i want to make admin ,admin , user, user,user. need to use sort by ? i dont know.
and this my coding search . where should i put sort by ?
<?php
include 'config1.php';
if(isset($_POST['search'])){
$searchTerm = $_POST['search'];
$query = "SELECT * FROM members WHERE userid LIKE '%$searchTerm%'";
$result = mysql_query($query);
$count = mysql_num_rows($result);
echo "<table height = '30%'border='1'>";
if($count == 0)
{
echo "NO ID REGISTERED!";
}
else
{
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td width='5%'><b>USER ID:</b> {$row['userid']} </td>";
echo "<td width='5%'><b>USER NAME :</b> {$row['username']} </td>";
echo "<td width='5%'><b>USER EMAIL:</b> {$row['useremail']} </td>";
echo "<td width='5%'><b>USER ROLE:</b> {$row['userrole']} </td>";
echo "<td width='5%'><b>USER DIVISION:</b> {$row['userdiv']} </td>";
echo "<td width='5%'><b>USER DEPARTMENT:</b> {$row['userdepartment']} </td>";
echo "</tr>";
}
}
echo"</table>";
}
?>
$query = "SELECT * FROM members WHERE userid LIKE '%$searchTerm%' ORDER BY userrole";
Also can use ASC or DESC for displaying in ascending or descending order after ORDER BY.
eg.
Select * from users WHERE choice = 'PHP' ORDER BY id ASC;
$query = "SELECT * FROM members WHERE userid LIKE '%$searchTerm%' order by fieldWhichYouWant";
OR
$query = "SELECT * FROM members WHERE userid LIKE '%$searchTerm%' ORDER BY userrole ASC";
or add ASC or DESC as Ascending or descending oder to sort.
Replace fieldWhichYouWant with your requirement.
I have multiple tables I am joining to use in results for a second query and nesting the second results inside the first results.
I am using the following code:
$result = mysqli_query($con,"SELECT info.lotto_id, info.name, info.number_balls, info.number_bonus_balls, info.db_name, country.name_eng AS country, currency.name AS currency, currency.symbol AS symbol, next.draw_date AS next_draw, next.jackpot AS next_jackpot
FROM info
LEFT JOIN country ON info.country_id = country.id_country
LEFT JOIN currency ON info.currency_id = currency.currency_id
LEFT JOIN next ON info.lotto_id = next.lotto_id
WHERE (info.active='1')
ORDER BY next_jackpot DESC");
while($lotto = mysqli_fetch_array($result))
{
echo "<table border='0' width='600px' align='center'>";
echo "<tr>";
echo "<td>";
echo "<h1>Results for:</h1>";
echo "</td>";
echo "<td align='right'>";
echo "<p><img src='images/". $lotto['lotto_id'] ."_big.png' alt='". $lotto['name'] ." Results'/></p>";
echo "</td>";
echo "</tr>";
echo "</table>";
$result2 = mysqli_query($con,"SELECT * FROM" .$lotto['db_name'].
"ORDER BY date DESC
Limit 3");
while($draw = mysqli_fetch_array($result2))
{
echo "<table class='results' align='center'>";
echo "<tr>";
$draw['display_date'] = strtotime($draw['date']);
$lotto['cols'] = $lotto['number_balls'] + $lotto['number_bonus_balls'];
echo "<td class='date' colspan='".$lotto['cols']."'>".date('D M d, Y', $draw['display_date']). "</td>";
if ($draw[jp_code] < "1")
{
echo "<td class='winner' align='center'>Jackpot Amount</td>";
}
else
{
echo "<td class='rollover' align='center'>Rollover Amount</td>";
}
It is giving me the following error: Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in /home/content/95/11798395/html/results/info_mysqli.php on line 59
This relates to my results2 query. Can somebody please suggest what I am doing wrong.
Thank you.
Change:
$result2 = mysqli_query($con,"SELECT * FROM" .$lotto['db_name'].
"ORDER BY date DESC
Limit 3");
to:
$result2 = mysqli_query($con, "SELECT * FROM {$lotto['db_name']} ORDER BY date DESC LIMIT 3");
if ($result === false) {
exit("Error: " . mysqli_error($con));
}
hi guys im trying to improve my query for better performance is it possible to write this query in better way thanks a lot your helps
$query = " SELECT A FROM out_org where zone_id='1'";
$query2 = " SELECT A FROM out_dis where zone_id='1'";
$result = mysql_query($query);
$result2 = mysql_query($query2);
echo "<table border=1 style='background-color:#F0F8FF;' >";
echo "<caption><EM>my table</EM></caption>";
echo "<tr>";
echo "<th>" .OA. "</th>" ;
echo "<th>" .DA. "</th>";
echo "<th>" .total. "</th>";
echo "</tr>";
while($row = mysql_fetch_array($result) )
{
while( $row2 = mysql_fetch_array($result2)){
echo "<tr>";
echo "<td>" .$row['A']."</td>";
echo "<td>" .$row2['A']."</td>";
echo "<td>" .$total = $row['A'] - $row2['A']."</td>";
echo "</tr>";
}
echo "</table>";
}
Change your SQL to one query using a join and do the subtraction within the query:
$query = "SELECT (o1.A - o2.A) as value
FROM out_org o1
LEFT JOIN out_dis o2
ON o1.zone_id = o2.zone_id
WHERE o1.zone_id='1'";
I have a database with a table named "photos". In that table i have a column "size"(which is the size of the photos i had upload). I want to get the sum of these sizes and print the sum in my php page. Can anyone give me an example code(sql code in php code)..??
i have try this:
$query = "SELECT SUM(ph_size) from photos";
$result= mysql_query($query,$con);
echo "<table border='1'>
<tr>
<th>SUM(ph_size)</th>
</tr>";
while($row = mysql_fetch_array($result))
{
//echo $row;
echo "<tr>";
echo "<td>" . $row['SUM(ph_size)'] . "</td>";
echo "</tr>";
}
echo "</table>";
but it doesnt work.
You can use the sum function
$result=mysql_query("SELECT SUM(p.size) AS sum_of_photos_sizes FROM `photos` p WHERE 1");
if (mysql_num_rows($result)){
$data=mysql_fetch_array($result);
echo 'Total size of photos: '.$data['sum_of_photos_sizes'];
}