473,699 Members | 2,226 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("SE LECT * FROM hole ORDER BY a5");

while($row = mysql_fetch_arr ay($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($co n);


?>
[/PHP]
Mar 15 '08
16 1986
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_arr ay($result))
{
$var='a'.$i;
$$var = $row['a5'];
++$i;
}
mysql_close($co n);
////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_arr ay($result))
{
$var='a'.$i;
$$var = $row['a5'];
++$i;
}
mysql_close($co n);
////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
6104
by: Corey Lubin | last post by:
someGlobal=1 class Original: def foo(self): # Make use of someGlobal from original import * someGlobal=2
4
8991
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 added a sample of that below (there would be many more rows). I'm wondering if there is a way to dynamically generate the variable names (ie: T1val, T2val, etc.) in my function 'calc', based on the argument 'regnum' that I pass it. The problem...
4
12573
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)"; document.getElementById('myelement') = new Function("myfnc(param1,param2,param3)");
13
2304
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 reassign the value to this new variable? Because I have a long list of the above, and I wish to loop thru it, instead of assigning it one by one.
5
2943
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 a+b
9
5706
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' is there a way to treat fields as the member name of the struct?
1
7493
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 fine the first time (when the input-field is created in a non-dynamical way). The next input-field is created dynamically by a function that is called when the user hits enter (the previously generated input-field will be hidden). Then I'm trying...
6
1833
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="" onclick="">button1</button>'); For each button I am creating , I have to use different names.I have tried by using a javascript string variable to hold the name of the button, and using it in the name attribue of the button .But this is not working.How can I assign...
4
2198
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 of its other objects , as a certain color or css style <HeaderStyle BackColor="<%= ssponcont %>"</HeaderStyle> ....doesnot work as a Parser error renders: Content ('<HeaderStyle
0
8618
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9178
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8916
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7752
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6534
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5875
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4631
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3058
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 we have to send another system
3
2010
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.