just wondered if there was a a faster way of running this query? I have over 250K rows in my 'results' table. The code searches the table for all of the different types of race a trainer has run. It the looks at how many races that trainer has ran in the type of race. Then it looks at hoe many races he/she has won. Hope this is enough information. Thanks for looking.
echo "<div style='text-align:center;'>";
echo "<table id='main_table'>";
echo "<tr ><td align = center colspan = 4>".$trainer."</td></tr>";
$result = mysqli_query($db,"SELECT
DISTINCT RaceType AS racetype
FROM results
WHERE trainer = '$trainer' ORDER BY RaceType ASC"); //placing_numerical,
while($row = mysqli_fetch_array( $result ))
{
echo "<tr>";
echo "<td>";
echo $row['racetype'];
echo "</td>";
echo "<td>";
$result_horse = mysqli_query($db,"
SELECT
COUNT(id) AS result_run
FROM results
WHERE trainer = '$trainer' AND RaceType = '".$row['racetype']."'
");
$row_horse = mysqli_fetch_array( $result_horse );
echo $row_horse['result_run'];
$a = $row_horse['result_run'];
echo "</td>";
echo "<td>";
$result_horse1 = mysqli_query($db,"
SELECT
COUNT(id) AS result_win
FROM results
WHERE trainer = '$trainer' AND RaceType = '".$row['racetype']."' AND placing_numerical = '1'
");
$row_horse1 = mysqli_fetch_array( $result_horse1 );
echo $row_horse1['result_win'];
$b = $row_horse1['result_win'];
echo "</td>";
echo "<td>";
$percent = ($b / $a ) * 100;
$percent = sprintf('%0.0f', $percent);
echo $percent."%";
echo "</td>";
echo "</tr>";
}
echo "</table>";
Here are two suggestions you can check :-
For row_horse and row_horse1 , use "mysqli_fetch_assoc" instead of "mysqli_fetch_array" .
Try to write a single query taking all the three queries and it will save you more time what you are expecting .
Related
Currently, in our office website, there is a userinput textbox and after inserting, results from database will be shown below. There are 4 results Lot ID, Product, EWSFLOW and Zone.Among them, only zone is different. I want to do that Lot ID, Product and EWSFlow must show at once and if that entered values have 5 different zones, Zone must shown Zone: 1,2,3,4,5. << First problem has been solved. And right now, I tried to add check boxes for each zone and checkbox must shown beside each zone. But currently, checkboxes are showing at the top. Also, count of the checkboxes must be same as Zones. lets say if the inserted value have 5 zones, it has to show 5 checkboxes besides of it (Example: Zone : [checkbox] 1).
Checkboxes are showing at top
echo "<table id='corwafer'>";
$arr = array();
while ($row = mysqli_fetch_assoc($result1)) {
$field1name = $row["lotid"];
$field2name = $row["product"];
$field3name = $row["ewsflow"];
$field4name = $row["zone"];
$key = $field1name + ":" + $field2name + ":" + $field3name;
if (!in_array($key, $arr)){
echo "<tr>";
echo "<th >Lot ID:</th>";
echo "<td >$field1name</td>";
echo "</tr>";
echo "<tr>";
echo "<th>Product:</th>";
echo "<td>$field2name</td>";
echo "</tr>";
echo "<tr>";
echo "<th>EWSFLOW: </th>";
echo "<td>$field3name</td>";
echo "</tr>";
array_push($arr, $key);
}
echo "<tr>";
echo "<th>Zone:</th>";
echo "<input type='checkbox' name='chkzone' value='chkzone'>";
echo "<td>$field4name</td>";
echo "</tr>";
}
echo "</table>";
You can define an array and put lotid, product and ewsflow into it as merged inside the loop. Then before echoing check if it's already used before :
$arr = array();
while ($row = mysqli_fetch_assoc($result1)) {
$field1name = $row["lotid"];
$field2name = $row["product"];
$field3name = $row["ewsflow"];
$field4name = $row["zone"];
$key = $field1name + ":" + $field2name + ":" + $field3name;
if (!in_array($key, $arr)){
echo "<tr>";
echo "<th >Lot ID:</th>";
echo "<td >$field1name</td>";
echo "</tr>";
echo "<tr>";
echo "<th>Product:</th>";
echo "<td>$field2name</td>";
echo "</tr>";
echo "<tr>";
echo "<th>EWSFLOW: </th>";
echo "<td>$field3name</td>";
echo "</tr>";
array_push($arr, $key);
}
echo "<tr>";
echo "<th>Zone:</th>";
echo "<td>$field4name</td>";
echo "</tr>";
}
You can change your query and use GROUP BY feature of MySQL. Below is the query. Ignore any spelling mistakes.
$sql = "SELECT lotid, product, ewsflow, GROUP_CONCAT(zone) FROM productdb.tbl_correlationwafer WHERE lotid = ? GROUP BY lotid, product, ewsflow ORDER BY lotid";
$pq = $mysqli->prepare($sql);
$pq->bind_param('i', $productlotid);
$pq->execute();
$result = $pq->get_result();
$data = $result->fetch_all();
GROUP_CONCAT() function returns a string with concatenated non-NULL value from a group.
GROUP BY statement groups rows that have the same values into summary rows, like "find the number of customers in each country".
You can accomplish the desired output in a much simpler fashion if you were to use group_concat in the SQL query to gather together the various zone columns into a formatted value - then the PHP really needs only process a single row in the recordset and display the desired table format.
The SQL takes advantage of a prepared statement to help mitigate SQL injection - matters not that it is an internal website IMO - always better to be secure!
$sql='SELECT
`lotid`,
`product`,
`ewsflow`,
group_concat( distinct `zone` order by `zone` asc separator ", " ) as `zone`
FROM `productdb`.`tbl_correlationwafer`
WHERE `lotid` = ?
ORDER BY `lotid`';
$stmt=$conn->prepare( $sql );
$stmt->bind_param('s', $productlotid );
$stmt->execute();
$stmt->bind_result( $lotid, $product, $ewsflow, $zone );
$stmt->fetch();
printf('
<table id="corwafer">
<tr>
<th>Lot ID:</th>
<td>%1$s</td>
</tr>
<tr>
<th>Product:</th>
<td>%2$s</td>
</tr>
<tr>
<th>EWSFLOW:</th>
<td>%3$s</td>
</tr>
<tr>
<th>Zone:</th>
<td>%4$s</td>
</tr>
</table>',
$lotid,
$product,
$ewsflow,
$zone
);
So I have a code
<?php
$showorder = "SELECT order_number FROM orders WHERE customer_number=522";
$orderesult = mysqli_query($con, $showorder);
$ord = mysqli_fetch_array($orderesult);
?>
in my database customer number 522 has 2 order numbers, when i tried to show the result, it only shows 1.
Here's my other code
echo "<table>";
echo "<th>Order Number</th><th>Order date</th>";
echo "<tr><td>";
echo $ord["order_number"];
echo "</td><td>";
echo $ord["order_date"];
echo "</td></tr>";
You just need to use while() here for getting all records, something like:
while($ord = mysqli_fetch_array($orderesult)){
//echo all value here
}
Also note that, if you want to print $ord["order_date"] than you must need to select column also in your query.
Otherwise, $ord will only contain order_number value.
Your SQL is missing the extra column.
Current SQL:
SELECT order_number FROM orders WHERE customer_number=522
Change to:
SELECT order_number, order_date FROM orders WHERE customer_number=522
Put mysqli_fetch_array($orderesult); in a while loop.
while($ord = mysqli_fetch_array($orderesult)) {
echo $ord["order_number"];
# code
}
Replace your code with the below code and then try again
<?php
$showorder = "SELECT order_number, order_date FROM orders WHERE customer_number=522";
$orderesult = mysqli_query($con, $showorder);
echo "<table>";
echo "<tr>";
echo "<th>Order Number</th><th>Order date</th>";
echo "</tr>";
while($ord = mysqli_fetch_array($orderesult)) {
echo "<tr>";
echo "<td>$ord['order_number']</td>";
echo "<td>$ord['order_date']</td>";
echo "</tr>";
}
echo "</table>";
?>
echo "<table>";
echo "<th>Order Number</th>";
while($ord = mysqli_fetch_array($orderesult)) {
echo "<tr><td>";
echo $ord["order_number"];
echo "</td></tr>";
}
you must use loop to show all result , and you can use echo one time .
while($ord = mysqli_fetch_array($orderesult)) {
echo "<table>
<th>Order Number</th><th>Order date</th>
<tr><td>".
$ord["order_number"]."
</td></tr>";
}
When I add my first record into the database table, it doesn't show on the page where the records are displayed. But when I add the second record and on, they are displayed on the page except the first record that I entered.
Here is my code:
$result = mysql_query("SELECT * FROM members ORDER BY player_role DESC", $db);
while ($row = mysql_fetch_array($result))
{
echo "<table>";
echo"<tr><th><B>Player Name</B><Th><B>Role</B></TR>";
while ($myrow = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>". $myrow['player_name']. "</td>";
echo "</td>";
echo "<td>" .$myrow['player_role']. "</td>";
echo "</tr>";
}
echo "</table>";
}
Can someone please tell me what is wrong?
It may be caused by nested while() loop. No need to use nested while(). Use one while() instead. Example:
$result = mysql_query("SELECT * FROM members ORDER BY player_role DESC", $db);
echo "<table>";
echo"<tr><th><B>Player Name</B></th><th><B>Role</B></th></tr>";
while ($row = mysql_fetch_array($result))
{
echo '<tr><td>'.$row['player_name'].'</td><td>'.$row['player_role'].'</td></tr>';
}
echo "</table>";
Player Name and Role should not be inside while() loop.
MOST IMPORTANT Do not use mysql, it is deprecated. Instead use mysqli
Your code (changed)
$result = mysqli_query($db,"SELECT * FROM members ORDER BY player_role DESC");
echo "<table>";
echo"<tr><th><B>Player Name</B></th><th><B>Role</B></th></tr>";
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
echo '<tr><td>'.$row['player_name'].'</td><td>'.$row['player_role'].'</td></tr>';
}
echo "</table>";
The code below creates html table data.
Row 1 is correct and returns column1 with $row['LNAME'] and then column2 with ALL of the $row1['NAME'] results (there are four).
Row 2 and onwards echo only column1 and an empty cell in column2.
while ($row = $sql->fetch_assoc()){
echo "<tr>";
echo "<td>";
echo $row['LNAME'];
echo "</td>";
echo "<td>";
while($row1 = $sql1->fetch_assoc()){
echo $row1['NAME'] . "<br>";
}
echo "</td>";
echo "</tr>";
}
It would seem that after the inner loop has completed it evaluates as false in future iterations of the outer loop where I would like it to evaluate as true.
What should I change so that the inner loop does not evaluate as false until the outer loop does?
This is because you're at the end of $sql1. You can seek to the beginning of it again, but my preference is to separate pulling the data from the DB and the iterations, so I'd write it like this (using your code as the starting point):
$lname = array();
while($row = $sql->fetch_assoc()){
$lname[] = $row;
}
$name = array();
while($row1 = $sql1->fetch_assoc()){
$name[] = $row1;
}
foreach($lname as $lvalue) {
echo "<tr>";
echo "<td>";
echo $lvalue;
echo "</td>";
echo "<td>";
foreach($name as $value) {
echo $value . "<br>";
}
echo "</td>";
echo "</tr>";
}
you will need to use mysql_data_seek($rs, 0) to reset your result pointer at the first row. this is under the assumption that mysql is hidden behind your object. for mysqli use mysqli_data_seek respectively
My solution to this is at the bottom
My issue is: I am trying to display foreign key data, but because there is more than one foreign key, I am getting a 'duplicate' query for each of the foreign keys.
http://i.imgur.com/Gfqx497.png
As you can see, I can query the correct data, but I don't know how to attach the other foreign key data to the same 'one line output'.
I've been lurking stackoverflow for a while to find an answer to my problem and I'm at a wits end. I have found quite a number of threads, such as the two links below, where I believe people are asking the same thing, however I can't seem to wrap my head around getting the solution to work in my case. From my understanding, I need to be using aliases for the tables, however I've tried multiple different interpretations of the solutions and can't recreate the solution.
How do I merge two or more rows based on their foreign key
mysql query 2 foreign keys
--
I've got two tables ('Minions and Ability'), one of which has four foreign keys linking to the other.
http://i.imgur.com/ctpFHur.png
This is the php code that I'm using for the query, which is mostly taken from PHP and MySQL Web Development 4th Edition (Welling, Thomson) which I purchased to get me started with php and mysql.
$query = "SELECT minions.name, minions.summon, minions.attack,
minions.health, minions.race, minions.rarity,
minions.ability1, minions.ability2, minions.ability3,
minions.ability4, minions.imagebig,
ability.ability
AS ability FROM minions
INNER JOIN ability on
minions.ability1 = ability.abilityid
OR minions.ability2=ability.abilityid";
//Only trying for 2 foreign keys to try get it to work
$result = $db->query($query);
$num_results = $result->num_rows;
echo "<p>Number of items found: ".$num_results."</p>";
for ($i=0; $i <$num_results; $i++){
$row = $result->fetch_assoc();
//echo "<p><strong>".($i+1).". Name: ";
echo "<p><strong>";
echo htmlspecialchars(stripslashes($row['name']));
echo "</strong><br />Summoning cost: ";
echo stripslashes($row['summon']);
echo "<br />Attack: ";
echo stripslashes($row['attack']);
echo "<br />Health: ";
echo stripslashes($row['health']);
echo "<br />Race: ";
echo stripslashes($row['race']);
echo "<br />Rarity: ";
echo stripslashes($row['rarity']);
//if (stripslashes($row['ability'] != NULL)){
echo "<br />Abilty: ";
echo stripslashes($row['ability']);
//}
echo "<br />";
$imageMinion = stripslashes($row['imagebig']);
// $iwidth = 25;
// $iheight = 100;
// echo '<img src="img/'.$imageMinion.'.png" style="width:'.$iwidth.'px;height:'.$iheight.'px;">';
//echo "<br />";
echo '<img src="img/'.$imageMinion.'.png">';
echo "</p>";
Could someone please guide me to getting this to display correctly? I've tried to follow the other solutions and just can't seem to get the alias naming correct, if I'm correct in thinking that is the solution.
========EDIT REGARDING ANSWER FROM verbumSapienti===========
I am embarrassingly unable to get your Answer to work. This is how the code looks.
$query = "SELECT minions.name, minions.summon, minions.attack, minions.health,
minions.race, minions.rarity, minions.ability1, minions.ability2,
minions.ability3, minions.ability4, minions.imagebig,
ability.ability
AS ability
FROM minions
INNER JOIN ability
ON minions.ability1 = ability.abilityid
OR minions.ability2 = ability.abilityid
OR minions.ability3 = ability.abilityid
OR minions.ability4 = ability.abilityid";
$result = $db->query($query);
$num_results = $result->num_rows;
echo "<p>Number of items found: ".$num_results."</p>";
for ($i=0; $i <$num_results; $i++){
$row = $result->fetch_assoc();
$abilities = array('ability1', 'ability2', 'ability3', 'ability4');
foreach($abilities as $ability)
{
$q = "SELECT $ability FROM minions WHERE name={$row['name']}";
$result = $db->query($q);
$row2 = $result->fetch_assoc();
$abilitiesArr[] = $row2[$ability];
}
echo "<p><strong>";
echo htmlspecialchars(stripslashes($row['name']));
echo "</strong><br />Summoning cost: ";
echo stripslashes($row['summon']);
echo "<br />Attack: ";
echo stripslashes($row['attack']);
echo "<br />Health: ";
echo stripslashes($row['health']);
echo "<br />Race: ";
echo stripslashes($row['race']);
echo "<br />Rarity: ";
echo stripslashes($row['rarity']);
foreach($abilitiesArr as $ability)
{
$q = "SELECT $ability FROM ability";
$result = $db->query($q);
$row = $result->fetch_assoc();
echo "<br />Ability: $row";
}
/*if (stripslashes($row['ability'] != NULL)){
echo "<br />Abilty: ";
echo stripslashes($row['ability']);
}*/
echo "<br />";
$imageMinion = stripslashes($row['imagebig']);
echo '<img src="img/'.$imageMinion.'.png">';
echo "</p>";
}
I've tried changing around a few things and haven't had any success. As is, I get the following error:
Fatal error: Call to a member function fetch_assoc() on a non-object in D:\Xampp\htdocs\ocduels\results.php on line 87
Which is:
$row2 = $result->fetch_assoc();
In:
$abilities = array('ability1', 'ability2', 'ability3', 'ability4');
foreach($abilities as $ability)
{
$q = "SELECT $ability FROM minions WHERE name={$row['name']}";
$result = $db->query($q);
$row2 = $result->fetch_assoc();
$abilitiesArr[] = $row2[$ability];
}
::MY SOLUTION TO THIS::
This seems to work. I don't think its efficient, but its enough to allow me to continue learning. Thank you for all the responses. This allows me to find a 'Minion' and only have one instance of the 'Minion' when there is more than 1 Foreign Key with data.
$query = "SELECT
m.name as m_name,
m.summon as m_summon,
m.attack as m_attack,
m.health as m_health,
m.race as m_race,
m.rarity as m_rarity,
m.ability1 as m_ability1,
m.ability2 as m_ability2,
aa.ability as a_ability,
ab.ability as b_ability,
m.imagebig as m_imagebig
FROM minions m
LEFT JOIN ability aa
ON m.ability1 = aa.abilityid
LEFT JOIN ability ab
ON m.ability2 = ab.abilityid";
$result = $db->query($query);
$num_results = $result->num_rows;
echo "<p>Number of items found: ".$num_results."</p>";
for ($i=0; $i <$num_results; $i++){
$row = $result->fetch_assoc();
echo "<p><strong>";
echo htmlspecialchars(stripslashes($row['m_name']));
echo "</strong><br />Summoning cost: ";
echo stripslashes($row['m_summon']);
echo "<br />Attack: ";
echo stripslashes($row['m_attack']);
echo "<br />Health: ";
echo stripslashes($row['m_health']);
echo "<br />Race: ";
echo stripslashes($row['m_race']);
echo "<br />Rarity: ";
echo stripslashes($row['m_rarity']);
if (stripslashes($row['a_ability'] != NULL)){
echo "<br />Ability 1: ";
echo stripslashes($row['a_ability']);
}
if (stripslashes($row['b_ability'] != NULL)){
echo "<br />Ability 2: ";
echo stripslashes($row['b_ability']);
}
echo "<br />";
$imageMinion = stripslashes($row['m_imagebig']);
echo '<img src="img/'.$imageMinion.'.png">';
echo "</p>";
}
Try DISTINCT keyword to restrict duplicate values.
SELECT DISTINCT minions.name, minions.summon, minions.attack,
minions.health, minions.race, minions.rarity,
minions.ability1, minions.ability2, minions.ability3,
minions.ability4, minions.imagebig,
ability.ability
AS ability FROM minions
INNER JOIN ability on
minions.ability1 = ability.abilityid
OR minions.ability2=ability.abilityid";
you could try a subquery that prints only the ability text for each ability ID contained in each minion's attributes, maybe something along the lines of:
$abilities = array('ability1', 'ability2', 'ability3', 'ability4');
foreach($abilities as $ability)
{
$q = "SELECT $ability FROM minions WHERE name={$row['name']}";
$result = $db->query($q);
$row2 = $result->fetch_assoc()
$abilitiesArr[] = $row2[$ability];
}
then replace
echo "<br />Abilty: ";
echo stripslashes($row['ability']);
with
foreach($abilitiesArr as $ability)
{
$q = "SELECT $ability FROM ability";
$result = $db->query($q);
$row = $result->fetch_assoc()
echo "<br />Ability: $row";
}