/    Sign up×
Community /Pin to ProfileBookmark

Select multiple values from a single field mysql php my admin

Here is the thing. I am trying to create an oop php login form.There are 2 tables, users and applications where applications have 2 fields -> title [50 chars] -> active [boolean] in which I have set the title as primary key. By the other hand The users table construction is : id->primary key,name [50 chars],password [50 chars] and in the end the foreign key from applications ,title.

Also when I am tryinf for an example to select the user with 2 titls ,eg , a user with 2 applications and since I insert the data to the database I canot select it. When the other users with one title->application are displayed normally. SO my question is why I cannot display one user with 2 values as a title instead of user with one value.

here is my code :

[code=php]1)Part with the necessary part of selection.

<?php
define(‘DB_SERVER’,’localhost’);
define(‘DB_USER’,’super_user’);
define(‘DB_PASS’ ,’fotis281986#’);
define(‘DB_NAME’, ‘login_oop’);

class DB_con
{

function __construct()
{
$conn = mysql_connect(DB_SERVER,DB_USER,DB_PASS) or die(‘localhost connection problem’.mysql_error());
mysql_select_db(DB_NAME, $conn);
}

public function select()
{
$res = mysql_query(“SELECT u.* , a.* FROM users u,applications a WHERE u.title = a.title”);
//$length = mysql_field_len($res, $title);
//echo $length;
if($res === FALSE) {
die(mysql_error()); // TODO: better error handling
} else {return $res;}

}

2)Part with important parts of index.php

<?php
include_once ‘mysql.php’;
$con = new DB_con();
$res=$con->select();
$res2= $con->select_app();
?>

<?php
while($row=mysql_fetch_array($res))
{
?>
<tr>
<td><?php echo $row[‘id’]; ?></td>
<td><?php echo $row[‘name’]; ?></td>
<td><?php echo($row[‘title’]); ?> </td>

<td align=”center”><a href=”javascript:edit_id(<?php echo $row[0]; ?>)”><img src=”b_edit.png” alt=”EDIT” /></a></td>
<td align=”center”><a href=”javascript:del_id(<?php echo $row[0]; ?>)”><img src=”b_drop.png” alt=”DELETE” /></a></td>
</tr>

<?php
}
?><td align=”center”><a href=”javascript:create_id(<?php echo $row[0]; ?>)”><img src=”b_create.png” alt=”create” /></a></td>
</table>[/code]

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@ginerjmApr 21.2017 — You state that the 'users' table has a 'primary_key' field. Where in your select do you find the user with a certain primary key? You are just doing a join of user and applications with matching titles. Plus if you have users with multiple titles then you have multiple primary keys for that specific user.

If you can't explain it better perhaps you could show us 2-3 records from each table so we can see what you are doing.

Also - a 50 character key for a table is kinda big. Instead of a complex string field like that why do you not use a user-id value that links the two tables?
Copy linkTweet thisAlerts:
@NogDogApr 21.2017 — Also when I am tryinf for an example to select the user with 2 titls[/quote]

This one-to-many and possibly many-to-many relationship suggest to me that you need a 3rd "pivot" table between users and applications, which would just consist of the users primary key and the applications primary key as its two columns. Then you would join across it with something like
<i>
</i>select &lt;field list&gt;
from users u
inner join user_to_application ua on u.id = ua.user_id
inner join applications a on ua.application_id = a.id
&lt;where clause&gt;

Caveat: my column names and such may be wrong ?
×

Success!

Help @Fotis 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.20,
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,
)...