Reformatting the query - php

I have a table called sym_dis.
select * from sym_dis gives
+--------------+-----------------------------------+
| disease | symptom |
+--------------+-----------------------------------+
| typhoid | headache |
| typhoid | high fever |
| typhoid | pain in the abdomen |
| typhoid | sore throat |
| typhoid | feeling of fatigue |
| typhoid | weekness |
| typhoid | constipation |
| polio | headache |
| polio | nausea |
| polio | vomiting |
| polio | general discomfort |
| polio | slight fever for upto three days |
| polio | stiffness |
| polio | fever |
| polio | difficulty swallowing |
| polio | muscle pain and spasms |
| yellow fever | high fever |
| yellow fever | chills |
| yellow fever | headache |
| yellow fever | muscle ache |
| yellow fever | vomiting |
| yellow fever | backache |
| hepatitis B | jaundice |
| hepatitis B | fatigue |
| hepatitis B | abdominal pain |
| hepatitis B | loss of appetite |
| hepatitis B | nausea |
| hepatitis B | vomiting |
| hepatitis B | joint pain |
| hepatitis B | dark coloured wine |
| hepatitis B | yellowish tinged skin and eyes |
+--------------+-----------------------------------+
How can I reformat the above table using php and html so that I get the following output?
+--------------+-----------------------------------+
| disease | symptom |
+--------------+-----------------------------------+
| typhoid | headache |
| | high fever |
| | pain in the abdomen |
| | sore throat |
| | feeling of fatigue |
| | weekness |
| | constipation |
| polio | headache |
| | nausea |
| | vomiting |
| | general discomfort |
| | slight fever for upto three days |
| | stiffness |
| | fever |
| | difficulty swallowing |
| | muscle pain and spasms |
| yellow fever | high fever |
| | chills |
| | headache |
| | muscle ache |
| | vomiting |
| | backache |
| hepatitis B | jaundice |
| | fatigue |
| | abdominal pain |
| | loss of appetite |
| | nausea |
| | vomiting |
| | joint pain |
| | dark coloured wine |
| | yellowish tinged skin and eyes |
+--------------+-----------------------------------+

Something like:
$result = mysql_query("select * from sym_dis order by disease");
$lastDisease = '';
echo "<table>";
while ($row = mysql_fetch_assoc($result)) {
echo "<tr><td>";
if ($row['disease'] != $lastDisease)
{
echo $row['disease'];
$lastDisease = $row['disease'];
}
echo "</td><td>";
echo $row['symptom'];
echo "</td></tr>";
}
echo "</table>";
I wasn't sure if you wanted an html table, or actually the dashes and + style.

<?
foreach( $rows as $row ){
if( $row[0] != $last )
echo $row[0] . " ";
echo $row[1]
$last = $row;
}
?>
or similar

You need to get all "disease" and the, for each "disease" collect the matching "symptom", like this (working and tested):
<?php
define("HOST", "localhost");
// Database user
define("DBUSER", "username");
// Database password
define("PASS", "password");
// Database name
define("DB", "database_name");
############## Make the mysql connection ###########
$conn = mysql_connect(HOST, DBUSER, PASS) or die('Could not connect !<br />Please contact the site\'s administrator.');
$db = mysql_select_db(DB) or die('Could not connect to database !<br />Please contact the site\'s administrator.');
$query = mysql_query(" SELECT DISTINCT disease FROM sym_dis ");
echo '<table>';
while ($data = mysql_fetch_array($query)) {
echo '<tr><td valign="top">'.$data["disease"].'</td><td>';
$query2 = mysql_query(" SELECT * FROM sym_dis WHERE disease = '".$data['disease']."' ");
while ($data2 = mysql_fetch_array($query2)) {
echo $data2["symptom"] . '<br>';
}
echo '</td></tr>';
}
echo '</table>';
?>

Related

Sort by reference count php

How can I sort by reference count?
In the above picture,
2-1
1-1
I want to go out like this.
I'm sorry for my bad english
To sort a result set by a column you would append an ORDER clause to your query.
For example:
mysql> SELECT name, birth FROM pet ORDER BY birth;
+----------+------------+
| name | birth |
+----------+------------+
| Buffy | 1989-05-13 |
| Bowser | 1989-08-31 |
| Fang | 1990-08-27 |
| Fluffy | 1993-02-04 |
| Claws | 1994-03-17 |
| Slim | 1996-04-29 |
| Whistler | 1997-12-09 |
| Chirpy | 1998-09-11 |
| Puffball | 1999-03-30 |
+----------+------------+
More information can be found in the manual:
https://dev.mysql.com/doc/refman/5.7/en/sorting-rows.html

