php/mysql basic search not returing results - php

I have a test query I am performing and it seems to be running OK, however not returning any data within the rows of the tables when using a varible passed or a direct set year for the table
<?
$curyr = date("Y");
isset($_GET['year']) ? $year = $_GET['year'] : $year = "";
$server = $_SERVER['PHP_SELF'];
$dbhost = "localhost";
$dbuname = "";
$dbpass = "";
$dbname = "";
$conn = new mysqli($dbhost,$dbuname,$dbpass,$dbname);
if($conn->connect_errno):
die("$conn->connect_error \n");
endif;
echo ("<title>$year</title>");
if (($year >= 2001) && ($year <=$curyr)) {
echo ("
<table border='0' align='center' width='100%'>
<tr>
<td>
<table border='1' align='center' width='60%' style='border:1 solid #000000; border-collapse: collapse'>
<tr>
<th align='center' colspan='5'>
DETAIL
</th>
</tr>");
$query = "SELECT c.* , p.* FROM $year c, dataset p WHERE c.id = p.id ORDER BY p.name ASC";
$r = $conn->query($query);
while ($row = $r->fetch_assoc()) {
echo ("
<tr>
<td>
$row[namepre]$row[name]
</td>
<td>
</td>
<td>
TEST
</td>
<td>
</td>
</tr>");
}
echo ("
</table>
</td>
</tr>
</table>");
} else {
echo ("ERROR NO ROWS");
}
$conn->close();
?>
I'm returning the "detail" portion of the top of the sample table but nothing else. This is just basic but wondering where I'm possibly missing something.

Related

TWO MySQL commands (SELECT to View), but only one is showing output

I have a table and each , I want to select a data from the same table in my database.
For example, first <td> is first name, then the second <td> is phone number.
I got the command, but only the first command is showing output.
This is my php codes to open and connect to the database :
<?php
include("./inc/db_connect.php");
$conn = OpenCon();
?>
This is the php codes for the table including <th> and <td> :
<div class="layer55">
<h3>
<table class="flat-table">
<tbody>
<tr>
<th>
<?php
$sql = "SELECT * FROM sharp_emp WHERE employee_id = 'AA170336'";
if ($result = $conn->query($sql)) {
if ($result->num_rows > 0) {
echo "Name";
}
}
?>
</th>
<th>
<?php
$sql = "SELECT * FROM sharp_emp WHERE employee_id = 'AA170336'";
if ($result = $conn->query($sql)) {
if ($result->num_rows > 0) {
echo "Phone Number";
}
}
?>
</th>
</tr>
<tr>
<td>
<?php
$sql = "SELECT first_name FROM sharp_emp WHERE employee_id = 'AA170336'";
while ($row = $result->fetch_array()) {
echo "" . $row['first_name'] . "";
}
?>
</td>
<td>
<?php
$sql = "SELECT phone FROM sharp_emp WHERE employee_id = 'AA170336'";
while ($row = $result->fetch_array()) {
echo "" . $row['phone'] . "";
}
?>
</td>
</tr>
</tbody>
</table>
</h3>
</div>
This is the php codes for db_connect.php :
<?php
function OpenCon()
{
$dbhost = "localhost";
$dbuser = // Hidden;
$dbpass = // Hidden;
$db = "sharp_db";
$conn = new mysqli($dbhost, $dbuser, $dbpass,$db) or die("Connect failed: %s\n". $conn -> error);
return $conn;
}
function CloseCon($conn)
{
$conn -> close();
}
?>
The expected output :
|----------|----------|
|Name |Phone Number|
|----------|----------|
|John |179898765 |
The current output :
|----------|----------|
|Name |Phone Number|
|----------|----------|
|John |Null (empty) |
You are running the same query multiple times, overwriting the $result variable for no reason, having useless $sql for the later 2 fetch without using them, and fetching a single $result twice by mistake.
So there are multiple concept problem with your code. I think your current code is something equivalant to this:
<div class="layer55">
<h3>
<table class="flat-table">
<tbody>
<tr>
<?php
$sql = "SELECT * FROM sharp_emp WHERE employee_id = 'AA170336'";
if ($result = $conn->query($sql)) {
if ($result->num_rows > 0) {
?>
<th>Name</th>
<th>Phone Number</th>
<?php } else { ?>
<th></th>
<th></th>
<?php } ?>
<?php } ?>
</tr>
<tr>
<?php if ($row = $result->fetch_array()) { ?>
<td><?php echo "" . $row['first_name'] . ""; ?></td>
<td><?php echo "" . $row['phone'] . ""; ?></td>
<?php } else { ?>
<td></td>
<td></td>
<?php } ?>
</tr>
</tbody>
</table>
</h3>
</div>
But frankly, it makes no sense to me to print an empty table when there is no result. So what you need is probably something like this.
<?php
$sql = "SELECT * FROM sharp_emp WHERE employee_id = 'AA170336'";
if (
($result = $conn->query($sql))
&& ($result->num_rows > 0)
&& ($row = $result->fetch_array())
):
?>
<div class="layer55">
<h3>
<table class="flat-table">
<tbody>
<tr>
<th>Name</th>
<th>Phone Number</th>
</tr>
<tr>
<td><?php echo $row['first_name']; ?></td>
<td><?php echo $row['phone']; ?></td>
</tr>
</tbody>
</table>
</h3>
</div>
<?php endif; ?>

Is there way to create foreach for sql code with array data

I got one query that takes data about users.
I need to make a 2nd query for each of these users.
I am getting data just for the first user. (with the second query)
First query:
$query = mysqli_query($conn, "SELECT * FROM tipuser WHERE permissions = '0'");
$ah = mysqli_fetch_array($query);
Also when i do for each($query as $x) and inside that i echo first and last name it shows all, when i do the same with $ah it doesn't show anything.
Then I got these variables:
$firstname = $ah['firstname'];
$lastname = $ah['lastname'];
Second query:
$sqlwork = mysqli_query($conn,
"SELECT DATE_FORMAT(datum, '%b %Y') AS Monthyear,
count(projekt) AS celkem,
SUM(projekt = 0) AS tipsport,
SUM(projekt = 1) AS slavia,
SUM(projekt = 2) AS bet
FROM zapasy
WHERE komentator1 = '$firstname $lastname'
OR komentator2 = '$firstname $lastname'
GROUP BY Monthyear");
I think the problem is somewhere in the array but I can't find it.
I am thankful for any help.
UPDATE:
this is how to display it on site:
<?php foreach($query AS $usersall) {?>
<table id="tablePreview" class="table table-hover table-sm table-bordered" style="border-top: 0px solid hsl(0, 0%, 87%);">
<thead style="color:black; background-color: hsla(0,0%,71%,1.00); border-top: 0px solid hsl(0, 0%, 87%);">
<tr>
<th scope="col">Měsíc</th>
<th scope="col">Zápasů</th>
<th scope="col">BET</th>
<th scope="col">TipSport</th>
<th scope="col">Slavia</th>
<th scope="col">Brutto</th>
<th scope="col">Daň</th>
<th scope="col">Netto</th>
<th scope="col">Zaplaceno</th>
</tr>
</thead>
<tbody>
<?php while ($tip = mysqli_fetch_assoc($sqlwork)) {?>
<tr>
<td>
<?=$tip['Monthyear'] ?>
</td>
<td>
<?=$tip['celkem']?>
</td>
<td>
<?=$tip['bet']?>
</td>
<td>
<?=$tip['tipsport']?>
</td>
<td>
<?=$tip['slavia']?>
</td>
<td>
<?php
$betx = $tip['bet']*$bet1cena['cena'];
$tipx = $tip['tipsport']*$tipcena['cena'];
$slax = $tip['slavia']*$slaviacena['cena'];
$celkem = $betx+$tipx+$slax;
echo "$celkem Kč";
?>
</td>
<td>
<?php
if($user['fakturuje'] == 0){
$dan = $celkem*0.15;
echo "$dan Kč";
}else{
echo "0 Kč";
}
?>
</td>
<td>
<?php
$netto = $celkem-$dan;
echo "$netto Kč";
?>
</td>
<td>
<input type="checkbox">
</td>
</tr>
<?php } ?>
</tbody>
</table>
<?php }?>
Although it is not completely clear from your question I think you are getting multiple rows from your first query and wanting to run the second query on each row returned by the first query
This is one way, I have taken the liberty of using a prepared query for the second one, this should remove any SQL Injection issues and also any problems with Last Names like O'Toole (the apostrophe issue). It also means you only need compile the query once, but can run it with new parameters as many times as you need.
$q1 = mysqli_query($conn, "SELECT * FROM tipuser WHERE permissions = '0'");
$q2 = "SELECT DATE_FORMAT(datum, '%b %Y') AS Monthyear,
count(projekt) AS celkem,
SUM(projekt = 0) AS tipsport,
SUM(projekt = 1) AS slavia,
SUM(projekt = 2) AS bet
FROM zapasy
WHERE komentator1 = ?
OR komentator2 = ?
GROUP BY Monthyear");
$stmt2 = $conn->prepare($q2);
while ($ah = mysqli_fetch_array($q1) ) {
// make the full name field
$name = $ah['firstname'] . ' ' . $ah['lastname'];
// bind those values to the prepared query
$stmt2->bind_param('ss', $name, $name );
$stmt2->execute();
// present the results of this query
// however you like
}

not able to get the data and display on web using php

$totDays = cal_days_in_month(CAL_GREGORIAN,$_REQUEST['month'],$_REQUEST['year']);
$attData = $commonObj->getAttendanceData($_REQUEST['month'],$_REQUEST['year']);
`
Student Name
<?php foreach($attData as $attk=>$attv){
$punchin = $commonObj->getTimeOfDate($attData[$attk]['punchin']);
$punchout = $commonObj->getTimeOfDate($attData[$attk]['punchout']);
$nam=$attv['name'];
?>
<tr>
<th class="danger">
<?php echo $nam;?>
</th>
<?php for($i=1;$i<=$totDays;$i++){?>
<?php if($commonObj->getDayOfDate($attData[$attk]['punch_time']) == $i){
echo "<td class='success' id='att_$i'>".$punchin.'-'.$punchout;?>
<table class="table table-responsive"style="display: none; position:relative;min-width:100px;max-width:200px; margin-top: -40px;" id="<?php echo "det_att_".$i;?>">
<tr>
<td>Time:</td>
<td><?php echo "p"?>
</td></tr>
<tr>
<td>UID:</td>
<td><?php echo $attData[$attk]['rfid_uid'];?></td>
</tr>
</table>
<?php
}else {echo "<td class='info'>#na";}?>
</td>
<?php }?>
</tr>
<?php }?>
</tr>
</table>`
this is the code for displaying on the web for viewing attendance ..
and here is the code for database part for getting attendance...
public function getAttendanceData($month,$year){
if(!is_numeric($month) || $month>12 || $month<1){
$this->setErrorMsg("Please Select valid month!");
header("location:viewAttendance.php");
exit;
}
$retData = array();
$fullDate = $year."-".$month;
//print_r($_SESSION);
if($_SESSION['user_type']==1){
$sql = "
SELECT t1.*
, t2.rfid_uid
, punch_time as punchin
, t2.name
FROM tbl_attendance as t1
right
join tbl_users as t2
on t1.rfid_uid = t2.rfid_uid
WHERE YEAR(punch_time) = $year
AND MONTH(punch_time) = $month
and t2.rfid_uid ='".$_SESSION['rfid_uid']."'
";
$retData = $this->getData($sql);
}else if($_SESSION['user_type'] == 2){
$sql = "SELECT t1.*,t2.rfid_uid,punch_time as punchin,t2.name FROM tbl_attendance as t1 right join tbl_users as t2 on t1.rfid_uid=t2.rfid_uid WHERE YEAR(punch_time) = $year AND MONTH(punch_time) = $month group by t2.rfid_uid";
$retData = $this->getData($sql);
}
return $retData;
}
web viewing imagedatabase table for attendanceuser
the attendance is recorded in the database but is not shown on the web..please help

PHP - Incorrect formatting

I am currently working through a PHP textbook to create a movie review website, however, the format of the page I have created is incorrect and I can't see the difference between my code and the code in the textbook. Here is a screenshot of the page I have created:
http://imgur.com/a/EBNxk
"Date" is supposed to be a table header alongside "Reviewer", "Comments" and "Rating". The "Reviews" header is also meant to be below the movie details and above "Date", "Reviewer", "Comment", and "Rating". The full code of the page is included below, I know the problem is probably going to be between the html tags but I'll include it all just incase.
EDIT: I see now that I left a quote mark out on line 130. Fixing this puts "Date" along with the other headers as it should be. But the Reviews header is still above the movie details.
<?php
function get_director($director_id)
{
global $db;
$query = "SELECT people_fullname FROM people WHERE people_id = ". $director_id;
$result = mysqli_query($db,$query) or die(mysqli_error($db));
$row = mysqli_fetch_assoc($result);
extract($row);
return $people_fullname;
}
function get_leadactor($leadactor_id)
{
global $db;
$query = "SELECT people_fullname FROM people WHERE people_id = ". $leadactor_id;
$result = mysqli_query($db,$query) or die(mysqli_error($db));
$row = mysqli_fetch_assoc($result);
extract($row);
return $people_fullname;
}
function get_movietype($type_id)
{
global $db;
$query = "SELECT movietype_label FROM movietype WHERE movietype_id = ". $type_id;
$result = mysqli_query($db,$query) or die(mysqli_error($db));
$row = mysqli_fetch_assoc($result);
extract($row);
return $movietype_label;
}
function generate_stars($rating)
{
$stars = " ";
for($i = 0; $i <= $rating; $i++)
{
$stars.= '<img src = "star.png" alt = "star " />';
}
return $stars;
}
function calculate_difference($takings,$cost)
{
$difference = $takings - $cost;
if($difference<0)
{
$color = "red";
$difference = "$ ".abs($difference)." million";
}
else if ($difference > 0)
{
$color = "green";
$difference = "$ ".abs($difference)." million";
}
else
{
$color = "blue";
$difference = "$ ".abs($difference)." million";
}
return "<span style = \"color:".$color.";\">".$difference."</span>";
}
$db = mysqli_connect('localhost','root') or die('Unable to connect');
mysqli_select_db($db,'moviesite') or die (mysqli_error($db));
$query = "SELECT movie_name, movie_year, movie_director, movie_leadactor,
movie_type, movie_running_time, movie_cost, movie_takings
FROM movie
WHERE movie_id = ".$_GET["movie_id"];
$result = mysqli_query($db,$query);
$row = mysqli_fetch_assoc($result);
$movie_name = $row['movie_name'];
$movie_director = get_director($row['movie_director']);
$movie_leadactor = get_leadactor($row['movie_leadactor']);
$movie_year = $row['movie_year'];
$movie_running_time = $row['movie_running_time']." mins";
$movie_takings = $row['movie_takings']." million";
$movie_cost = $row['movie_cost']." million";
$movie_health = calculate_difference($row['movie_takings'],$row['movie_cost']);
echo <<<ENDHTML
<html>
<head>
<title> Details for $movie_name </title>
</head>
<body>
<div style = "text-align:center">
<h2> $movie_name </h2>
<h3> Details </h3>
<table cellpadding = "2" cellspacing = "2" style = "width:70%; margin-left:auto; margin-right:auto">
<tr>
<td> <strong> Title </strong></td>
<td> $movie_name </td>
<td> <strong> Release Year </strong></td>
<td> $movie_year </td>
</tr>
<tr>
<td> <strong> Movie Director </strong></td>
<td> $movie_director </td>
<td> <strong> Cost </strong></td>
<td> $movie_cost </td>
</tr>
<tr>
<td> <strong> Lead Actor </strong></td>
<td> $movie_leadactor </td>
<td> <strong> Takings </strong></td>
<td> $movie_takings </td>
</tr>
<tr>
<td> <strong> Running Time </strong></td>
<td> $movie_running_time </td>
<td> <strong> Health </strong></td>
<td> $movie_health </td>
</tr>
ENDHTML;
$query = 'SELECT review_movie_id, review_date, reviewer_name, review_comment, review_rating
FROM reviews
WHERE review_movie_id = '.$_GET['movie_id'] . '
ORDER BY review_date DESC';
$result = mysqli_query($db, $query) or die(mysqli_error($db));
echo <<<ENDHTML
<h3><em>Reviews</em></h3>
<table cellpadding = "2" cellspacing = "2"
style = "width:90%; margin-left:auto; margin-right:auto;>
<tr>
<th style = "width: 7em"> Date </th>
<th style = "width: 10em"> Reviewer </th>
<th> Comments </th>
<th style = "width: 5em"> Rating </th>
</tr>
</table>
ENDHTML;
while($row = mysqli_fetch_assoc($result))
{
$date = $row['review_date'];
$name = $row['reviewer_name'];
$comment = $row['review_comment'];
$rating = generate_stars($row['review_rating']);
echo <<<ENDHTML
<td style = "vertical-align: top; text-align:center"> $date </td>
<td style = "vertical-align: top; text-align:center"> $name </td>
<td style = "vertical-align: top; text-align:center"> $comment </td>
<td style = "vertical-align: top; text-align:center"> $rating </td>
<br/>
ENDHTML;
}
echo <<< ENDHTML
</div>
</body>
</html>
ENDHTML;
?>
Try closing the quote marks on style:
<table cellpadding = "2" cellspacing = "2" style = "width:90%; margin-left:auto; margin-right:auto;">
<tr>
<th style = "width: 7em"> Date </th>

Select DISTINCT but echo all?

I need to populate a select list from mysql.
There are rows in mysql table with same value.
ex:
1-mamma
2-pappa
1-kus
My select dropdown will display the numbers 1, 1, 4.
First i don't wana se dublicates, here i can use DISTINCT in my select.
BUT, when i select number 1 in the dropdown i like to display both mamma & pappa.
How can i do this?
I have some code:
//Sql select
$sql = "SELECT * FROM phpexcel";
//
$aResult = mysql_query($sql);
if($_REQUEST['frm_action'] == 3){
if ($_REQUEST['id'] == 0){
$id = $_REQUEST['id'];
$sqlCustomer = "SELECT * FROM phpexcel";
}
else{
$id = $_REQUEST['id'];
$sqlCustomer = "SELECT * FROM phpexcel WHERE id ='$id'";
}
$aCustomer = mysql_query($sqlCustomer);
}
?>
<html>
<head>
<script type="text/javascript">
function changeSID()
{
oForm = eval(document.getElementById("frmForm"));
iCustomerId = document.getElementById("objekt_nr").value;
url = "get_element_by_objekt.php?frm_action=3&id=" +iCustomerId;
document.location = url;
}
</script>
</head>
<body>
<form name="frmForm" id="frmForm" >
<table border="0" cellspacing="2" cellpadding="2" width="40%">
<tr>
<td align="right" ><strong>Objekt</strong></td>
<td align="left"><select name="objekt_nr" id="objekt_nr" onchange="javascript:changeSID();">
<option value="">Select</option>
<option value="0">All</option>
<?php
$sid1 = $_REQUEST['id'];
while($rows=mysql_fetch_array($aResult,MYSQL_ASSOC))
{
$id = $rows['id'];
$sid = $rows['objekt_nr'];
if($sid1 == $id)
{
$chkselect = 'selected';
}
else
{
$chkselect ='';
}
?>
<option value="<?php echo $id;?>"<?php echo $chkselect;?>><?php echo $sid;?></option>
<?php } ?>
</td>
</tr>
<?php if($_REQUEST['frm_action'] == 3) { ?>
<tr>
<td colspan="2">
<table style="border:1px solid #003366;" cellspacing="2" cellpadding="2" width="100%" bgcolor="#003366">
<tr bgcolor="#EFEFEF">
<td><b><font color='Red'>Objekt Nr</font></b></td>
<td><b><font color='Red'>Objekt Rev</font></b></td>
<td><b><font color='Red'>Element Nr</font></b></td>
</tr>
<?php
while($row1 = #mysql_fetch_array($aCustomer,MYSQL_ASSOC))
{
$sid = $row1['objekt_nr'];
$sname = $row1['objekt_rev'];
$age = $row1['element_nr'];
?>
<tr bgcolor="#FFFFFF">
<td><b><font color='#663300'><?php echo $sid;?></font></b></td>
<td><b><font color='#663300'><?php echo $sname;?></font></b></td>
<td><b><font color='#663300'><?php echo $age;?></font></b></td>
SELECT id, GROUP_CONCAT(objekt_nr SEPARATOR ' ') AS objekt_nr FROM phpexcel GROUP BY id;
Should do the trick.
Will return you :
1 Pappa Mamma
2 Kus
This select
SELECT DISTINCT
objekt_nr, GROUP_CONCAT(DISTINCT element_nr ORDER BY element_nr) AS element_nr_list
FROM phpexcel
group by objekt_nr
ORDER BY objekt_nr
Gave me this:
N111
10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29
N123456
1,2,3,4,5,6,7,8,9

Categories