Guest Posted December 13, 2012 Share Posted December 13, 2012 I have a phpmyadmin database named FLOW. I have a table in that database named NAMES. I have a form that has the below code in it: <td><b>Assign To:</b> <br><input type="text" name="name" size="15" maxlength="30" value="" /><br /> </td> My question is, how would I edit this code to pull the data from the table listed above for the value? Link to comment https://forums.phpfreaks.com/topic/271971-pulling-data-from-a-database/ Share on other sites More sharing options...
Jessica Posted December 13, 2012 Share Posted December 13, 2012 SELECT * FROM NAMES Link to comment https://forums.phpfreaks.com/topic/271971-pulling-data-from-a-database/#findComment-1399234 Share on other sites More sharing options...
Guest Posted December 13, 2012 Share Posted December 13, 2012 for the value=" " Is there any way for me to randomly insert a name in there each time the form is displayed in a browser? I have a list of 5 names I'd like to randomly be displayed. Is that possible? Link to comment https://forums.phpfreaks.com/topic/271971-pulling-data-from-a-database/#findComment-1399250 Share on other sites More sharing options...
MDCode Posted December 13, 2012 Share Posted December 13, 2012 In your query you can use ORDER BY RAND() but will make your site slow with large tables. Link to comment https://forums.phpfreaks.com/topic/271971-pulling-data-from-a-database/#findComment-1399252 Share on other sites More sharing options...
Jessica Posted December 13, 2012 Share Posted December 13, 2012 If it's always going to be just those 5, just stick them in an array and use shuffle. Link to comment https://forums.phpfreaks.com/topic/271971-pulling-data-from-a-database/#findComment-1399254 Share on other sites More sharing options...
Barand Posted December 13, 2012 Share Posted December 13, 2012 or <?php $mysqli = new mysqli('host', 'user', 'pwd', 'database'); $sql = "SELECT name FROM names ORDER BY RAND() LIMIT 1"; $res = $mysqli->query($sql); $row = $res->fetch_row(); $randomName = $row[0]; $res->free(); ?> <td><b>Assign To:</b> <br><input type="text" name="name" size="15" maxlength="30" value="<?=$randomName?>" /><br /> </td> Link to comment https://forums.phpfreaks.com/topic/271971-pulling-data-from-a-database/#findComment-1399256 Share on other sites More sharing options...
Guest Posted December 14, 2012 Share Posted December 14, 2012 ok, I think I've just about got it with all of your help & thanks! Below is my code: <?php $mysqli = new mysqli('localhost', 'myusername', 'mypassword', 'flow'); $sql = "SELECT names FROM clerk_names ORDER BY RAND() LIMIT 1"; $res = $mysqli->query($sql); $row = $res->fetch_row(); $randomName = $row[0]; $res->free(); ?> <td><b>Assign To:</b> <br><input type="text" name="clerk_names" size="15" maxlength="30" value="<?=$randomName?>" /><br /> </td> In the mysql databse the databse name is flow. The table the names are listed in is named clerk_names & the only fields in thei table is names & clerk_id. When I put the above code in a form & display it in a browser, in the Assign To: field the value shows <?=$randomName?> instead of the result from the query...have I mistyped something? Thanks again for any help or assistance provided. Link to comment https://forums.phpfreaks.com/topic/271971-pulling-data-from-a-database/#findComment-1399352 Share on other sites More sharing options...
MDCode Posted December 14, 2012 Share Posted December 14, 2012 Sounds like you don't have short tags enabled. Link to comment https://forums.phpfreaks.com/topic/271971-pulling-data-from-a-database/#findComment-1399353 Share on other sites More sharing options...
Barand Posted December 14, 2012 Share Posted December 14, 2012 manual said: Note: Starting with PHP 5.4, short echo tag <?= is always recognized and valid, regardless of the short_open_tag setting. Prior to that, if short tags disabled, you need <?php echo $var ?> Link to comment https://forums.phpfreaks.com/topic/271971-pulling-data-from-a-database/#findComment-1399359 Share on other sites More sharing options...
MDCode Posted December 14, 2012 Share Posted December 14, 2012 That's why I said sounds Link to comment https://forums.phpfreaks.com/topic/271971-pulling-data-from-a-database/#findComment-1399360 Share on other sites More sharing options...
Guest Posted December 14, 2012 Share Posted December 14, 2012 still don't get it.... why isn't it returning the sql results in the form value? Link to comment https://forums.phpfreaks.com/topic/271971-pulling-data-from-a-database/#findComment-1399361 Share on other sites More sharing options...
Barand Posted December 14, 2012 Share Posted December 14, 2012 Is the file name ending with ".html" or ".php" Link to comment https://forums.phpfreaks.com/topic/271971-pulling-data-from-a-database/#findComment-1399384 Share on other sites More sharing options...
Guest Posted December 14, 2012 Share Posted December 14, 2012 It ends in .php And I'm using php 5.3.5 Link to comment https://forums.phpfreaks.com/topic/271971-pulling-data-from-a-database/#findComment-1399390 Share on other sites More sharing options...
MDCode Posted December 14, 2012 Share Posted December 14, 2012 If your short tags setting is disabled you will need to follow barand's suggestion here: On 12/14/2012 at 2:04 PM, Barand said: Prior to that, if short tags disabled, you need <?php echo $var ?> Link to comment https://forums.phpfreaks.com/topic/271971-pulling-data-from-a-database/#findComment-1399395 Share on other sites More sharing options...
Muddy_Funster Posted December 14, 2012 Share Posted December 14, 2012 what do you get if you do this? <?php $mysqli = new mysqli('localhost', 'myusername', 'mypassword', 'flow'); $sql = "SELECT names FROM clerk_names ORDER BY RAND() LIMIT 1"; $res = $mysqli->query($sql); $row = $res->fetch_row(); $randomName = $row[0]; $res->free(); $tableRow =<<<TABLE <td> <b>Assign To:</b> <br> <input type="text" name="clerk_names" size="15" maxlength="30" value="$randomName" /><br /> </td> TABLE; echo $tableRow; Link to comment https://forums.phpfreaks.com/topic/271971-pulling-data-from-a-database/#findComment-1399396 Share on other sites More sharing options...
Guest Posted December 14, 2012 Share Posted December 14, 2012 If I try that code I get the below error: Parse error: syntax error, unexpected T_SL in C:\wamp\www\gflow\officer_query\assign_test.php on line 204 on line 204 the code is: $tableRow =<<<TABLE thanks Link to comment https://forums.phpfreaks.com/topic/271971-pulling-data-from-a-database/#findComment-1399414 Share on other sites More sharing options...
Guest Posted December 14, 2012 Share Posted December 14, 2012 Where would I add: <?php echo $var ?> in the code I've tried adding it several places & get random errors. Link to comment https://forums.phpfreaks.com/topic/271971-pulling-data-from-a-database/#findComment-1399415 Share on other sites More sharing options...
Guest Posted December 14, 2012 Share Posted December 14, 2012 Oh, never mind..I got it LOL I'll give it a try thanks Link to comment https://forums.phpfreaks.com/topic/271971-pulling-data-from-a-database/#findComment-1399417 Share on other sites More sharing options...
Guest Posted December 14, 2012 Share Posted December 14, 2012 I got it working thanks to all your help & thanks again!!! I do have one question though..in this line of code: $sql = "SELECT names FROM clerk_names ORDER BY RAND() LIMIT 1"; The limit 1 at the end of the line, what does that do? If I change it to 2 or 3 what does that do? I have 10 names in the database to randomly choose from, is there any way to change this number so the same name will not be chosen twice without starting at the begining of the DB again? Or in other words, any way to change it so a name can not be chosen twice without all names have already been chosen? Link to comment https://forums.phpfreaks.com/topic/271971-pulling-data-from-a-database/#findComment-1399428 Share on other sites More sharing options...
MDCode Posted December 14, 2012 Share Posted December 14, 2012 LIMIT 1 will only gather 1 row from the table. So LIMIT 2 or 3 will gather 2 or 3. The query will not gather the same row twice. Link to comment https://forums.phpfreaks.com/topic/271971-pulling-data-from-a-database/#findComment-1399436 Share on other sites More sharing options...
DavidAM Posted December 14, 2012 Share Posted December 14, 2012 On 12/14/2012 at 7:33 PM, wantabe2 said: $sql = "SELECT names FROM clerk_names ORDER BY RAND() LIMIT 1"; The limit 1 at the end of the line, what does that do? If I change it to 2 or 3 what does that do? That query will select 1 random row from the table and return it On 12/14/2012 at 7:33 PM, wantabe2 said: I have 10 names in the database to randomly choose from, is there any way to change this number so the same name will not be chosen twice without starting at the begining of the DB again? Or in other words, any way to change it so a name can not be chosen twice without all names have already been chosen? That is not a random selection. To do that, you will have to keep track of each row that is returned and exclude it from the selection until all of the rows are excluded. Then remove the exclusions and begin again On 12/14/2012 at 8:12 PM, SocialCloud said: LIMIT 1 will only gather 1 row from the table. So LIMIT 2 or 3 will gather 2 or 3. The query will not gather the same row twice. So, on the 11th run, what happens? Does the query fail? Of course it will return the same row twice! It is even conceivable that it will return the same row twice in succession. Link to comment https://forums.phpfreaks.com/topic/271971-pulling-data-from-a-database/#findComment-1399450 Share on other sites More sharing options...
MDCode Posted December 15, 2012 Share Posted December 15, 2012 Really? Because all my querys that do almost the same thing never return the same row. Link to comment https://forums.phpfreaks.com/topic/271971-pulling-data-from-a-database/#findComment-1399479 Share on other sites More sharing options...
Christian F. Posted December 15, 2012 Share Posted December 15, 2012 I think the two of you might be misunderstanding each other a bit: SocialCloud was stating (as I understood it) that the query would not return duplicate copies the same row, in the same result set, when using LIMIT > 1. While DavidAM stated that it will return the same row(s) on subsequent executions, and possibly even as soon as the very next one. Both of which are indeed correct. Link to comment https://forums.phpfreaks.com/topic/271971-pulling-data-from-a-database/#findComment-1399506 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.