how to display the data in a column with sql and php

i have table in sql like this :
----------------------------------
| id | name | time1 | time2 |
----------------------------------
| 1 | softball | 05.00 | 10.00 |
| 2 | softball | 10.00 | 11.00 |
| 3 | softball | 11.00 | 14.00 |
-----------------------------------
here is my code :
$query = "select * from schejule";
$sql = mysql_query($query);
echo "<table class='table table-striped table-advance table-hover'>";
while ($u = mysql_fetch_array($sql)) {
echo "<tr><td>$u[time1] - $u[time2]</td></tr>";
}
echo "</table>";
but if i create like that, will display it like this :
-----------------
| 05.00 - 10.00 |
-----------------
| 10.00 - 11.00 |
-----------------
| 11.00 - 14.00 |
-----------------
i want to display it with php like this :
--------------------------------------------------------
| days | 05.00 - 10.00 | 10.00 - 11.00 | 11.00 - 14.00 |
--------------------------------------------------------
| mo | | | |
--------------------------------------------------------
| tu | | | |
--------------------------------------------------------
| we | | | |
--------------------------------------------------------
| th | | | |
--------------------------------------------------------
| fr | | | |
--------------------------------------------------------
| sa | | | |
--------------------------------------------------------
| su | | | |
--------------------------------------------------------
How can i display like that if i use php. I only know how to make it in rows.
Thx..
Try to prepare each column of a row before printing it to output stream.
Sample code for the first row (not tested):
$query = "select * from schejule";
$sql = mysql_query($query);
echo "<table class='table table-striped table-advance table-hover'>";
$row = '<tr><td>days</td>';
while ($u = mysql_fetch_array($sql)) {
$row .= "<td>$u[time1] - $u[time2]</td>";
}
$row .= '</tr>';
echo $row;
echo "</table>";

pulling from a specific data from a row and column in a database

