MYSQL/PHP AES_Decrypt function passing value - php

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'];
}

Related

How I can display all rows in php

I wrote this code to retrieve some rows form database
session_start();
$con = mysqli_connect('localhost', 'root', '');
if(!$con)
{
die("not ok");
}
mysqli_select_db($con,"uoh");
$q = " SELECT * FROM student WHERE id = " . $_SESSION['user_id'] ." and password = " . $_SESSION['user_pass'];
$result = mysqli_query($con , $q ) ;
if($row = mysqli_fetch_array($result))
{
echo "this academic transcripts for " . $row["name"];
echo " and the id is " . $row["id"];
}
$q1 = " SELECT student_record.course,student_record.grade,student_record.term,coe_courses.crd
FROM student_record INNER JOIN coe_courses ON student_record.course_number = coe_courses.course_number
where student_record.id = ".$_SESSION['user_id'] ;
$result = mysqli_query($con , $q1 ) ;
if($row = mysqli_fetch_array($result))
{
echo "<br />";
echo "<table border=\"1\" style=\"width:500\">";
echo "<tr>";
echo "<th>coe_courses</th>";
echo "<th>terms</th>";
echo "<th>Grades</th>";
echo "<th>CRD</th>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row["course"]. "</td>";
echo "<td>" . $row["term"]. "</td>";
echo "<td>" . $row["grade"]. "</td>";
echo "<td>" . $row["crd"]. "</td>";
echo "</tr>";
echo "</table>";
}
The problem is that only shows the first row while I have three rows in phpMyAdmin.
enter image description here
You need to call fetch_* repeatedly to retrieve all rows from your result set; each time you call it it retrieves the next row in the result set.
In your sample code above, you would replace
if ($row = mysqli_fetch_array($result))
{
with
while ($row = mysqli_fetch_array($result))
{
This will loop until fetch_array tries to read beyond the last record in $result, at which point fetch_array returns false and the loop exits.

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>';
?>

Output single result of a MySQL Query with PHP not working

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.

Select and Insert Cannot save session in database base

how can I get the selected database into second page, And insert into another table.
Here is my first page :
<?php
session_start();
$result = mysqli_query($con,"SELECT * FROM qwerty
WHERE ID LIKE '$id' ");
while($row = mysqli_fetch_array($result))
{
echo "Name:" . $row['Fname'] . " " . $row['Lname'];
}
$_SESSION['fname']=$row['Fname'];
$_SESSION['lname']=$row['Lname'];
?>
Here is my second page:
<?php
session_start();
$sql="INSERT INTO login (Fname, Lname)
VALUES
('{$_SESSION['fname']}','{$_SESSION['lname']}')";
$sql="INSERT INTO login set
Fname='".$_SESSION['fname']."',
Lname='".$_SESSION['lname']."'";
Please look and try the following:
session_start();
$firstName = $_SESSION['fname'];
$lastName = $_SESSION['lname'];
$sql="INSERT INTO `login` (Fname, Lname) VALUES ('$firstName','$lastName')";
Also ensure that when you populate the sessions
while($row = mysqli_fetch_array($result))
{
echo "Name:" . $row['Fname'] . " " . $row['Lname'];
}
$_SESSION['fname']=$row['Fname']; //CHECK
$_SESSION['lname']=$row['Lname'];
that there are data assigned. Maybe add it in the loop. If you are looking for the last record it will be assinged anyway:
while($row = mysqli_fetch_array($result))
{
echo "Name:" . $row['Fname'] . " " . $row['Lname'];
$_SESSION['fname']=$row['Fname']; //CHECK
$_SESSION['lname']=$row['Lname'];
}

Concat two fields into a dropdown menu

I am trying to simply display two fields from a table, e.g. firstname, lastname, combine them and display in a dropdown menu that can be selected, and is stored along with other data the user inputs. Below, works for one fields, but i am struggling to combine the lastname, i have tried concat but i think i did that wrong. Thanks in advance.
//Drop Down Select
$sql = "SELECT concat (firstname, lastname) as username FROM users_tbl";
$result = pg_query($sql);
echo "<select name='firstname'>";
while ($row = pg_fetch_array($result)) {
echo "<option value='" . $row['firstname'] . "'>" . $row['firstname'] ."</option>";
}
echo "</select>";
// close connection
Try this way:-
$sql = "SELECT concat (firstname, lastname) as username FROM users_tbl";
$result = pg_query($sql);
echo "<select name='firstname'>";
while ($row = pg_fetch_array($result)) {
echo '<option value="'.$row['username'].'" >'.$row['username'].'</option>';
}
echo "</select>";
// close connection
Either rely on SQL...
while ($row = pg_fetch_array($result)) {
echo "<option value='" . $row['username'] . "'</option>";
}
OR don't use CONCAT in SQL and do it with PHP:
while ($row = pg_fetch_array($result)) {
echo "<option value='" . $row['firstname'] . " " . $row['lastname'] ."'</option>";
}
but do not do both.

Categories