I have table with the following fields
+----+------+----------+----------+----------+
| id | name | sports_1 | sports_2 | sports_3 |
+----+------+----------+----------+----------+
| 3 | jack | 1 | 0 | 1 |
+----+------+----------+----------+----------+
I want to only display that column names that contain 1. To have the following results.
Jack does sport_1 sports_3
The thing you actually want is another table structure. I'll elaborate a little.
The thing you are describing is a many-to-many relationship. The sports you have in columns should actually be contained in another table called "sports" and in between the "users" table and the "sports" table you have a new table called "users_to_sports" connecting the two.
Check http://sqlrelationship.com/many-to-many-relationship/ for a more in-depth explanation
this is just work around and you may change the structure of your table as chrisR said
but here if you want you can look this if it help you.
$con = mysql_connect("localhost","root","admin");
if (!$con)
{
die('Could not connect ya rafik : ' . mysql_error());
}
mysql_select_db("mydatabase", $con);
$query = mysql_query("SELECT * FROM mytable2 ORDER BY name ");
// please use PDO or mysqli instead.
and then you manage results by php like this
while ($column_data = mysql_fetch_assoc ($query)) {
echo $column_data['name']." does : ";
if ($column_data['sports_1'] == 1 ){echo " Sport1 ";} else {}
if ($column_data['sports_2'] == 1 ){echo " Sport2 ";} else {}
if ($column_data['sports_3'] == 1 ){echo " Sport3 " ;} else {}
echo "<br />" ;
}
the database is like that
id name sports_1 sports_2 sports_3
1 Mark 1 0 1
2 John 0 1 1
the the result will be like that:
John does : Sport2 Sport3
Mark does : Sport1 Sport3
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.