Output single result of a MySQL Query with PHP not working - php

My table 'viewlevels' has the following data (among other):
id |title
10 |Cenas
I'm running the SQL query:
SELECT title FROM viewlevels WHERE id=10
Which is returning "Cenas" as expected.
But using the following PHP script, I just get "texto= " , why?
$res = $db->query("SELECT title FROM viewlevels WHERE id=10");
$res->data_seek(0);
while ($row = $res->fetch_assoc()) {
echo " texto= " . $row['title'] . "\n";
};

To see both fields you have to echo those columns:
while ($row = $res->fetch_assoc()) {
echo " id= " . $row['id'] . "\n";
echo " texto= " . $row['title'] . "\n";
};

You don't need to use data_seek in this instance.
$res = $db->query("SELECT title FROM viewlevels WHERE id=10");
while ($row = $res->fetch_assoc()) {
echo " texto= " . $row['title'] . "\n";
}
Will work.

Related

MYSQL/PHP AES_Decrypt function passing value

if ($db_found) {
$result = mysql_query("SELECT *,
CAST(AES_DECRYPT(jmeno, 'usa2010') AS CHAR(50)) name,
CAST(AES_DECRYPT(prijmeni, 'usa2010') AS CHAR(50)) lastname,
FROM pacienti WHERE id='13'");
while ($row = mysql_fetch_array($result)) {
$together = $row['name'] . " " . $row['lastname'];
}
echo $together;
}
Variable $together is null, but should contain data from table.
screenshot of table output after sql request above
Try to write echo in loop
while ($row = mysql_fetch_array($result))
{
echo $together = $row['name'] . " " . $row['lastname'];
}

Fitting table in HTML

I'm trying to design a webpage where it simply shows info from a database using PHP.
I almost solve all of my problem but it's the first time I'm using HTML and I am having trouble finding a solution for my problem.
The code I have so far is:
<?php
$connection = mysql_connect('localhost','XXXX','XXXX');
mysql_select_db('cardata_laptimer');
echo "<table>";
echo "<table border = 1>";
$query = "SELECT * from tempos GROUP BY piloto";
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
{
$query = "SELECT * from tempos WHERE piloto = '" . $row['piloto'] . "' ORDER BY tempo";
$resultado = mysql_query($query);
echo "<td>" . $row['piloto'] . "</td></tr>";
while($rows = mysql_fetch_array($resultado))
{
echo "<td>" . $rows['tempo'] . "</td><td>" . $rows['data'] . "</td></tr>";
}
}
The output I'm getting can be seen in www.cardata.pt
1st problem: How to I make the "piloto" (for example AA) occupy the space of 2 cells?
2nd problem: I want the table to show "pilotos" side by side and the info for each one (tempo and data) down the piloto name.
Thanks in advance
To occupy the space of 2 cells add : colspan="2"
here is my edit of your code :
<?php
$connection = mysql_connect('localhost','XXXX','XXXX');
mysql_select_db('cardata_laptimer');
echo '<table border="0"><tr>';
$query = "SELECT * from tempos GROUP BY piloto";
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
{
echo '<td><table border="1"><tr>';
$query = "SELECT * from tempos WHERE piloto = '" . $row['piloto'] . "' ORDER BY tempo";
$resultado = mysql_query($query);
echo '<td colspan="2">' . $row['piloto'] . "</td></tr><tr>";
while($rows = mysql_fetch_array($resultado))
{
echo "<td>" . $rows['tempo'] . "</td><td>" . $rows['data'] . "</td>";
}
echo '</tr></table></td>';
}
echo '</tr></table>';
?>

SQL order by date inside WHILE LOOP

I'm having trouble sorting my events by date, as I have event id's in one table and event date in another.
The code is as follows.
$getEventIds = "SELECT * FROM rz6wq_ohanah_registrations WHERE email='".$userEmail."'";
$result = mysqli_query($conn, $getEventIds);
echo '<br />';
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$getEventTitle = "SELECT * FROM rz6wq_ohanah_events WHERE ohanah_event_id=" . $row['ohanah_event_id'] . " ORDER BY date ASC";
$title = mysqli_query($conn, $getEventTitle);
$rowTitle = $title->fetch_assoc();
$originalDate = $rowTitle["date"];
$newDate = date("d-m-Y", strtotime($originalDate));
date_default_timezone_set('Europe/Copenhagen');
$currentDate = date('d/m/Y');
if(strtotime($currentDate) < strtotime($newDate)){
//echo "id: " . $row["ohanah_event_id"] . "<br>";
echo "<div class='eventsTilmeldt'>";
echo "<b>" . utf8_encode($rowTitle["title"]) . "</b><br>";
echo "<br>";
echo "Status: "; if($row["paid"] == 0){echo "<span style='color:red;'>ej betalt</span>";}else{echo "<span style='color:green;'>betalt</span>";}
echo "<br>";
echo "Dato: " . $newDate . "<br>";
echo "Tidspunkt: " . $rowTitle["start_time"] . "<br>";
echo "<br>";
echo "Adresse: " . utf8_encode($rowTitle["adress"]) . "<br>";
echo "By: " . utf8_encode($rowTitle["geolocated_city"]) . "<br>";
echo "Sted: " . utf8_encode($rowTitle["venue"]) . "<br>";
echo "</div>";
echo "<br>";
}else{
echo "";
}
}
} else {
echo "Ingen tilmeldte events.";
}
The issue that it doesen't sort my event's correctly by date. I figure it has something to do with it being inside the while loop where I order by date?
"SELECT * FROM rz6wq_ohanah_events WHERE ohanah_event_id=" . $row['ohanah_event_id'] . " ORDER BY date ASC";
I need to get the event's from another table first, called registrations - inside this table there is no date field.
How can I sort this list by date ?
You can use JOIN instead:
$getEventIds = "SELECT reg.*,event.* FROM rz6wq_ohanah_registrations as reg
JOIN rz6wq_ohanah_events as event ON reg.ohanah_event_id=event.ohanah_event_id
WHERE email='".$userEmail."'
ORDER BY event.date ASC";
Why use two query You can do it in single query
SELECT A.*,B.* FROM rz6wq_ohanah_registrations AS A JOIN
rz6wq_ohanah_events AS B ON A.ohanah_event_id=B.ohanah_event_id WHERE
A.email='".$userEmail."' ORDER BY B.date ASC
$getEventIds = "SELECT * FROM rz6wq_ohanah_registrations AS ROR, z6wq_ohanah_events AS ZOE WHERE ROR.ohanah_event_id = ZOE.ohanah_event_id AND ZOE.email = '" . $userEmail . "' ORDER BY date ROR.ASC";

PHP / MySQL - Nested List by recommendations / IDs

We have on a social project a member database, which includes, which member recommended an other member. The fields of the database looks like this:
id | name | email | code | recruit_by
Now we want to print a nested list of the structure, who recommended whom on all deep levels.
We didn't get it running with the following code:
<?PHP
mysql_connect("www.mysqlserver.net", "database1", "password") or die(mysql_error());
mysql_select_db("project_db1") or die(mysql_error());
echo "<ul>";
$result = mysql_query("SELECT * FROM registration") or die(mysql_error());
while($row = mysql_fetch_array($result))
{
echo "<li class=\"level0\">" . $row['id'] . " - " . $row['name'] . " - " . $row['email'] . " - " . $row['recruit_by'] . "</li>";
// 1. Level
$result2 = mysql_query("SELECT * FROM registration WHERE recruit_by LIKE " . $row['id']) or die(mysql_error());
while($row2 = mysql_fetch_array($result2))
{
echo "<li class=\"level1\">1. " . $row2['id'] . " - " . $row2['name'] . " - " . $row2['email'] . " - " . $row2['recruit_by'] . "</li>";
// 2. Level
$result3 = mysql_query("SELECT * FROM registration WHERE recruit_by LIKE " . $row2['id']) or die(mysql_error());
while($row3 = mysql_fetch_array($result3))
{
echo "<li class=\"level2\">2. " . $row3['id'] . " - " . $row3['name'] . " - " . $row3['email'] . " - " . $row3['recruit_by'] . "</li>";
// 3. Level
$result4 = mysql_query("SELECT * FROM registration WHERE recruit_by LIKE " . $row3['id']) or die(mysql_error());
while($row4 = mysql_fetch_array($result4))
{
echo "<li class=\"level3\">3. " . $row4['id'] . " - " . $row4['name'] . " - " . $row4['email'] . " - " . $row4['recruit_by'] . "</li>";
// 4. Level
$result5 = mysql_query("SELECT * FROM registration WHERE recruit_by LIKE " . $row4['id']) or die(mysql_error());
while($row5 = mysql_fetch_array($result5))
{
echo "<li class=\"level4\">4. " . $row5['id'] . " - " . $row5['name'] . " - " . $row5['email'] . " - " . $row5['recruit_by'] . "</li>";
}
}
}
}
}
echo "</ul>";
?>
Very similar to an answer i gave here:
MySQL best practice: SELECT children recursive as performant as possible?
Again not the best use case for MySQL but yeah....
Dont do so many queries, do one
"SELECT * FROM registration"
Go through the result to build this as a tree like...
// use a database query to get this member info, here a simplified version
$member = array();
$member[] = array('id' =>'2', 'recruit_by' =>'1');
$member[] = array('id' =>'3', 'recruit_by' =>'2');
$member[] = array('id' =>'4', 'recruit_by' =>'3');
$member[] = array('id' =>'9', 'recruit_by' =>'1');
//use php to build a array containing all the recruit-relationships as a tree
function buildtree($member) {
$ref = array();
foreach($member as $mem){
$member_id = $mem['id'];
$parent = $mem['recruit_by'];
if(!is_array($ref[$member_id])) $ref[$member_id] = array('id' => $member_id);
if(!is_array($ref[$parent])) $ref[$parent] = array('id' => $parent);
$ref[$parent]['child'][] = &$ref[$member_id];
}
return $ref;
}
$tree = buildtree($member);
/// use a recursive function to output the tree
function echotree($tree, $parent = 0) {
foreach ($tree as $t) {
if($parent){
echo "$parent recruited " . $t['id'] . '<br>';
}
if(is_array($t['child']) && !$parent){
echotree($t['child'],$t['id']);
}
}
}
echotree($tree);
will give you:
2 recruited 3
1 recruited 2
1 recruited 9
3 recruited 4
should not be too hard to put this in whatever layout you wish.

