2

So my question is kind of confusing. I'm not a very strong coder.
I hope this makes sense. This website is someone else's work, so i'm trying to add to it.

I'm trying to make a dropdown box that pulls results from a database. If anything looks wrong, please tell me. I cannot seem to get any reults except for a blank dropdown box.

(aps2_get_user_role($_SESSION['uid']) == APS_ADMIN) {
                    $return .= '<br/><select>';
                    $selvalue .= 1001;
                    $result5 .= @mysql_query("SELECT * FROM aps2_users WHERE role = '1001'");


                    while($urow = @mysql_fetch_array($result5, MYSQL_ARRAY)){
                        $return .= '<option value="test">test</option>';
                        $usrid .= $urow['uid'];
                        $usrname .= $urow['username'];
                        $return .= '<option value="' . $usrid . '">"' . $usrname . '"</option>';
                    }
                $return .= '</select>';
                $return .= $result5;
                }
Mark Davidson
  • 5,503
  • 5
  • 35
  • 54
Derek
  • 69
  • 1
  • 2
  • 7
  • Why are you using concatenation `.=` everywhere? Do you understand the difference between concatenation and assigning value? – u_mulder Aug 02 '13 at 20:41
  • Nope, I don't know the difference. That is just what the previous coder left everywhere so I just followed his code. – Derek Aug 02 '13 at 20:44
  • Then you should read some manuals and change this piece of code accordingly. – u_mulder Aug 02 '13 at 20:45
  • 1
    Wow, that was in no way helpful. Are you just here to point out others mistakes or help people? – Derek Aug 02 '13 at 20:49
  • It's not an answer, so it shouldn't be helpful. But if you don't understand coding basics - nothing to talk about, sorry. – u_mulder Aug 02 '13 at 20:54
  • Well, like the original question says, i'm not a strong coder. That's why i'm here. – Derek Aug 02 '13 at 20:59
  • You mean a user starts typing in the box, and then the page suggests mysql values based on what you typed? If so, you should be using AJAX. – pattyd Aug 02 '13 at 21:10
  • Lol @u_mulder _"It's not an answer, so it shouldn't be helpful."_... so the comments can be super annoying now? – pattyd Aug 02 '13 at 21:11

2 Answers2

1

I think you want:

(aps2_get_user_role($_SESSION['uid']) == APS_ADMIN) {
    $return = '<br/><select>';
    $result5 = @mysql_query("SELECT * FROM aps2_users WHERE role = '1001'");

    while($urow = @mysql_fetch_array($result5, MYSQL_ARRAY)){
        $return .= '<option value="test">test</option><option value="'.$urow['uid'].'">"'.$urow['username'].'"</option>';
    }
    return ($return.'</select>');
}

But - I don't know what you're trying to achieve with line 1!

You're returning result5 which is the query array in your code which I can't see you'd want. Also assignment versus appending to variables. You also don't use the $selvalue variable anywhere.

If you're getting no results - ensure those fields you're pulling from the database exist in the table.

Also may be worth posting the code that incorporates the results of this into your HTML page..

WindsorAndy
  • 237
  • 1
  • 11
1

There are several things I notice in above piece of code you should correct it

  • $selvalue .= 1001; I haven't seen the usage of this variable
  • @ before the @mysql_query() is not a good practice why just an if else to see the query execution is successful or throws an error
  • $result5 .=@mysql_query(...) no need of concatenation when executing the query result
  • @mysql_fetch_array($result5, MYSQL_ARRAY) remove @ and see if it throws an error
  • $return .= '<option value="test">test</option>'; is this really you need this option in each iteration of the query?
  • $usrid .= $urow['uid']; $usrname .= $urow['username']; for each iteration of the loop the new values will be concatenated with the previous one I am sure you don't need that type of output for your select box
  • The last and main thing $return .= $result5; why the result of mysql_query is appended with the options there is no use of this

Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

Zoe
  • 27,060
  • 21
  • 118
  • 148
M Khalid Junaid
  • 63,861
  • 10
  • 90
  • 118