Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
|
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 27th, 2004, 06:37 PM
Authorized User
 
Join Date: Aug 2003
Posts: 37
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Tachyon
Default trouble creating array

I'm getting a result set from a MySQL query but I'm having trouble dropping that into an array.

Code:
    $countryquery = "SELECT * FROM count_list";
    $countryresult = mysql_query($countryquery) or die(mysql_error());

while ($querydata = mysql_fetch_array($countryresult)){
    extract($querydata);
    $c[] = array ($count_name);
}
foreach ($c as $cp){
    echo ("$cp<br>");
}
This gets me "Array" printed many times.

When I try this:
Code:
while ($querydata = mysql_fetch_array($countryresult)){
    extract($querydata);
    $c = array ($count_name);
}
foreach ($c as $cp){
    echo ("$cp<br>");
}
I get the last item in the array displayed but nothing else. I'm assuming PHP is copying over the same variable again and again until it hits the last value and displays that on the screen.

Can anyone offer suggestions on how to change this code to create the array I need?

__________________
An overworked Web Developer who\'s expected to know everything yet given time to study nothing.
 
Old June 27th, 2004, 07:53 PM
Authorized User
 
Join Date: Jun 2003
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Try this:

$countryquery = mysql_query("SELECT * FROM count_list");
while ($allcountries = mysql_fetch_array($countryquery))
{
echo $allcountries['country'];
}

assuming you have a field named ['country'] you would like to echo out. If not just change ['country'] to the field you would like to echo out, like ['nameoffieldoutofdb'].

Hope this helps
Sami
 
Old June 27th, 2004, 11:34 PM
Authorized User
 
Join Date: Aug 2003
Posts: 37
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Tachyon
Default

Thanks for the tip. Now I've found a way to print the information I want from the result set on my screen but what I'd really like to do is get the info from the result set and read it into an array. Then I can use the array several time in the program.

My result set gets count_num and count_name. I just want to put all the count_name values into an array. From there I'm good to go.

 
Old June 28th, 2004, 11:25 AM
Authorized User
 
Join Date: Aug 2003
Posts: 37
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Tachyon
Default

Woo hoo! I found the answer.

Quote:
quote:<?php
while ($querydata = mysql_fetch_array($countryresult)){
    extract($querydata);
    $temp1[] = $count_name;
    $temp2 = array("Canada", "USA");
    $c = array_diff($temp1, $temp2);
}
foreach ($c as $cp){
    echo ("$cp<br>");
}
?>
This grabs the list of countries out of my database, knocks out U.S. and Canada and makes an array I can use for listing "other countries" when people are filling out their addresses.






Similar Threads
Thread Thread Starter Forum Replies Last Post
creating array hiruni Beginning VB 6 5 February 9th, 2006 03:22 AM
trouble displaying data from array Arsi C# 1 October 22nd, 2004 04:13 PM
Trouble creating new asp page umzi Classic ASP Databases 7 July 11th, 2004 05:20 AM
Trouble creating a Copy Record command on a Form Ron V Access 0 May 19th, 2004 01:36 PM
trouble creating package for project omb Pro VB 6 4 August 14th, 2003 08:38 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.