/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] A little help with foreach

Hey

I need a little help with foreach.

I have this database:

formation:
formation_ID: 1 – player_ID: 2 – position: Keeper
formation_ID: 2 – player_ID: 6 – position: Central defence
formation_ID: 3 – player_ID: 4 – position: Left defence
formation_ID: 4 – player_ID: 7 – position: right defence
formation_ID: 5 – player_ID: 3 – position: Central defence2

Get for example only Central defence echo/printed?

i tried this:

[CODE]<?php
include(‘config.php’);
$sql = “SELECT positions_ID, position FROM postions”;
$result = mysql_query($sql);

while($row = mysql_fetch_array($result)){
echo $row[1].”<br>”;
}
?>[/CODE]

but it just prints the intire colum.

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@pcthugJan 07.2007 — What is the [B]positions_ID[/B] of [I]Central defence[/I]?
Copy linkTweet thisAlerts:
@blulagoonJan 07.2007 — Given that arrays normally start at [0] you'd expect that to print out all the positions from the table.

Have you tried substituting $row['positions_ID'] and $row['positions'] to see what you get. If you don't want to print out all the data from the table, you'd need a where clause in your query.

Blu.

edit: i.e.

$sql = "SELECT positions_ID, position FROM postions where positions_ID = 3";

edit2: I think you might have a typo in the table name.
Copy linkTweet thisAlerts:
@NightShift58Jan 07.2007 — You're mixing ROWS and COLUMNS up. In the following, you're asking for ALL rows:[code=php]<?php
include('config.php');
$sql = "SELECT positions_ID, position FROM postions";
$result = mysql_query($sql);

while($row = mysql_fetch_array($result)){
echo $row[1]."<br>";
}
?>[/code]
If you only want one row, you'll have to limit the query to the one you want:[code=php]$POSid = 3;
$sql = "SELECT position FROM postions where positions_ID = '$POSid' LIMIT 1";[/code]
Copy linkTweet thisAlerts:
@JJohnsenDKauthorJan 07.2007 — okay

but what do i do in this case:

i have 11 colums and i want 3 of the colums lets say colums 3, 5 and 8

how do i get these?
Copy linkTweet thisAlerts:
@NightShift58Jan 07.2007 — Eleven columns or eleven rows?

If you meant rows, the solution would be something like this:[code=php]$POSids = array(3,5,8);
$positions = implode(",", $POSids);
$sql = "SELECT position FROM postions where positions_ID IN '$positions'';[/code]
Copy linkTweet thisAlerts:
@JJohnsenDKauthorJan 07.2007 — i meant colums ?
Copy linkTweet thisAlerts:
@NightShift58Jan 07.2007 — Then it doesn't make sense...

What does the "postions" table look like?
Copy linkTweet thisAlerts:
@JJohnsenDKauthorJan 07.2007 — yes sry, i mixed rows and colums up :rolleyes:

But thanks for helping ?
×

Success!

Help @JJohnsenDK spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.24,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...