Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums

Go Back   Code Forums > Application and Web Development > PHP

Reply
 
LinkBack Thread Tools Display Modes
Old 06-13-2007, 10:41 PM   #1 (permalink)
Salchester
Salchester
 
Salchester's Avatar
 
Join Date: Jul 2005
Location: In a house
Posts: 230
Salchester is an unknown quantity at this point
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

Quote:
$array[0] = "Joe";
Many Thanks,
__________________
Many Thanks, in advance!

Salchester.
The Future Is Here - Are You Ready?
Salchester is offline   Reply With Quote
Old 06-13-2007, 11:35 PM   #2 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,475
sde is on a distinguished road
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
sde is offline   Reply With Quote
Old 06-14-2007, 09:26 AM   #3 (permalink)
Salchester
Salchester
 
Salchester's Avatar
 
Join Date: Jul 2005
Location: In a house
Posts: 230
Salchester is an unknown quantity at this point
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?
Salchester is offline   Reply With Quote
Old 06-14-2007, 02:15 PM   #4 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,475
sde is on a distinguished road
look at your database connection and select logic in your first example.. then look at what your missing in what you just posted.
__________________
Mike
sde is offline   Reply With Quote
Old 06-15-2007, 12:11 PM   #5 (permalink)
Salchester
Salchester
 
Salchester's Avatar
 
Join Date: Jul 2005
Location: In a house
Posts: 230
Salchester is an unknown quantity at this point
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?
Salchester is offline   Reply With Quote
Old 06-15-2007, 12:24 PM   #6 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,475
sde is on a distinguished road
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
sde is offline   Reply With Quote
Old 06-15-2007, 12:29 PM   #7 (permalink)
Salchester
Salchester
 
Salchester's Avatar
 
Join Date: Jul 2005
Location: In a house
Posts: 230
Salchester is an unknown quantity at this point
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?
Salchester is offline   Reply With Quote
Old 06-15-2007, 01:01 PM   #8 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,475
sde is on a distinguished road
ok, .. as far as the VB goes.. i really dislike that language and have not worked with it in years.
__________________
Mike
sde is offline   Reply With Quote
Old 06-15-2007, 01:16 PM   #9 (permalink)
Salchester
Salchester
 
Salchester's Avatar
 
Join Date: Jul 2005
Location: In a house
Posts: 230
Salchester is an unknown quantity at this point
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?
Salchester is offline   Reply With Quote
Old 06-15-2007, 01:41 PM   #10 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,475
sde is on a distinguished road
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
sde is offline   Reply With Quote
Old 06-15-2007, 03:34 PM   #11 (permalink)
DJMaze
Senior Contributor
 
DJMaze's Avatar
 
Join Date: Mar 2005
Posts: 661
DJMaze is on a distinguished road
VB wasn't that an acronym for "Visual Bull****"?

P.S. Where did freewebs.com/comphelper go?
__________________

UT: Ultra-kill... God like!
DJMaze is offline   Reply With Quote
Old 06-15-2007, 10:28 PM   #12 (permalink)
Salchester
Salchester
 
Salchester's Avatar
 
Join Date: Jul 2005
Location: In a house
Posts: 230
Salchester is an unknown quantity at this point
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?
Salchester is offline   Reply With Quote
Old 06-16-2007, 12:30 PM   #13 (permalink)
Salchester
Salchester
 
Salchester's Avatar
 
Join Date: Jul 2005
Location: In a house
Posts: 230
Salchester is an unknown quantity at this point
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:

Code:
record(1).name
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?
Salchester is offline   Reply With Quote
Old 06-16-2007, 02:00 PM   #14 (permalink)
DJMaze
Senior Contributor
 
DJMaze's Avatar
 
Join Date: Mar 2005
Posts: 661
DJMaze is on a distinguished road
yes there is.

$record[1]['name']

or

$record[1]->name
__________________

UT: Ultra-kill... God like!
DJMaze is offline   Reply With Quote
Old 06-17-2007, 12:08 AM   #15 (permalink)
Salchester
Salchester
 
Salchester's Avatar
 
Join Date: Jul 2005
Location: In a house
Posts: 230
Salchester is an unknown quantity at this point
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:

Code:
$record[1]['name']
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?
Salchester is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Table data from db as new form input tomycon PHP 2 03-03-2006 05:26 AM
Input File Data Into Array in C++? wizardman Standard C, C++ 1 09-02-2005 11:15 PM
How to insert HTML form data into mysql database ? plomon PHP 5 02-06-2005 08:23 AM


All times are GMT -8. The time now is 07:12 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting