Why is only one row from this query displayed? - php

I am trying to print the results of this query, but it only prints out the first row. Why is that?
if (isset($_GET['consumiperstanza'])) {
$num_stanza=$_GET['num_stanza'];
$data1=$_GET['data1'];
$data2=$_GET['data2'];
$query="SELECT stanze.num_stanza,consumi.cod_consumo, servizi.nome,
consumi.quantita, servizi.prezzo, consumi.data_c
FROM consumi, servizi, stanze
WHERE stanze.num_stanza=consumi.num_stanza
AND servizi.cod_servizio=consumi.cod_servizio
AND stanze.num_stanza='$num_stanza'
AND consumi.data_c BETWEEN '$data1' AND '$data2'
GROUP BY stanze.num_stanza";
$risultato = $conn->query($query);
if(mysqli_num_rows($risultato) > 0){
echo "<table border=1 bgcolor= 'white' align='center'>";
echo "<tr>";
echo "<th>NUMERO STANZA</th>";
echo "<th>CODICE CONSUMO</th>";
echo "<th>NOME</th>";
echo "<th>QUANTITÀ</th>";
echo "<th>PREZZO</th>";
echo "<th>DATA</th>";
echo "</tr>";
while($riga = mysqli_fetch_array($risultato)){
echo "<tr>";
echo "<td>" . $riga[0] . "</td>";
echo "<td>" . $riga[1] . "</td>";
echo "<td>" . $riga[2] . "</td>";
echo "<td>" . $riga[3] . "</td>";
echo "<td>" . $riga[4] . "</td>";
echo "<td>" . $riga[5] . "</td>";
echo "</tr>";
}
echo "</table><br>";
}
}

This is part of your criteria
AND stanze.num_stanza='$num_stanza'
But you're also doing this.
GROUP BY stanze.num_stanza
So, you'll only get one group.
Additionally, all the other columns in your SELECT are not well-defined since they are not included in the GROUP BY and are not aggregate expressions. Newer versions of MySQL actually will not allow you to do this by default. It is possible in older versions, but may not give you the results you expect.
The MySQL 5.6 manual:
... this is useful primarily when all values in each nonaggregated column not named in the GROUP BY are the same for each group. The server is free to choose any value from each group, so unless they are the same, the values chosen are nondeterministic.

Related

Convert unsigned int ip_address from db to display as xxx.xxx.xxx.xxx string in table

I have ip_addresses that were imported as strings into the database and stored as unsigned integers. I want to display them in standard xxx.xxx.xxx.xxx format in a table. In my code, I tried the following:
<?php
$query = $con->query('SELECT inet_ntoa(IP_ADDRESS)as address, FILENAME, country, area, city FROM download WHERE FILENAME is not null ORDER BY country,area,city');
while ($row = $query->fetch())
{
echo "<tr>";
echo "<td>" . $row['$address'] ."</td>";
echo "<td>" . $row['FILENAME'] ."</td>";
echo "<td>" . $row['country'] . "</td>";
echo "<td>" . $row['area'] . "</td>";
echo "<td>" . $row['city'] . "</td>";
echo "</tr>";
}
?>
It no longer throws an error, but the ip's are all wrong, many starting with 0.0.0.
How do I do the conversion so I can display the ip_address correctly?
Thanks in advance,
Larry

Filtering MySQL Database by field result

I am pulling stock data off of a trading API through a loop based on an aray of stock symbols. This spits out xml data which I then parse and input into a MySQL database for analysis later. This is an example of the fields I'm inserting into the database (I can add more):
$sql = "INSERT into securities (symbol, ask, ask_time, asksz)
VALUES ('$symbol', '$ask', '$ask_time','$asksz')";
My current query pulls all of the data and lists it in rows, entry after entry into a table:
$sql = "SELECT id, symbol, ask, ask_time, asksz FROM securities";
if($result = mysqli_query($conn, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table>";
echo "<tr>";
echo "<th>id</th>";
echo "<th>symbol</th>";
echo "<th>ask</th>";
echo "<th>ask_time</th>";
echo "<th>asksz</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['symbol'] . "</td>";
echo "<td>" . $row['ask'] . "</td>";
echo "<td>" . $row['ask_time'] . "</td>";
echo "<td>" . $row['asksz'] . "</td>";
echo "</tr>";
}
echo "</table>";
Is there a way to compare two separate stocks side by side? For instance, structure my query in order to pull those 4 fields only for 'AAPL', then in the same row pull the same data for 'GOOG'?

How to put user input into a SQL Query

I have a database and I want the user to be able to have an input into what comes out. i.e
Select from Table where example = user input from box **(input by the user)**
Im guessing what I need is a variable to hold the value that then goes into the statement. I know how to get the value from the input box with script but can I use it like:
select * From handover WHERE hdate = variable. However I am guessing someone is going to talk to me about security if its even possible.
<html><body>
<input>User input</input> //That needs to go into statement
<?php
include 'config.php';
$result = mysqli_query($con,"SELECT * FROM handover WHERE hdate = **user input**;");
echo "<table border='1'>
<tr>
<th>hdate</th>
<th>Delay</th>
<th>Health and Safety</th>
<th>Non Vsa</th>
<th>VSA</th>
<th>Dar</th>
<th>Other</th>
<th>Hour</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['hdate'] . "</td>";
echo "<td>" . $row['hdelay'] . "</td>";
echo "<td>" . $row['hs'] . "</td>";
echo "<td>" . $row['nv'] . "</td>";
echo "<td>" . $row['vsa'] . "</td>";
echo "<td>" . $row['dar'] . "</td>";
echo "<td>" . $row['other'] . "</td>";
echo "<td>" . $row['hour'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
Any help is welcome and advice on the best language to use for this.
Kind Regards
Fintan
first of all, this question has nothing to do with javascript & ajax. so you can delete those tags.
you want to show/search data from mysql.
$result = mysqli_query($con,"SELECT * FROM handover WHERE hdate = '".$_POST['abc']."' ");
this is when you want to check if hdate column have exact data as user input ( $_POST['abc'] ).
and also don't forget to use mysqli_real_escape_string
you can learn common mysql pattern queries from here: http://dev.mysql.com/doc/refman/5.0/en/pattern-matching.html

How to put a break in the output when specific values change using mysqli php

I am building a website to list statistics for bowling tournaments over the last 24 years. Using the following code generates a long, single table showing all the data. I would like to put a break in the table when the $row['season'] value changes, i.e., from 1990-1991 to 1991-1992, and for each subsequent change of seasons and echo either an html horizontal line between seasons or put the value of the season from the database, i.e., 2013-2014 at the top of each table segment. After a week of searching the web haven't figured out an answer. Here's the code I have now. Needs to be mysqli.
$result = mysqli_query($conn,"SELECT * FROM members INNER JOIN scores ON members.id=scores.memberID WHERE format LIKE '%s%' ORDER BY year, STR_TO_DATE( month, '%b' ), format ASC;");
echo "<table border='0'>
<tr>
<th>Name</th>
<th>Hometown</th>
<th>Month</th>
<th>Year</th>
<th>Season</th>
<th>Center</th>
<th>Center City</th>
<th>Format</th>
</tr>";
foreach($result as $row) {
echo "<tr>";
echo "<td>" . $row['firstName'] . " ". $row['lastName'] . "</td>";
echo "<td>" . $row['hometown'] . "</td>";
echo "<td>" . $row['month'] . "</td>";
echo "<td>" . $row['year'] . "</td>";
echo "<td>" . $row['season'] . "</td>";
echo "<td>" . $row['center'] . "</td>";
echo "<td>" . $row['centerCity'] . "</td>";
echo "<td>" . $row['format'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($conn);
The answer is actually already given but it looks like your new with php so here is a smal example i hope its useful to you.
You need to insert some if statements to check if there is any change of year. same goes for any other checks you want to perform.
this is something you could do...
<?php
$lastYear = null;//you can also set the first year manually of course
foreach($result as $row) {
//set the first year
if($lastYear == null){$lastYear = $row['year'];}
//check if the year changed or not
if($lastYear == $row['year']){
//if the year didnt change... do something
}else{
//your year changed... do something different
$lastYear = $row['year']; //update your 'last'year
}
}
?>
I hope this will help you.

id not going through the url

my id is not going through the url. my code is as follows
<?php
include 'library/connect.php';
$result = mysql_query("SELECT * FROM meetings ");
echo "<table border='1'><tr><th>Title</th><th>Chairman</th><th>Secretary</th><th>Terms of Reference</th><th>Named membership</th><th>Occurences</th><th>Book Room</th></tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['title']. "</td>";
echo "<td>" . $row['chairman']. "</td>";
echo "<td>" . $row['secretary']. "</td>";
echo "<td>" . $row['termsOfReference']. "</td>";
echo "<td>" . $row['named_membership']. "</td>";
echo "<td>" . $row['occurences']. "</td>";
?>
<td><font color="#CC3300">Book: room/date/time</font></td>
<?php
}
echo "</tr>";
echo "</table>";
include 'library/closedb.php';
?>
have you got any idea of what the problem can be?
where is $meeting_id set? seems to me like it should be $row['meeting_id'].
Where $meeting_id is defined?
It seems like you did a select but forgot to retrieve the meeting id.
Try to change the link to:
<a href ="secretary_booksRoom.php?meeting_id=<?php echo $row['meeting_id']; ?>">
In case you have column named meeting_id in your table of course.
<tr> tag need to be closed in the while loop
You need to define the $meeting_id variable before adding it to the URL, otherwise your link will simply be "secretary_booksRoom.php?meeting_id=". I am going to assume that your meetings table has an id column as a primary key. In your while loop, try declaring "$meetind_id = $row['your meeting id column']". This should get the meeting id and pass it to the URL.
Hope this helps.

Categories