Jump to content

Pulling Data From A Database


wantabe2

Recommended Posts

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
Share on other sites

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>

Edited by Barand
Link to comment
Share on other sites

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
Share on other sites

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;

Edited by Muddy_Funster
Link to comment
Share on other sites

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
Share on other sites

$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

 

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

 

 

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.