dcsimg
www.webdeveloper.com
Results 1 to 3 of 3

Thread: Select multiple values from a single field mysql php my admin

  1. #1
    Join Date
    Jan 2017
    Posts
    6

    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 :

    PHP Code:
    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>

  2. #2
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    1,727
    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?
    JG
    PS - If you're posting here you should be using:

    error_reporting(E_ALL);
    ini_set('display_errors', '1');


    at the top of ALL php code while you develop it!

  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    21,866
    Also when I am tryinf for an example to select the user with 2 titls
    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
    Code:
    select <field list>
    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
    <where clause>
    Caveat: my column names and such may be wrong
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles