Jump to content


Photo

Pulling Data From A Database

php jscript

  • Please log in to reply
22 replies to this topic

#1 wantabe2

wantabe2

    Advanced Member

  • Members
  • PipPipPip
  • 65 posts

Posted 13 December 2012 - 01:54 PM

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?

#2 Jessica

Jessica

    This is not my name.

  • Gurus
  • 8,982 posts
  • LocationDallas, TX
  • Age:26

Posted 13 December 2012 - 01:55 PM

SELECT * FROM NAMES
My goal in replying to posts is to help you become a better programmer, including learning how to debug your own code and research problems. For that reason, rather than posting the solution, I reply with tips and hints on how to find the solution yourself. See below for useful links when you get stuck.

How to Get Good Help: How to Ask Questions | Don't be a help vampire
Debugging Your Code: Debugging your SQL | What does a php function do? | What does a term mean? | Don't see any errors?
Things You Should Do: Normalize Your Data | use print_r() or var_dump()
Lulz: "Functions should not have side effects." - trq

Please take a look at my new PHP/Web Dev blog: The Web Mason - Thanks!!

#3 wantabe2

wantabe2

    Advanced Member

  • Members
  • PipPipPip
  • 65 posts

Posted 13 December 2012 - 03:57 PM

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?

#4 MDCode

MDCode

    Advanced Member

  • Members
  • PipPipPip
  • 640 posts

Posted 13 December 2012 - 04:08 PM

In your query you can use ORDER BY RAND() but will make your site slow with large tables.

#5 Jessica

Jessica

    This is not my name.

  • Gurus
  • 8,982 posts
  • LocationDallas, TX
  • Age:26

Posted 13 December 2012 - 04:18 PM

If it's always going to be just those 5, just stick them in an array and use shuffle.
My goal in replying to posts is to help you become a better programmer, including learning how to debug your own code and research problems. For that reason, rather than posting the solution, I reply with tips and hints on how to find the solution yourself. See below for useful links when you get stuck.

How to Get Good Help: How to Ask Questions | Don't be a help vampire
Debugging Your Code: Debugging your SQL | What does a php function do? | What does a term mean? | Don't see any errors?
Things You Should Do: Normalize Your Data | use print_r() or var_dump()
Lulz: "Functions should not have side effects." - trq

Please take a look at my new PHP/Web Dev blog: The Web Mason - Thanks!!

#6 Barand

Barand

    Sen . ( ile || sei )

  • Gurus
  • 14,426 posts
  • LocationCheshire, UK

Posted 13 December 2012 - 04:24 PM

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, 13 December 2012 - 04:29 PM.

moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts
|baaSelect| generate js and php code for dynamic linked dropdowns

 


#7 wantabe2

wantabe2

    Advanced Member

  • Members
  • PipPipPip
  • 65 posts

Posted 14 December 2012 - 08:16 AM

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.

#8 MDCode

MDCode

    Advanced Member

  • Members
  • PipPipPip
  • 640 posts

Posted 14 December 2012 - 08:18 AM

Sounds like you don't have short tags enabled.

#9 Barand

Barand

    Sen . ( ile || sei )

  • Gurus
  • 14,426 posts
  • LocationCheshire, UK

Posted 14 December 2012 - 09:04 AM


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 ?>

Edited by Barand, 14 December 2012 - 09:08 AM.

moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts
|baaSelect| generate js and php code for dynamic linked dropdowns

 


#10 MDCode

MDCode

    Advanced Member

  • Members
  • PipPipPip
  • 640 posts

Posted 14 December 2012 - 09:06 AM

That's why I said sounds :)

#11 wantabe2

wantabe2

    Advanced Member

  • Members
  • PipPipPip
  • 65 posts

Posted 14 December 2012 - 09:06 AM

still don't get it.... why isn't it returning the sql results in the form value?

#12 Barand

Barand

    Sen . ( ile || sei )

  • Gurus
  • 14,426 posts
  • LocationCheshire, UK

Posted 14 December 2012 - 10:58 AM

Is the file name ending with ".html" or ".php"

moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts
|baaSelect| generate js and php code for dynamic linked dropdowns

 


#13 wantabe2

wantabe2

    Advanced Member

  • Members
  • PipPipPip
  • 65 posts

Posted 14 December 2012 - 11:41 AM

It ends in .php

And I'm using php 5.3.5

Edited by wantabe2, 14 December 2012 - 11:44 AM.


#14 MDCode

MDCode

    Advanced Member

  • Members
  • PipPipPip
  • 640 posts

Posted 14 December 2012 - 11:58 AM

If your short tags setting is disabled you will need to follow barand's suggestion here:


Prior to that, if short tags disabled, you need

<?php echo $var ?>



#15 Muddy_Funster

Muddy_Funster

    Advanced Member

  • Members
  • PipPipPip
  • 2,993 posts

Posted 14 December 2012 - 12:00 PM

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, 14 December 2012 - 12:00 PM.

A simple password hash :

function makePass($word=''){
  $dbSalt = '$2a$07$'.substr(hash('whirlpool',$word),0,22);
  $dbPass = crypt($word, $dbSalt);
 return substr($dbPass,12);
}



My SQL/PHP Blog

#16 wantabe2

wantabe2

    Advanced Member

  • Members
  • PipPipPip
  • 65 posts

Posted 14 December 2012 - 01:28 PM

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

#17 wantabe2

wantabe2

    Advanced Member

  • Members
  • PipPipPip
  • 65 posts

Posted 14 December 2012 - 01:30 PM

Where would I add:
<?php echo $var ?>

in the code I've tried adding it several places & get random errors.

#18 wantabe2

wantabe2

    Advanced Member

  • Members
  • PipPipPip
  • 65 posts

Posted 14 December 2012 - 01:31 PM

Oh, never mind..I got it LOL
I'll give it a try thanks

#19 wantabe2

wantabe2

    Advanced Member

  • Members
  • PipPipPip
  • 65 posts

Posted 14 December 2012 - 02:33 PM

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?

#20 MDCode

MDCode

    Advanced Member

  • Members
  • PipPipPip
  • 640 posts

Posted 14 December 2012 - 03:12 PM

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.

Edited by SocialCloud, 14 December 2012 - 03:13 PM.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com