My data base looks like this.
its ordered ascending by NO#
And col2 is the start of the database NO# is basically invisible and only used as a reference as to row number
so lets say I wanted to display on a web page the text in col8, row 5. What would the php code be?
PS. the connect code is seperate and not an issue hence i did not include itI
-|NO#|col2|col3|col4|col5|col6|col7|col8|col9|col10
---------------------------------------------------
|1 | | | | | | | | | |
---------------------------------------------------
|2 | | | | | | | | | |
---------------------------------------------------
|3 | | | | | | | | | |
---------------------------------------------------
|4 | | | | | | | | | |
---------------------------------------------------
|5 | | | | | | |2012| | |
---------------------------------------------------
|6 | | | | | | | | | |
---------------------------------------------------
|7 | | | | | | | | | |
---------------------------------------------------
|8 | | | | | | | | | |
---------------------------------------------------
|9 | | | | | | | | | |
---------------------------------------------------
|10 | | | | | | | | | |
---------------------------------------------------
Here is my code but it whites out the page when I try to load it.
<?php
//selects row
$query = "SELECT * FROM `Inventory` WHERE NO# = '5'";
//select column
$col8 = $row['col8'];
// fetch the results
WHILE($row = mysql_fetch_array($query):
$row = mysql_fetch_array($result);
// display the results
<div id="year">echo "$col8";</div>
?>
I would probably do something like what's below. I have not tested this code, though.
<?php
$result = mysql_query( "SELECT `col8` FROM `Inventory` WHERE `NO#` = '5' LIMIT 1" );
$row = mysql_fetch_assoc( $result );
?>
<div id="year"><?php echo $row['col8']; ?></div>
Hopefully that'll help you out a bit.

My table in postgresql is truncating these strings

I have a table in VMobjects like this
MGRCONFIG_DB=# select * from vmobjects;
guid | ipaddress | username | password | hostid | vmname | guestostype | guestos
name
-----------------------------------+---------------+----------+----------+----------+-----------------+-------------+--------
-----
7728235734dcd25700e7c02.96324791 | gsdag | gsdasd | | Physical | rag | windows |
3642656604dcd343d3bcd11.54875889 | fsd | | | Physical | derde | windows |
17714467484dcd35dd0fa677.27764184 | dsf | | | Physical | fdsfd | windows |
1837080764dcd362fafe404.83675706 | fgf | | | Physical | fgf | windows |
2791118544dcd363df11bf1.21924610 | fdghhg | | | Physical | $%^ | windows |
7716352574dcd365c9eb777.30236042 | dsffd | | | Physical | ^ | windows |
10753160514dcd366631f5b6.48505657 | gfdgd | | | Physical | # | windows |
8253046164dcd366f177bc3.85542378 | ghgfdhg | | | Physical | ############## | windows |
9126180214dcd367a5b42e0.23605256 | fsdfdsfdsf | | | Physical | fdsaj;( | windows |
11086632854dcd36f62f7e79.14470771 | dfsdfsd | | | Physical | ^ | windows |
Now I have a php page addvm.php, when I add username/ip/password/ or anything it gets truncated
gets truncated on entering data as '~!##$%^&*()_+=-`{}|\][:"';<>?/.,' for all fields.
After using pg_escape_string
i am able to insert '~!##$%^()_=-`{}|][:"';<>?/. all strings except + and &.
#Emil Vikström: say that i have to use urlencode for this. but i don't no, How & whr it is used?
Use pg_escape_string on the data before entering it into your SQL query:
$data = '~!##$%^&*()_+=-';
$data_escaped = pg_escape_string($data);
$query = 'INSERT INTO table (data) VALUES("'. $data_escaped .'");';

Joining MySQL Tables and Calculating Counts - Output in PHP

I am trying to query a MySQL database of available computers in a busy public computer lab. I have two tables, COMPUTERS and COMPUSE. The COMPUSE table is updated each time a user logs on or signs off a computer. When the logofftime is null, the computer is in use. In this example, Computers 1, 2, 3, and 8 are in use. Computers 4, 5, 6, 7, 9, 10 are available.
|-----------------------|-----------------------|----------|-----------|
| logontime | logofftime | recID | compname |
|-----------------------|-----------------------|----------|-----------|
| 2011-05-13 13:45:16 | <<null>> | 310052 | Comp001 |
| 2011-05-13 13:35:18 | 2011-05-13 13:39:37 | 310043 | Comp001 |
| 2011-05-13 12:12:09 | 2011-05-13 12:33:37 | 309979 | Comp001 |
| 2011-05-13 13:00:57 | <<null>> | 310018 | Comp002 |
| 2011-05-13 11:30:13 | 2011-05-13 12:58:15 | 309940 | Comp002 |
| 2011-05-13 09:36:15 | 2011-05-13 09:47:22 | 309850 | Comp002 |
| 2011-05-13 09:25:29 | <<null>> | 309840 | Comp003 |
| 2011-05-13 08:45:38 | 2011-05-13 09:24:03 | 309793 | Comp003 |
| 2011-05-12 22:39:58 | 2011-05-13 00:36:31 | 309640 | Comp003 |
| 2011-05-13 12:06:22 | 2011-05-13 12:50:23 | 309972 | Comp004 |
| 2011-05-13 11:10:16 | 2011-05-13 12:01:16 | 309915 | Comp004 |
| 2011-05-13 07:17:18 | 2011-05-13 09:42:10 | 309731 | Comp004 |
| 2011-05-13 11:51:38 | 2011-05-13 12:15:35 | 309959 | Comp005 |
| 2011-05-13 08:55:14 | 2011-05-13 09:47:48 | 309807 | Comp005 |
| 2011-05-12 18:15:05 | 2011-05-12 18:15:16 | 309502 | Comp005 |
| 2011-05-13 12:08:40 | 2011-05-13 13:16:41 | 309974 | Comp006 |
| 2011-05-13 11:29:09 | 2011-05-13 12:05:56 | 309939 | Comp006 |
| 2011-05-13 11:10:41 | 2011-05-13 11:19:14 | 309916 | Comp006 |
| 2011-05-13 10:45:27 | 2011-05-13 11:16:44 | 309896 | Comp007 |
| 2011-05-13 09:21:42 | 2011-05-13 09:55:48 | 309839 | Comp007 |
| 2011-05-13 08:23:33 | 2011-05-13 09:14:24 | 309770 | Comp007 |
| 2011-05-13 13:54:12 | <<null>> | 310058 | Comp008 |
| 2011-05-13 13:38:53 | 2011-05-13 13:39:23 | 310045 | Comp008 |
| 2011-05-13 10:13:23 | 2011-05-13 13:26:51 | 309878 | Comp008 |
| 2011-05-13 12:16:06 | 2011-05-13 13:26:21 | 309984 | Comp009 |
| 2011-05-13 10:13:09 | 2011-05-13 12:15:13 | 309877 | Comp009 |
| 2011-05-13 08:23:22 | 2011-05-13 10:07:08 | 309769 | Comp009 |
| 2011-05-13 13:45:51 | 2011-05-13 13:47:11 | 310053 | Comp010 |
| 2011-05-13 11:18:12 | 2011-05-13 13:19:59 | 309925 | Comp010 |
| 2011-05-13 07:28:50 | 2011-05-13 09:50:09 | 309737 | Comp010 |
|-----------------------|-----------------------|----------|-----------|
This data needs to be joined with a table that indicates which floor of the building the computer is on. That table looks similar to this:
|--------|-----------|-------------|
| compID | compname | LOCATION |
|--------|-----------|-------------|
| 95 | Comp001 | 1st Floor |
| 96 | Comp002 | 1st Floor |
| 97 | Comp003 | 1st Floor |
| 98 | Comp004 | 1st Floor |
| 99 | Comp005 | 2nd Floor |
| 100 | Comp006 | 2nd Floor |
| 101 | Comp007 | 2nd Floor |
| 102 | Comp008 | 3rd Floor |
| 103 | Comp009 | 3rd Floor |
| 104 | Comp010 | 3rd Floor |
|--------|-----------|-------------|
The first table, COMPUSE, has several thousand records in it as it is used for calculating usage statistics for the lab. I need to create an output of how many computers are available on each level. I don't know how to join location from the COMPUTERS table to compname from the COMPUSE table without disrupting my query. Initially, I ran the following query to determine the total number of computers available, but I really need to be able to break it down by area of the building.
SELECT
(SELECT COUNT(compname)
FROM compusage.computers) -
(SELECT COUNT(compname)
FROM compusage.compuse
WHERE logofftime IS NULL)
Can anyone help me construct a query that would output the number of available computers on each level of the building? The expected result in this example would be:
Level 1: 1 computer (of 4) available
Level 2: 3 computers (of 3) available
Level 3: 2 computers (of 3) available
Thanks,
Jordan
UPDATE: This is really similar to what I'm trying to do, but I also can't figure out how to adapt these queries: PHP::Group and subtract two tables
UPDATE 2: This is what I am trying to adapt now, but I don't understand subqueries well enough to get this to work:
select (totalcomps.total - inuse.inusecomps) as available, totalcomps.total
from (SELECT count(compname) as total, location
FROM compuse.computers
GROUP BY location) as totalcomps
inner join (SELECT count(compname) as inusecomps
FROM computers.compuse
WHERE logofftime is null
GROUP BY location) as inuse
on computers.compname = compuse.compname
UPDATE 3: I've updated the sample data to be more of a real-world example with more records.
Have you tried this?
SELECT LOCATION, COUNT(1) as numberOfComps
FROM COMPUTERS c
LEFT JOIN COMPUSE cu ON c.COMPNAME = cu.Computer
WHERE logofftime IS NULL
GROUP BY LOCATION
I did accomplish this task through some PHP calculations based on two separate MySQL queries. This works, but I would prefer to structure an SQL query that does the same thing. Here's the PHP:
<?php
$username = "user"; $password = "passwd"; $database = "compusage";
mysql_connect(localhost,$username,$password);
#mysql_select_db($database) or die( "Unable to select database");
$inUseQuery = "SELECT location, COUNT(1) as inUseComps
FROM compusage.computers
INNER JOIN compusage.compuse ON computers.compname = compuse.compname
WHERE logofftime IS NULL
GROUP BY LOCATION";
$totCompsQuery="SELECT location, COUNT(compname) as totComps
FROM compusage.computers
GROUP BY LOCATION";
$result = mysql_query($inUseQuery);
$inUseNum = mysql_numrows($result);
$totCompsResult = mysql_query($totCompsQuery);
$totCompsNum = mysql_numrows($totCompsResult);
mysql_close();
$i=0;
while ($i < $inUseNum) {
$numCompsInUse=mysql_result($result,$i,"inUseComps");
$inUseLocation=mysql_result($result,$i,"location");
$inUseArray[$inUseLocation] = $numCompsInUse;
$i++;
}
$i=0;
while ($i < $totCompsNum) {
$totComps=mysql_result($totCompsResult,$i,"totComps");
$totCompsLocation=mysql_result($totCompsResult,$i,"location");
$totCompsArray[$totCompsLocation] = $totComps;
$i++;
}
while($totCompsKey = key($totCompsArray)) {
if (array_key_exists($totCompsKey, $inUseArray)) {
$availFloor = $totCompsArray[$totCompsKey] - $inUseArray[$totCompsKey];
if ($availFloor == "1") {
printf("<li>%s: %s computer available.</span></li>", $totCompsKey, $availFloor);
}
else {
printf("<li>%s: %s computers available.</span></li>", $totCompsKey, $availFloor);
}
}
else {
printf("<li>%s: all computers in use.</span></li>", $totCompsKey);
}
next($totCompsArray);
}
?>
SELECT LOCATION, COUNT(compID) as Available
FROM COMPUTERS C1
INNER JOIN COMPUSE C2 ON C1.COMPNAME = C2.COMPNAME WHERE LOGOFF TIME IS NOT NULL
GROUP BY LOCATION

Categories