Calculating total of rows

I fetching and displaying records from database like this
SELECT purchorders.orderno,
suppliers.suppname,
purchorders.orddate,
purchorders.deliverydate,
purchorders.initiator,
purchorders.requisitionno,
purchorders.allowprint,
purchorders.status,
suppliers.currcode,
currencies.decimalplaces AS currdecimalplaces,
SUM(purchorderdetails.unitprice*purchorderdetails.quantityord) AS ordervalue
FROM purchorders
INNER JOIN purchorderdetails
ON purchorders.orderno = purchorderdetails.orderno
INNER JOIN suppliers
ON purchorders.supplierno = suppliers.supplierid
INNER JOIN currencies
ON suppliers.currcode=currencies.currabrev
WHERE purchorders.orderno=purchorderdetails.orderno
GROUP BY purchorders.orderno,
suppliers.suppname,
purchorders.orddate,
purchorders.initiator,
purchorders.requisitionno,
purchorders.allowprint,
purchorders.status,
suppliers.currcode,
currencies.decimalplaces LIMIT 5
I am getting the result properly. But i want to calculate and display the result of ordervalues which i have displayed (total of all 5 as i limit it to 5)
I tried doing like this
$SalesOrdersResult2 = DB_query($SQL,$db);
while ($row = DB_fetch_array($SalesOrdersResult2))
{
$FormatedOrderValue2 = locale_number_format($row['ordervalue'],$row['currdecimalplaces']);
$Total = $array_sum($row['ordervalue']);
$FormatedOrderDate1 = ConvertSQLDate($row['orddate']);
$FormatedDelDate1 = ConvertSQLDate($row['deliverydate']);
echo " <tr><td> " . $row['suppname'] . " </td>";
echo " <td>$FormatedOrderDate1</td><td>$FormatedDelDate1</td><td> " . $row['initiator'] . " </td><td>$FormatedOrderValue2</td><td> " . $row['status'] . " </td></tr> ";
}
echo "<tr><td colspan='3'>Total---</td><td colspan='2'>$Total</td></tr></tbody>";
But it says "Fatal error: Function name must be a string in ..."
Somebody please help me in doing this
Thanks
I suspect you're trying to do something like this?:
$SalesOrdersResult2 = DB_query($SQL, $db);
$Total = 0;
while ($row = DB_fetch_array($SalesOrdersResult2))
{
$FormatedOrderValue2 = locale_number_format($row['ordervalue'],$row['currdecimalplaces']);
$Total += $row['ordervalue'];
$FormatedOrderDate1 = ConvertSQLDate($row['orddate']);
$FormatedDelDate1 = ConvertSQLDate($row['deliverydate']);
echo " <tr><td> " . $row['suppname'] . " </td>";
echo " <td>$FormatedOrderDate1</td><td>$FormatedDelDate1</td><td> " . $row['initiator'] . " </td><td>$FormatedOrderValue2</td><td> " . $row['status'] . " </td></tr> ";
}
echo "<tr><td colspan='3'>Total---</td><td colspan='2'>$Total</td></tr></tbody>";
Remove the $ before array_sum.

Categories