March 8th, 2013, 08:24 PM
-
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;
March 8th, 2013, 09:18 PM
-
What is items intended to return? Usually I use a while() loop echoing <OPTION> lines.
March 8th, 2013, 10:23 PM
-
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;
March 9th, 2013, 07:25 AM
-
No go, any other suggestions? I am 100% stuck, at a loss for where to loook for answers.
March 9th, 2013, 07:58 AM
-
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.
March 9th, 2013, 07:59 AM
-
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>
March 9th, 2013, 08:17 AM
-
No go, any other suggestions? I am 100% stuck, at a loss for where to look for answers.
Comments on this post
March 9th, 2013, 08:27 AM
-
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>
March 9th, 2013, 09:12 AM
-
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>
March 9th, 2013, 09:24 AM
-
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.
March 9th, 2013, 03:42 PM
-
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.