I am trying to pull some stats from a gameserver database, and return them in a table.
I have managed to do the first bit - pulling 10 results, displaying in a table in html, HOWEVER... the next bit has me stumped... I for each player I want to get some info from another table...
Here is what I have so far... excuse the messy code, I am just learning!
// adding ALL info from the first 10 tables 'profile' based on humanity, ascending, to the variable 'profile_info'
$profile_info = mysql_query("SELECT * FROM profile ORDER BY humanity desc LIMIT 10");
while($row = mysql_fetch_array($profile_info))
{
// Below I add the players unique ID into the variable $unique, to be used later for pulling their survival time from the 2nd table, which is called 'survivor'
$unique = $row['unique_id'];
echo "<tr>";
echo "<td class=\"c1\">" . $row['name'] . "</td>";
echo "<td class=\"c2\">" . $row['humanity'] . "</td>";
echo "<td class=\"c3\">" . $row['total_survivor_kills'] . "</td>";
echo "<td class=\"c4\">" . $row['total_bandit_kills'] . "</td>";
echo "<td class=\"c5\">" . $row['total_zombie_kills'] . "</td>";
echo "<td class=\"c6\">" . $unique . "</td>";
//In the line below, I try to get data from the 2nd table (called survivor), checking for the unique_id for the player (extracted from the first table, called 'profile') which is common across both tables and which have a 0 in the field 'is_dead'
$result = mysql_query("SELECT * FROM `survivor` WHERE `unique_id` ='.$unique' AND `is_dead` = 0") or die(mysql_error());
echo $unique;
if (mysql_num_rows($result)) {
$survivors_survival_time = mysql_fetch_assoc($result);
echo "<td class=\"c7\">" . $survivors_survival_time['survival_time'] . "</td>";
}
I hope that, even though the code above is probably rubbish, you can see what I am trying to do?
Most of it works fine, it is just that the part where I try to get the info for a player from the second table, based on their unique_id from their row in the first table, it doesn't work :(
Any ideas, or is the above so bad I should just give up?
I believe you have a typo in your query that pulls info for each individual player here:
mysql_query("SELECT * FROM `survivor` WHERE `unique_id` ='.$unique' AND `is_dead` = 0")
Specifically, the unique_id = '.$unique' part, where there is an extra . in the value field.
Try removing it to get the following:
$result = mysql_query("SELECT * FROM `survivor` WHERE `unique_id`='$unique' AND `is_dead` = 0") or die(mysql_error());
This, of course, is under the assumption that you don't prepend a . to each of your unique_id values in the survivor table.
Side-note (not answer specific):
If you were to update your code to use the MySQLi or PDO libraries opposed to the deprecated mysql_ functions, you would have the ability to use prepared statements. Using these would prevent minor errors like the one noted above as well as provide more-secure code too.
Nest your while loops or read about mysql LEFT JOIN and update your query.
I do not know about MySQL, because I've always used MSSQL, but here's how I would write it in PHP and mssql:
'SELECT * FROM survivor WHERE unique_id="'.$unique.'" AND is_dead = 0'
Try it and let me know ;)
You can combine those queries using a join:
SELECT
*
FROM
profile AS p
LEFT JOIN
survivor AS s ON p.unique_id = s.unique_id
WHERE
s.is_dead = 0
ORDER BY
humanity DESC
LIMIT
10
Then simply loop the results. Using LEFT JOIN gives you all the results from profile and any matches in survivor. If you change that to JOIN (i.e. drop the LEFT) it will give you only rows where a match exists in profile AND survivor.
A couple of suggestions:
Explicitly state which columns you want, i.e. "SELECT name, humanity, survival_time, etc..." instead of SELECT *.
Use a query method that allows you to use prepared statements, such as PDO.
Use single quotes instead of doubles so that you don't have to escape all the doubles in your HTML output. Anyone else who reads your code will thank you for that!
Related
I tried coming up with a better title, and I wish there was a chat that I could join to better discuss what i mean. Anyway, I have a while loop running that pulls data from two tables and displays the results correctly. I have a third table that I want to display the name of a result based on what id comes out in a loop... See what i mean, it doesn't make sense.
Here is the queries and loop:
try {
$stmt = $Conn->prepare("SELECT * FROM wn_trailer "
. "INNER JOIN wn_trailer_history ON wn_trailer.id = wn_trailer_history.trailer_id "
. "ORDER BY trailer_number ASC");
$stmt->execute();
$stmt2 = $Conn->prepare("SELECT status_name FROM wn_trailer_status WHERE status_id = :status");
$stmt2->execute([":status" => $row[1]]);
while ($row = $stmt->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_NEXT)) {
$data = "<tr>\r\n<td><a href='view-trailer.php?id=$row[0]'>" . $row[1] . "</a></td>\r\n"
. "<td>" . $row[1] . "</td>\r\n"
. "<td>" . $row[16] . "</td>\r\n"
. "<td><a href='https://www.google.com/maps/search/?api=1&query=$row[3],$row[4]' target='_blank'>View Map</a></td>\r\n"
. "</tr>\r\n";
print $data;
}
} catch (PDOException $e) {
print $e->getMessage();
}
$stmt2 is the second query that is hitting the third table. As the loop runs i want it to compare a result from the first query, and if it finds the id to return the value in the column. The query is good, i checked it in the mysql log, but it is not returning a value.
Another option i was thinking about doing is writing a function that runs through the wn_trailer_status and creates a select case, and using that to compare the results that why i'm not banging against the database a bunch of times.
tldr; My question is how can i compare the results from the first query against the second query and display the result of a column, or should i use a select case function to handle the work load?
assuming the value status is retun by the first query form table wn_trailer you could resolve both the query using adding an inner join
$stmt = $Conn->prepare("SELECT *
FROM wn_trailer
INNER JOIN wn_trailer_history ON wn_trailer.id = wn_trailer_history.trailer_id
INNER JOIN wn_trailer_status ON wn_trailer_status.status_id = wn_trailer.status
ORDER BY trailer_number ASC");
Basically, I'm coding a site that has many different categories and I want to display the amount of rows specific to that ID.
So for example, I have as the query:
$query= "SELECT job_sec FROM jobs WHERE job_sec = ?";
mysqli_num_rows($query);
I need to know how I can count the rows of an ID then echo the rows counted.
I'd like the results to display:
Web Design: 2,001 jobs
Logo Design: 5,120 Jobs
The job_sec column just uses a numerical value, would it be easier to have a text value then count the rows relating to the text value and echo them?
I have a feeling I need to use an array however I need the most efficient method.
Any help would be much appreciated!
Assuming job_sec is the category and I think you are looking for "group by":
$sql= "SELECT job_sec, count(*) AS c FROM jobs GROUP BY job_sec";
$r = mysqli_query($sql);
while ($row = mysqli_fetch_assoc($r)) {
echo $row['job_sec'] . ': ' . $row['c'] . ' Jobs ';
}
(didn't test and not sure if the mysqli syntax is correct)
I am trying to make all the data I have in my database organized by SaleID, this is how I have my DB right now. SalesID and ProductID are foreign keys.
TID.......SaleID .........ProductID
....1...............1.......................1
....2...............1.......................4
....3...............1.......................6
....4...............2.......................3
....5...............3.......................1
....6...............3.......................5
....7...............4.......................3
....8...............5.......................3
....9...............5.......................6
I want to make a table that shows all the data organized like this. Not stored into a database just to output this information.
SaleID........Products
.........1.......1,4,6
.........2.......3
.........3.......1,5
.........4.......3
.........5.......3,6
I was trying to do this with multidimensional arrays but every iteration it added a new row and showed exactly the same thing as the first table not being able to modify or add to a past row.
this is the code that I have right now
<?php
mysql_connect('localhost','root','');
mysql_select_db('mydb');
$query="SELECT * FROM prodsales ORDER by salesID ASC";
$result = mysql_query($query);
echo "<table border='1'>";
while($row = mysql_fetch_array($result)){
echo "<tr><td>" . $row['salesID'] . "</td><td>" . $row['productID'] . "</td></tr>";
}
echo "</table>";
mysql_close();
?>
SELECT SaleID , GROUP_CONCAT( DISTINCT ProductID SEPARATOR ',' ) AS PID FROM prodsales GROUP BY SaleID
Please try this hope it help you to get what you wnat
MySQL has a lovely group_concat function as Abhik posted in a comment. You can use it like this to get the distinct rows you want and to make the other matching rows a comma (or just about anything else) separated list:
select
saleID,
group_concat(productID)
from
prodsales
group by
saleID
order by
saleID
This will return the rows in the format you want and you can simply then output into the table as you are doing.
I have a db that have this kind of structure:
name|color
paul|blue
mary|red
paul|green
joe |yellow
paul|purple
mary|orange
paul|white
etc |etc
What am I trying to achieve here is to list the colors associated with a name, something like this:
paul=blue,green,purple,white
mary=red,orange
joe=yellow
I was checking some examples and found this:
$query="SELECT * FROM mytable order by name";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
echo $row['name']. " - ". $row['color'];
echo "<br />";
}
To be honest I just don't know how to go from this to what an I trying to achieve. How can I create a condition that will list all the color associated with one name and then jump to the next and so on?
You need to use GROUP_CONCAT. Try something like this:
SELECT
name,
GROUP_CONCAT(color) AS color
FROM mytable
GROUP BY name
See this fiddle - updated
If you need to account for duplicates, use GROUP_CONCAT(DISTINCT color). You can also include a custom SEPARATOR, but if you leave it out the default is ,. So in your case you don't need to specify. Also, as can be seen in the documentation linked above, you can, if desired, order the colors in whichever way you see fit - although, note that the default order is ASC, so you don't need to specify that, either, unless you want to change it.
You can do all with the SQL query:
SELECT name, GROUP_CONCAT(DISTINCT color ORDER BY color DESC SEPARATOR ',')
FROM mytable
GROUP BY name;
thank you for pointing me into the right direction. I manage to gather more info and make it work with this:
$query="SELECT name, GROUP_CONCAT(color) FROM mytable GROUP BY name";
$result = mysql_query($query) or die(mysql_error());
while($row=mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['GROUP_CONCAT(color)'] . "</td>";
echo "</tr>";
}
Thank you again :)
I am trying to program a lookup tool in PHP with SQL database.
$sql="SELECT
ID,
switch,
vlan,
circuit_id
FROM vlan
WHERE switch
LIKE LOWER('%" . $name . "%') OR vlan='" . $name ."'
ORDER BY vlan";
then further down I have this to put the results into variables:
while($row=mysql_fetch_array($result)){
$switch =$row['switch'];
$vlan=$row['vlan'];
$circuitID=$row['circuit_id'];
$ID=$row['ID'];
What I am wanting to do is take the switch reply and use that to do another query on a different table named router and return that information.
I have been searching for a few days and all of my searches ended up here but didnt seem to quite fit. I apreciate any help you can give.
UPDATE
With some great answers I finally got this to work. Here is how I did it. Thanks for the help.
while($row=mysql_fetch_assoc($result)){
$switch =$row['switch'];
$vlan=$row['vlan'];
$circuitID=$row['circuit_id'];
$ID=$row['ID'];
$sql2 = "SELECT IPAddress FROM router
WHERE switch LIKE '%".$switch."%'
LIMIT 1";
$result2 = mysql_query($sql2);
$row2=mysql_fetch_array($result2);
$IPAddress = $row2['IPAddress'];
Probably real ugly to the more experienced but it works. Thanks again.
It would be highly inefficient to run a secondary lookup query in a loop.
What you want is a simple JOIN between your two tables based on the switch field. What this will do is bring the linked rows in your router table alongside the rows in your vlan table where values in the switch column match up:
$sql = '
SELECT a.id, a.switch, a.vlan, a.circuit_id, b.ipaddress
FROM vlan a
JOIN router b ON a.switch = b.switch
WHERE a.switch LIKE "%' . strtolower($name) . '%" OR vlan = "' . $name . '"
ORDER BY a.vlan';
Then in your same fetch loop, you'll be able to access the linked values from your router table without having to run extra database calls:
while($row=mysql_fetch_array($result)){
$switch =$row['switch'];
$vlan=$row['vlan'];
$circuitID=$row['circuit_id'];
$ID=$row['ID'];
$ipaddress = $row['ipaddress'];
}
How many results do you expect? If there's more than one result, you might need to point which row exactly you want to take. Since it's fetch_array, your $row['switch'] or $switch value is still an array. var_dump it for keys and use the key, i.e.
SELECT `col` FROM `router` WHERE `switch` = '$switch[1]'