Forum Moderators: coopster

Message Too Old, No Replies

Use mySql results as variables

Reuse records

         

cyclic

10:32 pm on Nov 14, 2002 (gmt 0)

10+ Year Member



This is probably simple, but I need a pointer. I want to pull integers from the db and then reuse them in another script. I'm happy with getting the results, it's just how to use them as variables in either a script or being placed back in a form field.

andreasfriedrich

10:58 pm on Nov 14, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I´m not too sure what your problem is. If you are wondering how to access individual fields of a record then that will depend on the mysql_fetch_* function you use. Have a look at their individual documentation on php.net.

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { 
printf ("ID: %s Name: %s", $row["id"], $row["name"]);
}

As you can see the individual fields may be accessed as a hash. You can do whatever you want with the $row array. To use them as values in a form just print the form and the corresponding value from the database whereever it goes.

echo '<input type="text" name="name" value="', $row['name'], '">';

Hope this helps.

Andreas

cyclic

11:04 pm on Nov 14, 2002 (gmt 0)

10+ Year Member



Thanks Andreas

Could you just explain the '%s'?

jatar_k

11:08 pm on Nov 14, 2002 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



from php.net on sprintf() [php.net]

This applies to both sprintf() and printf().

Each conversion specification consists of a percent sign (%), followed by one or more of these elements

A type specifier that says what type the argument data should be treated as. Possible types:

% - a literal percent character. No argument is required.
b - the argument is treated as an integer, and presented as a binary number.
c - the argument is treated as an integer, and presented as the character with that ASCII value.
d - the argument is treated as an integer, and presented as a (signed) decimal number.
u - the argument is treated as an integer, and presented as an unsigned decimal number.
f - the argument is treated as a float, and presented as a floating-point number.
o - the argument is treated as an integer, and presented as an octal number.
s - the argument is treated as and presented as a string.
x - the argument is treated as an integer and presented as a hexadecimal number (with lowercase letters).
X - the argument is treated as an integer and presented as a hexadecimal number (with uppercase letters).

cyclic

11:16 pm on Nov 14, 2002 (gmt 0)

10+ Year Member



Thanks guys - problem solved as per usual - brilliant!

andreasfriedrich

11:28 pm on Nov 14, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You might want to read up on sprintf. It is a very useful and efficient function to include lots of variables into a formatted string. You might want to use it to build your SQL strings.

Have a look at this post [webmasterworld.com] to see how sprintf() compares to concatenation and interpolation speedwise.

Andreas