I have more than one devices sending data to the database at the same time. even though I have a primary id for database, as there are more than one devices, selection of a particular value with primary id seems really hard. I have the unique names of each device (A, B, C etc.)
i am diplaying data in a php table. the table will have headings like |Serial number| voltage| current| Select|. here serial number has no any relationship with database. I use it to organize data in the php table. When user select a value say serial number 3, previous 5 values should be considered for avg calculation, here values with serial 3,4,5,6 and 7. my doubt is how to choose these particular serial values?. I was thinking of saving these table in a php array and use these serials as key values. Any suggestions?
<?php
$conn= mysqli_connect( $dbhost, $dbuser, $dbpass, $db );
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT Date, Voltage, Current FROM DataTable WHERE ID='A' ORDER BY Date DESC";
$result = $conn->query($sql);
//$i is used to assign serial number for each row
$i=1;
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<form action = 'samepage.php' method =get>";
echo "<tr>
<td>" . $i. "</td>
<td>" . $row["Time"]. "</td>
<td>" . $row["Voltage"] . "</td>
<td>" . $row["Current"]. "</td>
<td>"." <input type=hidden name=serialSelected value =" . $i."</td>
<td>"." <input type=submit name=Submit value=select >"." </td>
</tr>";
$i++;
echo "</form>";
}
echo "</table>";
} else { echo "0 results"; }
$avgCurrent=0;
$avgVoltage=0;
if(isset ($_GET['serialSelected'])){
//how to choose the value selected and the previous 4 values (total 5 values for avg calculation)
}
Related
Im setting up a website for my home work and I need to display 1 row at random from an sql table in a html table using php. For example, my website is called random data where the website will generate random data for you (except all it is actually doing is displaying data already inserted into a database). So, does anyone know the code required to display 1 row of data from an sql table and display it in a html table using php in each cell of the table to retrieve the data from the database? Here is what I have so far (it works when I don't try to select just one row at random, but when I select all of them, it does work. Pleas Help! :)
<?php
$username = "root";
$password = "root";
$dbname = "fakedeets";
$conn = new mysqli("localhost", $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT FLOOR(RAND()*(10-5+1)+5);";
$result = $conn->query($sql);
if($row = $result-> fetch_assoc()) {
echo "<tr><td>". $row["firstname"].
"</td><td>". $row["lastname"].
"</td><td>". $row["dob"].
"</td><td>". $row["gender"].
"</td><td>". $row["emailaddress"].
"</td><td>". $row["phonenumber"].
"</td><td>". $row["photo"].
"</td></tr>";
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
?>
What I expect to appear is just 1 row of data displayed on a html doc selected at random from an sql database. Instead it just outputs nothin
You can just use SELECT {your columns} FROM {table} ORDER BY RAND() LIMIT 1
This selects the columns from the table you need, orders it randomly and then limits the set to just one entry. Replace the 1 with number of rows you need.
EDIT: I've just thought I could run this as one query with something like this:
SELECT * FROM clubs
JOIN match_request_archive
ON (SELECT COUNT (clubs.id) AS requests FROM match_request_archive)
I appreciate though my syntax is horrendous I've just whipped this up and will tweak it now.
I have a basic script for displaying data in a table. I've got a problem I can't solve which is I need to run one simple query to show all the data to the user. But in the last column I need to run a separate query to get the result.
Basically the first column in my table has the unique ID of a Rugby Club on the database. In a separate table on the database is a list of all requests made by clubs to players. I want to display how many requests each club has made in the last column of my script below which means running this query:
SELECT * FROM match_request_archive WHERE club_id LIKE "$row['id']"
The $row['id'] is from the PHP script below:
<?php
include 'credentials.php';
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM clubs";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc())
{
echo "
<tr>
<td>" . $row['id'] . "</td>
<td>" . $row['club_name'] . "</td>
<td>" . $row['club_captain'] . "</td>
<td>" . $row['club_captain_number'] . "</td>
<td>" . $row['club_captain_email'] . "</td>
<td>TBC</td>
</tr>
";
}
} else {
echo "<tr><td>0 results</td></tr>";
}
$conn->close();
?>
To solve this I tried embedding a second script in the echo command but it's my understanding you can't do this and in any case it did cause a fatal error. I can run this query separately and have it displayed in a different table, my problem is I need this to be all in one table. I hope that all makes sense.
Executing query in loop is bad practice because it has very slow performance.
So you should try get all data by one query if it possible:
SELECT *, COUNT(*) FROM clubs
LEFT JOIN match_request_archive
ON match_request_archive.club_id = clubs.id
GROUP BY clubs.id
This is my code with my output
<?php
$data = mysql_query("select * from request, users where request.user_id = users.id and request.user_id = $user_id") or die(mysql_error());
Print "<table border cellpadding=3>";
while($info = mysql_fetch_array( $data ))
{
Print "<tr class='my_loans_tr'>";
print "<td class='detail'>" .$info['loan_id'] . "</td> ";
Print " <td class='admin_amount'>".$info['amount'] . "</td> ";
Print " <td class='admin_points'>".$info['points'] . "</td> ";
Print " <td class='admin_date'>".$info['req_date'] . " </td>";
Print " <td class='admin_status'>".$info['status'] . " </td>";
Print " <td class='admin_cancelled'>".$info['cancelled_loan'] . " </td></tr>";
Print "</tr>";
}
Print "</table>";
?>
output
The loan id is within the database, there is a second table named collected, that has info for each loan id (3 rows minimum of info based on same loan id)
How can I get each id from the query, click on it, get the current id and declare it as a value so I can execute another query which is,
select * from collected where loan id = "$loan_id clicked";
I have searched for javascript and jquery but I am not able to get it working with the post method, what is the best way to do this?
I want to display a table that looks like this from a Mysql query using PHP:
Vendor Name
Item Not to Drawing Item Defective Incorrect Item Received Other
9 2 3 5
Vendor Name
Item Not to Drawing Item Defective Incorrect Item Received Other
2 4 5 7
etc..
Here is my code....
<?php
$con = mysql_connect("localhost","xxx","xxx");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("qa", $con);
$result = mysql_query("SELECT Vendor, Sum(draw), Sum(defective), Sun(received), Sum(other) FROM qa_reports Group by Vendor Order by Vendor ASC");
echo "<table>
<tr>
<th>Item not to Drawing</th>
<th>Item Defective</th>
<th>Incorrect Item Received</th>
<th>Other</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['Sum(draw)'] . "</td>";
echo "<td>" . $row['Sum(defective)'] . "</td>";
echo "<td>" . $row['Sum(received'] . "</td>";
echo "<td'>" . $row['Sum(other)'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
Not sure how to get the above results formatted as such.
I've done this with CFML but am new to PHP and can not grasp how to list results in a table grouped by a field.
You need to alias your columns in order to reference them in your $row array.
SELECT vendor,
Sum(draw) AS draw,
Sum(defective) AS defective,
Sun(received) AS received,
Sum(other) AS other
FROM qa_reports ...
Then you can reference them like so:
$row['draw'];
...
SELECT Vendor, Sum(draw) AS sumDraw, Sum(defective) AS sumDefective ...
$row['sumDraw'] etc.
I have some code im trying to get working. What im looking for is to have it so that i query a site, then display results and upload to database. I want it to ignore duplicates which i have working by checking the date. issue im having is when a user searches for another stock ticker it does not insert into DB because the date is the same. Anyone have any suggestions?
<?php
$con = mysql_connect("localhost","user","pass");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("bbpinpal_stocks", $con);
mysql_query("INSERT INTO stocks (stock_name, stock_value, stock_perc, date)
VALUES ('$name', '$last','$perc2', '$date')");
$result = mysql_query("SELECT * FROM stocks");
echo "<table border='1'>
<tr>
<th>Name</th>
<th>Value</th>
<th>Percent Change</th>
<th>Date</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['stock_name'] . "</td>";
echo "<td>" . $row['stock_value'] . "</td>";
echo "<td>" . $row['stock_perc'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
I have set date to unique but i know that is prob my issue. How can i have this so that it checks both name and date and if match to ignore
Replace your UNIQUE index on date with one on (stock_name, date):
ALTER TABLE stocks
DROP KEY date,
ADD UNIQUE KEY (stock_name, date)
I have set date to unique but i know that is prob my issue
This is most probably the issue. Why don't you use another column to uniquely identify a row?
http://en.wikipedia.org/wiki/Unique_key
"..Each unique key is composed from one or more data attributes of that data entity.."
INSERT IGNORE prevents you adding data with a primary key that is already in the database. You can have a primary key that consists of 2 fields.