#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    12
    Rep Power
    0

    Question Populate from MySQL...stuck


    I am stuck. Connection to db is working but I cant seem to be able to get the dropdown box on my form to populate...all I see is Please Select with an empty clickable row below. Help!?

    PHP Code:
    $items "|Please Select[c]\n";

    $host="localhost"// Host name
    $username="xxxxxxxxxx"// Mysql username
    $password="xxxxxxxx"// Mysql password
    $db_name="test_db"// Database name
    $tbl_name="test_table"// Table name

    //Connect to server and select database.
    $conn mysql_connect("$host""$username""$password")or die("cannot connect");
    mysql_select_db("$db_name")or die("cannot select DB");

    if(! 
    $conn){ die('Could not connect: ' mysql_error());}echo "Connected successfully\n";

    $result mysql_query("SELECT * FROM test_table" ) or die(mysql_error());

    foreach (
    $result as $r)
    $items .= $r->id '|' $r->corpprice "\n";
    return 
    $items
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    667
    Rep Power
    6
    What is items intended to return? Usually I use a while() loop echoing <OPTION> lines.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    12
    Rep Power
    0
    Originally Posted by Triple_Nothing
    What is items intended to return? Usually I use a while() loop echoing <OPTION> lines.
    Here is the modified code with while (). I can see the array print and everything is correct but the dropdown still does not populate?

    $items = "|Please Select[c]\n";

    $host="localhost"; // Host name
    $username="xxxxxxxxxx"; // Mysql username
    $password="xxxxxxxx"; // Mysql password
    $db_name="test_db"; // Database name
    $tbl_name="test_table"; // Table name

    //Connect to server and select database.
    $conn = mysql_connect("$host", "$username", "$password")or die("cannot connect");
    mysql_select_db("$db_name")or die("cannot select DB");

    if(! $conn){ die('Could not connect: ' . mysql_error());}echo "Connected successfully\n";

    $result = mysql_query("SELECT * FROM test_table" ) or die(mysql_error());



    while($r[]=mysql_fetch_assoc($result));

    echo "<pre>";
    print_r ($r);
    echo "</pre>";



    foreach ($result as $r)
    $items .= $r->id . '|' . $r->corpprice . "\n";
    return $items;
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    12
    Rep Power
    0

    Angry


    No go, any other suggestions? I am 100% stuck, at a loss for where to loook for answers.
  8. #5
  9. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,927
    Rep Power
    1045
    Hi,

    an associative array isn't an object, so you cannot use the "->" syntax on it. Actually, your screen should be flooded with error messages, but I guess your PHP is misconfigured. Turn on all error messages in your php.ini:

    Code:
    error_reporting = -1;
    display errors = On;
    Or do it in the code itself.

    Also the old MySQL extension is ancient, it's obsolete since almost a decade and will be officially deprecated in the next PHP version. If you got any chance to switch to one of the "new" extensions, do it.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    667
    Rep Power
    6
    This would be similar to something I may do in your situation. I usually have such drop-downs built a few times in my projects, so I create it as a function. This has been edited to fit your single item, as well as updated to use mysqli instead of the deprecated mysql.

    PHP Code:
    <SELECT name="somename">
      <OPTION>Please Select...</OPTION>
      <?php
        $statement 
    $link->prepare("SELECT `id`, `corpprice` FROM `test_table`");
        
    $statement->execute();
        
    $statement->bind_result($col1,$col2);
        while (
    $statement->fetch()) {
          echo 
    '<OPTION value="somevalue">' $col1 ' | ' $col2 "</OPTION>\n";
        }
        
    $statement->close();
      
    ?>
    </SELECT>
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    12
    Rep Power
    0

    Angry


    No go, any other suggestions? I am 100% stuck, at a loss for where to look for answers.

    Comments on this post

    • Jacques1 disagrees : That's all you have to say after two detailed replies?
    • ptr2void disagrees : WTF? Just looking for a complete handout no doubt. Oh Noes! Don't make me actually LEARN how to do something!
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    667
    Rep Power
    6
    Other than that being a copied "No go." reply, you must be doing something other than any of us know. What I handed you was clean and basic.

    Can you perhaps manually write up the script you would like your PHP script to print out? It's apparently not the drop-down shown below...

    Code:
    <SELECT name="favcolor">
      <OPTION value="red">
      <OPTION value="green">
      <OPTION value="blue">
      <OPTION value="yellow">
    </SELECT>
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    12
    Rep Power
    0
    Sorry, just very frustrated with this. I realize it simple and yet I just can't figure it out. I appreciate all help given.

    Here is the code with your suggestion. I get the dropdown box with Please Select and message Connected successfully but that is it. So I know that connection to db works but where I am going wrong with populating dropdown box I am at a loss. I have been at it for days and Googled it to bits...Charpters here I come I guess...lol

    The table has one row with id1 and 50 columns. Each column has a value:

    Column1 = Value1
    Column2 = Value2
    and so on

    This all code I have. There is nothing else.

    PHP Code:
    <?php

    $host
    ="localhost"// Host name
    $username="xxxx"// Mysql username 
    $password="xxxx"// Mysql password
    $db_name="test_db"// Database name
    $tbl_name="test_table"// Table name

    //Connect to server and select database.
    $conn mysql_connect("$host""$username""$password")or die("cannot connect");
    mysql_select_db("$db_name")or die("cannot select DB");

    if(! 
    $conn){ die('Could not connect: ' mysql_error());}echo "Connected successfully\n";
    ?>
    <SELECT name="somename">
      <OPTION>Please Select...</OPTION>
    <?php
        $statement 
    $link->prepare("SELECT `id`, `corpprice` FROM `test_table`");
        
    $statement->execute();
        
    $statement->bind_result($col1,$col2);
        while (
    $statement->fetch()) {
          echo 
    '<OPTION value="somevalue">' $col1 ' | ' $col2 "</OPTION>\n";
        }
        
    $statement->close();
      
    ?>
    </SELECT>
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    667
    Rep Power
    6
    I'm sorry. Perhaps I should have also included my build of $link. That is the whole connection to the DB.

    PHP Code:
    <?php
        $host 
    'localhost';
        
    $user 'usrname';
        
    $password 'pass';
        
    $database 'dbname';
        
    $link = new mysqli($host$user$password$database);
        if (
    mysqli_connect_errno()) {
            die(
    'Unable to connect to database [' $db->connect_error ']');
        }
    ?>

    This replaces everything above the opening SELECT in your script.
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    12
    Rep Power
    0
    Thanks very much. This worked. I don't get where I was going wrong but clearly it had something to do with the connection I was making. This will give me a direction so thanks a lot again.

IMN logo majestic logo threadwatch logo seochat tools logo