473,513 Members | 11,702 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

assign db fields to variables with dynamically built var names

142 New Member
Hi,

I need to store database data in variables so I can work with it.
Please se below what I am trying to do in my script....

If you echo the stuff out is work fine. Meaning if I go echo $row['a5]; I get the data I want but I can't get the data to store in a vairable.

I need something that goes from $a1 to $a18 and in each of then store the data from one field in the database name a5.

Thanks

[PHP]
<?php
$con = mysql_connect("localhost","*****","******");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("*******", $con);

$result = mysql_query("SELECT * FROM hole ORDER BY a5");

while($row = mysql_fetch_array($result))
{
$a1 = $row['a5'];
$a2 = $row['a5']; /////// needs to give second result
$a3 = $row['a5']; /////// needs to give third result

}

echo "<img name=\"Hole\" src=\"$a1.jpg\" width=\"32\" height=\"32\" alt=\"Hole\">";



mysql_close($con);


?>
[/PHP]
Mar 15 '08 #1
16 1970
ronverdonk
4,258 Recognized Expert Specialist
Please remember to provide a meaningful Title for any threads started (see the FAQ entry Use a Good Thread Title).

This helps to ensure that other members, and also the general public, will have a better chance of finding answers to any similar questions.

MODERATOR
Mar 15 '08 #2
Markus
6,050 Recognized Expert Expert
Hi,

I need to store database data in variables so I can work with it.
Please se below what I am trying to do in my script....

If you echo the stuff out is work fine. Meaning if I go echo $row['a5]; I get the data I want but I can't get the data to store in a vairable.

I need something that goes from $a1 to $a18 and in each of then store the data from one field in the database name a5.

Thanks

[PHP]
<?php
$con = mysql_connect("localhost","*****","******");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("*******", $con);

$result = mysql_query("SELECT * FROM hole ORDER BY a5");

while($row = mysql_fetch_array($result))
{
$a1 = $row['a5'];
$a2 = $row['a5']; /////// needs to give second result
$a3 = $row['a5']; /////// needs to give third result

}

echo "<img name=\"Hole\" src=\"$a1.jpg\" width=\"32\" height=\"32\" alt=\"Hole\">";



mysql_close($con);


?>
[/PHP]
This is untested (just some logic)
[php]
$_i = 0;
$_a = array();
while($_row = mysql_fetch_array($_res))
{
$_a[$_i] = $_row['col'];
++$_i;
}
[/php]
Mar 15 '08 #3
ronverdonk
4,258 Recognized Expert Specialist
Your question is so not clear!

Do you want

1. to assign the content of column 'a5' in each subsequent row into a variable $a1

2. store the contents of column 'a5' of each row into 18 different variables starting with $a1-$a18?

3. store the content of column 'a5' in variables $a1-$a18, e.g. column 'a5' content from row 1 goes into $a1, from row 10 goes into $a10, from row 18 goes into $a18.

Please describe your problem a bit more clearer.

Ronald
Mar 15 '08 #4
webandwe
142 New Member
Your question is so not clear!

Do you want

1. to assign the content of column 'a5' in each subsequent row into a variable $a1

2. store the contents of column 'a5' of each row into 18 different variables starting with $a1-$a18?

3. store the content of column 'a5' in variables $a1-$a18, e.g. column 'a5' content from row 1 goes into $a1, from row 10 goes into $a10, from row 18 goes into $a18.

Please describe your problem a bit more clearer.

Ronald

Hi yes.

Number 3.

3. store the content of column 'a5' in variables $a1-$a18, e.g. column 'a5' content from row 1 goes into $a1, from row 10 goes into $a10, from row 18 goes into $a18.

Please describe your problem a bit more clearer.
Mar 15 '08 #5
Markus
6,050 Recognized Expert Expert
Hi yes.

Number 3.

3. store the content of column 'a5' in variables $a1-$a18, e.g. column 'a5' content from row 1 goes into $a1, from row 10 goes into $a10, from row 18 goes into $a18.

Please describe your problem a bit more clearer.
Then my post should suffice.
Mar 15 '08 #6
webandwe
142 New Member
I am testing it but for some reason it does not want to go...
Mar 15 '08 #7
Markus
6,050 Recognized Expert Expert
I am testing it but for some reason it does not want to go...
Post what youre using
Mar 15 '08 #8
webandwe
142 New Member
[PHP]
<?php
$con = mysql_connect("l*****","*****","*****");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("****datebase*****", $con);

$result = mysql_query("SELECT * FROM hole ORDER BY a5");

$i = 0;
$a = array();
while($row = mysql_fetch_array($result))
{
$a[$i] = $row['a5'];
++$i;
}

mysql_close($con);
////Testing
echo "$a1 $a2 $a3 ";

?>
[/PHP]
Mar 15 '08 #9
Markus
6,050 Recognized Expert Expert
[PHP]
<?php
$con = mysql_connect("l*****","*****","*****");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("****datebase*****", $con);

$result = mysql_query("SELECT * FROM hole ORDER BY a5");

$i = 0;
$a = array();
while($row = mysql_fetch_array($result))
{
$a[$i] = $row['a5'];
++$i;
}

mysql_close($con);
////Testing
echo "$a1 $a2 $a3 ";

?>
[/PHP]
Remember, the variables are being assigned into an array - not seperate variables.
[php]
echo "{$_a[0]} {$_a[1]} {$_a[2]}";
[/php]
Mar 15 '08 #10
webandwe
142 New Member
Super, it is working.!!!!!!!!!!!!!!

Never worked with it before but now i'll know!

Thanks a mil again!
Mar 15 '08 #11
ronverdonk
4,258 Recognized Expert Specialist
Remember, the variables are being assigned into an array - not seperate variables.
[php]
echo "{$_a[0]} {$_a[1]} {$_a[2]}";
[/php]
That is cheating: the requirement was to assign them to separate variables $a1 - $a18. You can do that as follows:
[php]while($row = mysql_fetch_array($result))
{
$var='a'.$i;
$$var = $row['a5'];
++$i;
}
mysql_close($con);
////Testing
echo "$a1 $a2 $a3 ";[/php]Ronald
Mar 15 '08 #12
Markus
6,050 Recognized Expert Expert
Super, it is working.!!!!!!!!!!!!!!

Never worked with it before but now i'll know!

Thanks a mil again!
Welcome!
Regards.
:)
Mar 15 '08 #13
webandwe
142 New Member
That is cheating: the requirement was to assign them to separate variables $a1 - $a18. You can do that as follows:
[php]while($row = mysql_fetch_array($result))
{
$var='a'.$i;
$$var = $row['a5'];
++$i;
}
mysql_close($con);
////Testing
echo "$a1 $a2 $a3 ";[/php]Ronald

This one work the best for me because I need to use the Vairables all over the place. Thanks!!!!!
Mar 15 '08 #14
ronverdonk
4,258 Recognized Expert Specialist
As Markus already said: you are welcome!. See ya again.

Ronald
Mar 15 '08 #15
Markus
6,050 Recognized Expert Expert
As Markus already said: you are welcome!. See ya again.

Ronald
Thanks for that ron!
I knew there was a way to name vars but didn't know how.

:D
Mar 15 '08 #16
ronverdonk
4,258 Recognized Expert Specialist
We can learn something each day from each other.

Ronald
Mar 15 '08 #17

Sign in to post your reply or Sign up for a free account.

Similar topics

8
6097
by: Corey Lubin | last post by:
someGlobal=1 class Original: def foo(self): # Make use of someGlobal from original import * someGlobal=2
4
8979
by: Terry | last post by:
I have a number of input boxes used to display totals based on selected items for each row in a table. There are more than a few rows that are identical, except for the form field name. I have...
4
12540
by: Eric | last post by:
How can I dynamically assign an event to an element? I have tried : (myelement is a text input) document.getElementById('myelement').onKeyUp = "myfnc(param1,param2,param3)"; ...
13
2289
by: Shelby | last post by:
Hi, if I have : rs.Fields("firstname").Value = "John" rs.Fields("middlename").Value = "D" rs.Fields("lastname").Value = "Paul" Is it possible to Dim the column name and make it a variable and...
5
2934
by: Sakcee | last post by:
python provides a great way of dynamically creating fuctions calls and class names from string a function/class name can be stored as string and called/initilzed e.g def foo(a,b): return...
9
5677
by: sean.scanlon | last post by:
can someone help understand how i can could access a struct field dymanically like: foo->fields ? when i try to compile this i get the following error: 'struct pwd' has no member named 'fields'...
1
7478
by: vega80 | last post by:
Hi. I have a problem with assigning an onkeypress-function to dynamically created input-boxes.I want to put the content of an input-field into a tag-list when the user hits enter. This works...
6
1806
by: likhin M | last post by:
Hi everyone, I am developing a webpage using only javascript.And I am dynamically creating buttons inside javascript as follows document.write('<button name=""...
4
2187
by: .Net Sports | last post by:
I need to dynamically assign a datalist attribute upon a helper function receiving data from a querystring. If a user picks a certain region, i need the datalist to display its back color, or any...
0
7254
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7153
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7432
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7519
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5677
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4743
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3230
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3218
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1585
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.