i have one table named players with 2 columns: players and status
I need to select unique random teams!
Every team has X players as: Y base and Z pivots
Y and Z its set due input form
If in insert 3 for base and 2 for pivots The result should be like 3 teams or 3 players:
team 1
Base 1
Pivot 1
Pivot 2
team 2
Base 1
Pivot 1
Pivot 2
team 3
Base 1
Pivot 1
Pivot 2
After i generate teams i must be able to set status as "selected". If i need to create another team all users with status "selected" can't be use in another new team!
I currently use:
$nteams=$_POST['teams'];
$nbase=$_POST['base'];
$npivots=$_POST['pivots'];
$allplayers=$nteams*($nbase+$npivots);
require_once "connect_to_mysql.php";
$sqlCommand = "SELECT id FROM players ORDER BY RAND() LIMIT $allplayers";
$query = mysql_query($sqlCommand) or die (mysql_error());
if(mysql_num_rows($query)<$allplayers) // sanity
die('Not enough players!');
else
for($team=1;$team<=$nteams;$team++)
{
for($base=1;$base<=$nbase;$base++)
{
$row = mysql_fetch_array($query);
echo "Team $team Base $base = {$row['id']}<br />";
}
for($pivot=1;$pivot<=$npivots;$pivot++)
{
$row = mysql_fetch_array($query);
echo "Team $team Pivot $pivot = {$row['id']}<br />";
}
}
mysql_close();
Add WHERE status<>'selected' to your $sqlCommand statement
Store all read IDs from the database in an array, e.g. $selectedIds
Run update script UPDATE players SET status='selected' WHERE id IN ('.implode(',', $selectedIds).')
Side notes:
Consider rewriting the whole loop. It can be done in one while loop and one cursor/flag - let this be a homework :)
Beware of SQL Injection
You can wrap SELECT and UPDATE in a transaction to avoid inconsistency and conflicts
Related
I have this simple query:
$q5 = "select listingid FROM userlisting WHERE userid = '$_SESSION[UserID]'";
$r5 = mysql_query($q5) or die(mysql_error());
$a5 = mysql_fetch_array($r5);
The userlisting table is a many to many relationship. It is used as a lookup table. Each userid can be associated with multiple listingids and vice versa.
Also in my file (a html template file) I have this code
if(!empty($_SESSION[UserID]) and $a5['listingid'] == $_GET['id']) :
So I am wanting to check if the listingid column in userlisting table = the id of the page (well, it is a property website and it is the id of the property). As each user can be associated with many listingids I need to be able to check multiple rows. But for some reason it only checks the very first row.
In the userlisting table there is the following test entries:
userid | listingid
1 1
1 2
So one user associated to two listingids. However, it is acting like this userid is only associated with listingid 1, the very first row.
You are only grabbing the first entry.
$a5 = mysql_fetch_array($r5);
This does not fetch the whole query result, but only the first row. If there is none, it will return false.
You have to create a loop to get more results.
i want to get values from two different tables.
my problem is the table have no relation to each other but the "team_id"
i have a "player" and a "player_stat" table.
my result should look like:
Playername (from the "player" table)
Points: 8, Assists: 2, Fouls: 0 (from the "player_stat" table)
Playername 2 (from the "player" table)
Points: 8, Assists: 2, Fouls: 0 (from the "player_stat" table)
Here is my try:
<?php
/* PLAYERSTAT */
$attrs = array(PDO::ATTR_PERSISTENT => true);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare("SELECT data.player_stat
FROM
(SELECT * from player_stat
UNION
SELECT * from player) data
");
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
?>
echo $row["playername"]."<br>";
echo $row["points"]." ".$row["assists"];
<?php }
?>
my tables
player:
id
team_id
name
created
player_stat:
id
team_id
points
assists
fouls
created
Your SQL query could look like this (supposed the team_id is supposed to connect the stats and the player):
SELECT
player.playername,
player_stat.points,
player_stat.assists,
player_stat.fouls
FROM
player
LEFT JOIN player_stat ON (player.team_id = player_stat.team_id)
But it's a little bit strange, that the only relation between your tables is the team_id. If the output should be per player you have to introduce the relation between the stats and the player.
Say, your player can be in a team, there should be a team_id and a player_id. And your JOIN would then read:
LEFT JOIN player_stats ON (player.player_id = player_stats.player_id)
and as addition you can query for your team:
WHERE
player.team_id = 55
or you can add other joins so that you get your team details and so on.
Modify your select statement to read:
SELECT * FROM player, player_stat WHERE player.team_id = player_stat.team_id
The above is based on your statement that they have the team_id in common. If this is not what you are trying to achieve please refine your question and post the database schema.
I want to fetch only 3 data from database table when any user login.And after that when the next user login the next 3 question will come. How can I do this? What changes should I made in SELECT query?
$sql ="SELECT * FROM questiontypes";
$query = mysql_query($sql);
$qset = "";
while($result = mysql_fetch_array($query)) {
$qset .='<li id="qtypeli">'.$result['qtname'].'</li>';
$sql1 = "select questions .* from questions where qtype='".$result['qtname']."'order by RAND() LIMIT 3";
$query1 = mysql_query($sql1);
while($result1 = mysql_fetch_array($query1)) {
$qset .='<li id="'.$result1['qid'].'" qt="'.$result1['qtid'].'"><span class="qnamespan"> '.$i.'. '.$result1['qname'].'</span></li>
you can set a field in table called taken, in first time the 3 rows taken should be updated with the value 1 in taken field,rest rows will have null in taken. when second time just get the row with max id which has taken=1, increment that id by 1 and get next 3 rows from that id, after getting set the new 3 rows of taken 1 and remove 1 from old 3 rows thats the logic you can apply
I have a table which holds details of exams taken. Each exam entered into this table is entered as a pair of people i.e. two people per exam performed so you have per exam, two people. Each person has their own unique id for each exam taken but each pair has a value called partner_id which is the same for them both.
What I am trying to do is extract the partner_id values from that table in order to then be able to use those values to find the partners of a said person and show/echo onto the page the exam result details of every partner that person has had exam with.
What I have tried so far is:
$partner_ider = mysql_query("SELECT partner_id as value1 from exam WHERE Student_email='eating#gnomes.com'");
$row1 = mysql_fetch_array($partner_ider);
while($row1 = mysql_fetch_array($partner_ider))
{
echo $row1['value1'];
}
And this:
$result = mysql_query("SELECT * from exam WHERE Student_email='beating#dead.com'");
$row = mysql_fetch_array($result);
while ($row = mysql_fetch_array($result))
{
echo $row['partner_id'];
}
The result these would give is 2 for that email of which has two entries in the exam table, what I was looking for was 1, 2 which are the values of the two partner_ids for this email's exam records. When I change the email to someone else who only has one, it returns nothing.
What I would be looking to do with the result of 1, 2 is to use those values to select all other people except the original person(eating#gnomes.com) and show their details from out of that table.
What I'm asking for is how would I go about doing the above as I haven't done something like this before?
In both code snippets, you have at line 2 a mysql_fetch_array() which should not be there.
It fetches a row, puts it into $row1 and increments the internal pointer.
When you call mysql_fetch_array() in your while, it fetches the second record then the third etc. until all the rows have been processed.
You should, in both examples, remove the second line and try again.
$partner_ider = mysql_query("SELECT partner_id as value1 from exam WHERE Student_email='eating#gnomes.com'");
//$row1 = mysql_fetch_array($partner_ider);
while($row1 = mysql_fetch_array($partner_ider))
{
echo $row1['value1'];
}
If your table structure is the following :
id
student_id
student_name
student_email
student_whatever
partner_id
The SQL query would look like
SELECT Student_email FROM exam WHERE partner_id IN (SELECT partner_id FROM exam WHERE student_Email = 'eating#gnomes.com') AND Student_email <> 'eating#gnomes.com';
But you should really split up your table. You have two entities (a student and an exam) and a joining table.
Student
id
name
email
Exam
id
name
(other exam data)
StudentGroup
exam_id
student_id
group
In PHP, I have an array of 11 persons where just the ID of each person is given:
$persons = array(1, 3, 39, 72, 17, 20, 102, 99, 77, 2, 982);
In my MySQL database, there's a table containing detailed information about each person:
TABLE personInfo
- ID (integer)
- name (string)
- birth date (timestamp)
- weight (decimal)
- ...
PROBLEM:
So now, I want to select the matching name for each ID in the PHP array. I can only imagine two solutions to do this:
1. for-loop:
foreach ($persons as $person) {
$result = mysql_query("SELECT name FROM personInfo WHERE id = ".$person);
}
2. logical operator OR
$result = mysql_query("SELECT name FROM personInfo WHERE id = 1 OR id = 3 OR id = 39 OR ...");
Both solutions are slow, aren't they?
But if I had another MySQL table containing the IDs of the PHP array ...
TABLE ids
- ID (integer)
... I could use a join to make a really fast MySQL query, right?
$result = mysql_query("SELECT a.ID, b.name FROM ids AS a JOIN personInfo AS b ON a.ID = b.ID");
QUESTION:
Is all this correct so far? If yes: Why is this so? The MySQL query is faster if I have a second table? With only one table it is incredibly slow? What is the fastest way to solve my problem (selecting the names matching the IDs of the PHP array)?
If you have lots of ids (several hundreds or more), feeding the values into a temporary table and joining it is actually faster.
You may want to read this article:
Passing parameters in MySQL: IN list vs. temporary table
IF you have ID's you should just query the Id's you have. So
$result = mysql_query("SELECT name FROM personInfo WHERE id = 1 OR id = 3 OR id = 39 OR ...");
would be right, although I would use
$result = mysql_query("SELECT name FROM personInfo WHERE id IN (1,2,3,4....) )