|
 |
|
 |
06-13-2007, 10:41 PM
|
#1 (permalink)
|
|
Salchester
Join Date: Jul 2005
Location: In a house
Posts: 230
|
Insert Table Data into Array
Hi Everyone,
I'm currently using the following code to insert information into a database using a form. How can all this information be selected from the table and inserted into an Array.
Code:
<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("WB", $con);
$sql="INSERT INTO Birds (Name, Species, Sex)
VALUES
('$_POST[Name]','$_POST[Species]','$_POST[Sex]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
mysql_close($con)
?>
Resulting in each record having its own "numeric array store" starting at 0
Many Thanks,
__________________
Many Thanks, in advance!
Salchester.
The Future Is Here - Are You Ready?
|
|
|
06-13-2007, 11:35 PM
|
#2 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,475
|
read the comments
PHP Code:
// your query $result = mysql_query("select * from Birds");
// use mysql_fetch_array to get a numerically indexed array while ($array = mysql_fetch_array($result)) { echo $array[0] . '<br />'; }
// alternatively, you could use mysql_fetch_assoc if you wanted // the array to be keyed by the field name while ($array = mysql_fetch_assoc($result)) { echo $array['Name'] . '<br />'; }
__________________
Mike
|
|
|
06-14-2007, 09:26 AM
|
#3 (permalink)
|
|
Salchester
Join Date: Jul 2005
Location: In a house
Posts: 230
|
Insert Table Data into Array
SDE,
Upon execution of the code you supplied, the following error messages appear. Why is this?
- Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in
C:\Documents and Settings\Dale Piper\Desktop\xampplite\htdocs\Array.php on line 13
- Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in
C:\Documents and Settings\Dale Piper\Desktop\xampplite\htdocs\Array.php on line 19
Code:
<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
// your query
$result = mysql_query("select * from Birds");
// use mysql_fetch_array to get a numerically indexed array
while ($array = mysql_fetch_array($result)) {
echo $array[0] . '<br />';
}
// alternatively, you could use mysql_fetch_assoc if you wanted
// the array to be keyed by the field name
while ($array = mysql_fetch_assoc($result)) {
echo $array['Name'] . '<br />';
}
?>
Many Thanks,
__________________
Many Thanks, in advance!
Salchester.
The Future Is Here - Are You Ready?
|
|
|
06-14-2007, 02:15 PM
|
#4 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,475
|
look at your database connection and select logic in your first example.. then look at what your missing in what you just posted.
__________________
Mike
|
|
|
06-15-2007, 12:11 PM
|
#5 (permalink)
|
|
Salchester
Join Date: Jul 2005
Location: In a house
Posts: 230
|
Insert Table Data into Array
SDE,
Cheers, I finally got your code working. Do you understand Visual Basic code/arrays?
How do I convert the following Visual Basic code into PHP code?
Code:
Private Type details
name As String
End Type
Dim record() As details
Private Sub Form_Load()
ReDim record(0)
End Sub
Private Sub lblNext_Click()
ReDim Preserve record(UBound(record) + 1)
record(UBound(record)).name = txtName.Text
MsgBox record(1).name
End Sub
Many Thanks,
__________________
Many Thanks, in advance!
Salchester.
The Future Is Here - Are You Ready?
|
|
|
06-15-2007, 12:24 PM
|
#6 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,475
|
salch, let me ask you.. do you feel you're learning anything here? .. or are you just getting code handed to you without trying to understand how it works.
__________________
Mike
|
|
|
06-15-2007, 12:29 PM
|
#7 (permalink)
|
|
Salchester
Join Date: Jul 2005
Location: In a house
Posts: 230
|
Insert Table Data into Array
SDE,
By all means do ask.
It may seem a strange way to learn, But i prefer to see the end result, then try and tear the code to pieces, to see how different aspects of the code works.
But obviously you need code for a starting point.
You can't learn from whats not there!!!
Many Thanks, hope this makes sense.
__________________
Many Thanks, in advance!
Salchester.
The Future Is Here - Are You Ready?
|
|
|
06-15-2007, 01:01 PM
|
#8 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,475
|
ok, .. as far as the VB goes.. i really dislike that language and have not worked with it in years.
__________________
Mike
|
|
|
06-15-2007, 01:16 PM
|
#9 (permalink)
|
|
Salchester
Join Date: Jul 2005
Location: In a house
Posts: 230
|
Insert Table Data into Array
SDE,
Do you remember anything about Visual Basic Arrays?
How do I create a "Record Set" array in PHP?
Where all record information is stored inside the array. Thats basic what the Visual Basic coded does.
Many Thanks,
__________________
Many Thanks, in advance!
Salchester.
The Future Is Here - Are You Ready?
|
|
|
06-15-2007, 01:41 PM
|
#10 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,475
|
Quote:
|
Where all record information is stored inside the array. Thats basic what the Visual Basic coded does
|
sounds like you're answering your own question. there's a nice array tutorial on this site in the PHP section if you need to learn about arrays.
if i were to help out with VB code, i'd have to use google a bit, .. and that's a little too much like work right now.
__________________
Mike
|
|
|
06-15-2007, 03:34 PM
|
#11 (permalink)
|
|
Senior Contributor
Join Date: Mar 2005
Posts: 661
|
VB wasn't that an acronym for "Visual Bull****"?
P.S. Where did freewebs.com/comphelper go?
__________________

UT: Ultra-kill... God like!
|
|
|
06-15-2007, 10:28 PM
|
#12 (permalink)
|
|
Salchester
Join Date: Jul 2005
Location: In a house
Posts: 230
|
Insert Table Data into Array
SDE,
The website located at www.freewebs.com/comphelper, was just a website I made at college. Nothing special really.
Many Thanks,
__________________
Many Thanks, in advance!
Salchester.
The Future Is Here - Are You Ready?
|
|
|
06-16-2007, 12:30 PM
|
#13 (permalink)
|
|
Salchester
Join Date: Jul 2005
Location: In a house
Posts: 230
|
Insert Table Data into Array
SDE/DJMAZE,
In PHP, is there anyway of using variables to store each piece of information (Name,Species,Sex). Then, when it comes to fetching the information, the array position (e.g. 0) is called along with the information type (Name etc).
A bit like the following:
The information is coming from the record array, at position 1 displaying names.
Many Thanks,
__________________
Many Thanks, in advance!
Salchester.
The Future Is Here - Are You Ready?
|
|
|
06-16-2007, 02:00 PM
|
#14 (permalink)
|
|
Senior Contributor
Join Date: Mar 2005
Posts: 661
|
yes there is.
$record[1]['name']
or
$record[1]->name
__________________

UT: Ultra-kill... God like!
|
|
|
06-17-2007, 12:08 AM
|
#15 (permalink)
|
|
Salchester
Join Date: Jul 2005
Location: In a house
Posts: 230
|
Insert Table Data into Array
DJMaze,
Brilliant. So that's used to display specific information within the array, but how do you put information into the array using variables, in order to use the the following code:
The following VB code uses an array that inherits the name, species, sex variables "record() as details" Each piece of information is called using the array position, an the variable it is going to be coming out of.
Code:
Private Type details
name As String
species As String
sex As String
End Type
Dim record() As details
Private Sub Form_Load()
ReDim record(0)
End Sub
Private Sub lblNext_Click()
ReDim Preserve record(UBound(record) + 1)
record(UBound(record)).name = txtName.Text
MsgBox record(1).name
End Sub
Many Thanks,
__________________
Many Thanks, in advance!
Salchester.
The Future Is Here - Are You Ready?
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 07:12 PM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|