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 - 06: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.

  • Staff Alumni
  • 8,982 posts
  • LocationDallas, TX
  • Age:26

Posted 13 December 2012 - 06: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 - 08: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
  • 645 posts

Posted 13 December 2012 - 09: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.

  • Staff Alumni
  • 8,982 posts
  • LocationDallas, TX
  • Age:26

Posted 13 December 2012 - 09: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 )

  • Moderators
  • 16,931 posts
  • LocationCheshire, UK

Posted 13 December 2012 - 09: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 - 09:29 PM.

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.







moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#7 wantabe2

wantabe2

    Advanced Member

  • Members
  • PipPipPip
  • 65 posts

Posted 14 December 2012 - 01:16 PM

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
  • 645 posts

Posted 14 December 2012 - 01:18 PM

Sounds like you don't have short tags enabled.

#9 Barand

Barand

    Sen . ( ile || sei )

  • Moderators
  • 16,931 posts
  • LocationCheshire, UK

Posted 14 December 2012 - 02:04 PM


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 - 02:08 PM.

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.







moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#10 MDCode

MDCode

    Advanced Member

  • Members
  • PipPipPip
  • 645 posts

Posted 14 December 2012 - 02:06 PM

That's why I said sounds :)

#11 wantabe2

wantabe2

    Advanced Member

  • Members
  • PipPipPip
  • 65 posts

Posted 14 December 2012 - 02:06 PM

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

#12 Barand

Barand

    Sen . ( ile || sei )

  • Moderators
  • 16,931 posts
  • LocationCheshire, UK

Posted 14 December 2012 - 03:58 PM

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

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.







moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#13 wantabe2

wantabe2

    Advanced Member

  • Members
  • PipPipPip
  • 65 posts

Posted 14 December 2012 - 04:41 PM

It ends in .php

And I'm using php 5.3.5

Edited by wantabe2, 14 December 2012 - 04:44 PM.


#14 MDCode

MDCode

    Advanced Member

  • Members
  • PipPipPip
  • 645 posts

Posted 14 December 2012 - 04:58 PM

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
  • 3,271 posts

Posted 14 December 2012 - 05: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 - 05:00 PM.

Please: "This doesn't work..." is not a question.  We're not the government: we don't have anything to view what your doing on your computer.  Help us help you by asking a good question.

 

"Things needed to ask a "good" question:

  • A description of the context of your issue (Optional but can be a huge help in certain circumstances)
  • What is the actual problem (Mandatory)
  • What does the code actually do (Mandatory)
  • What you think the code should do / What you want the code to do (Mandatory)
  • What things have you tried so far (Optional, but missing it out just wastes your time and ours)
  • The actual code as you are running it - minus any personal information like Database Login Credentials (Mandatory - don't just post pseudo, the vast majority of issues are syntax and not logic)
  • As much info about your development environment as you can give - even if it's just letting us know you are using a hosting provider instead of a local install (Optional, but some questions can not be answered without it.)

 

 

My SQL/PHP Blog


#16 wantabe2

wantabe2

    Advanced Member

  • Members
  • PipPipPip
  • 65 posts

Posted 14 December 2012 - 06: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 - 06: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 - 06: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 - 07: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
  • 645 posts

Posted 14 December 2012 - 08: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 - 08:13 PM.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users