Page 1 of 1

arrays

Posted: Tue Dec 06, 2005 4:20 pm
by InnerShadow
i have this update statement:

Code: Select all

<?php

$conn = mysql_connect("localhost", "root", "") or die('Could not connect to the database: '.mysql_error()); 
mysql_select_db("countries", $conn) or die('Could not select the database: '.mysql_error()); 
$sql = "update baseadd set $resource = $amount where convert(country using utf8) = '$country' limit 1"; 
$res = mysql_query($sql) or die(mysql_error().'<p>SQL:<br />'.$sql.'</p>');


?>
I was wondering if there was any way to run three arrays at the same time or arrays embeded in eachother, so that i can have a country defined by one array, then for that country there will be a number or resources (put in place of $resource) and a specific amount that goes with each resource

i don't even know if arrays are the way to go with this. Any imput would be great.

Posted: Wed Dec 07, 2005 12:15 pm
by John Cartwright
To be honest, I have no idea what you asked.

Posted: Wed Dec 07, 2005 1:03 pm
by Luke
I have no IMPUT because I have no idea what you're talking about either.

Posted: Wed Dec 07, 2005 8:33 pm
by InnerShadow
oops, didn't see that....sry.

Umm, lets see....how about i just put out the long version and ask if there is any way to simplify it?

Code: Select all

<?php
	$conn = mysql_connect("localhost", "root", "") or die('Could not connect to the database: '.mysql_error()); 
	mysql_select_db("countries", $conn) or die('Could not select the database: '.mysql_error());
	$sql = "update forts set Forts = 3 where convert(country using utf8) = 'United States' limit 1";
	$res = mysql_query($sql) or die(mysql_error().'<p>SQL:<br />'.$sql.'</p>');

	$conn = mysql_connect("localhost", "root", "") or die('Could not connect to the database: '.mysql_error()); 
	mysql_select_db("countries", $conn) or die('Could not select the database: '.mysql_error());
	$sql = "update forts set Forts = 3 where convert(country using utf8) = 'Great Britain' limit 1";
	$res = mysql_query($sql) or die(mysql_error().'<p>SQL:<br />'.$sql.'</p>');

	$conn = mysql_connect("localhost", "root", "") or die('Could not connect to the database: '.mysql_error()); 
	mysql_select_db("countries", $conn) or die('Could not select the database: '.mysql_error());
	$sql = "update forts set Forts = 3 where convert(country using utf8) = 'France' limit 1";
	$res = mysql_query($sql) or die(mysql_error().'<p>SQL:<br />'.$sql.'</p>');

	$conn = mysql_connect("localhost", "root", "") or die('Could not connect to the database: '.mysql_error()); 
	mysql_select_db("countries", $conn) or die('Could not select the database: '.mysql_error());
	$sql = "update forts set Forts = 2 where convert(country using utf8) = 'Spain' limit 1";
	$res = mysql_query($sql) or die(mysql_error().'<p>SQL:<br />'.$sql.'</p>');

	$conn = mysql_connect("localhost", "root", "") or die('Could not connect to the database: '.mysql_error()); 
	mysql_select_db("countries", $conn) or die('Could not select the database: '.mysql_error());
	$sql = "update forts set Forts = 0 where convert(country using utf8) = 'Nicaragua' limit 1";
	$res = mysql_query($sql) or die(mysql_error().'<p>SQL:<br />'.$sql.'</p>');

	$conn = mysql_connect("localhost", "root", "") or die('Could not connect to the database: '.mysql_error()); 
	mysql_select_db("countries", $conn) or die('Could not select the database: '.mysql_error());
	$sql = "update forts set Forts = 0 where convert(country using utf8) = 'Hawaii' limit 1";
	$res = mysql_query($sql) or die(mysql_error().'<p>SQL:<br />'.$sql.'</p>');

	$conn = mysql_connect("localhost", "root", "") or die('Could not connect to the database: '.mysql_error()); 
	mysql_select_db("countries", $conn) or die('Could not select the database: '.mysql_error());
	$sql = "update forts set Forts = 0 where convert(country using utf8) = 'Panama' limit 1";
	$res = mysql_query($sql) or die(mysql_error().'<p>SQL:<br />'.$sql.'</p>');

	$conn = mysql_connect("localhost", "root", "") or die('Could not connect to the database: '.mysql_error()); 
	mysql_select_db("countries", $conn) or die('Could not select the database: '.mysql_error());
	$sql = "update forts set Forts = 0 where convert(country using utf8) = 'Jamaica' limit 1";
	$res = mysql_query($sql) or die(mysql_error().'<p>SQL:<br />'.$sql.'</p>');

	$conn = mysql_connect("localhost", "root", "") or die('Could not connect to the database: '.mysql_error()); 
	mysql_select_db("countries", $conn) or die('Could not select the database: '.mysql_error());
	$sql = "update forts set Forts = 0 where convert(country using utf8) = 'India' limit 1";
	$res = mysql_query($sql) or die(mysql_error().'<p>SQL:<br />'.$sql.'</p>');

	$conn = mysql_connect("localhost", "root", "") or die('Could not connect to the database: '.mysql_error()); 
	mysql_select_db("countries", $conn) or die('Could not select the database: '.mysql_error());
	$sql = "update forts set Forts = 0 where convert(country using utf8) = 'Sudan' limit 1";
	$res = mysql_query($sql) or die(mysql_error().'<p>SQL:<br />'.$sql.'</p>');

	$conn = mysql_connect("localhost", "root", "") or die('Could not connect to the database: '.mysql_error()); 
	mysql_select_db("countries", $conn) or die('Could not select the database: '.mysql_error());
	$sql = "update forts set Forts = 0 where convert(country using utf8) = 'Libya' limit 1";
	$res = mysql_query($sql) or die(mysql_error().'<p>SQL:<br />'.$sql.'</p>');

	$conn = mysql_connect("localhost", "root", "") or die('Could not connect to the database: '.mysql_error()); 
	mysql_select_db("countries", $conn) or die('Could not select the database: '.mysql_error());
	$sql = "update forts set Forts = 0 where convert(country using utf8) = 'Algeria' limit 1";
	$res = mysql_query($sql) or die(mysql_error().'<p>SQL:<br />'.$sql.'</p>');

	$conn = mysql_connect("localhost", "root", "") or die('Could not connect to the database: '.mysql_error()); 
	mysql_select_db("countries", $conn) or die('Could not select the database: '.mysql_error());
	$sql = "update forts set Forts = 0 where convert(country using utf8) = 'Vietnam' limit 1";
	$res = mysql_query($sql) or die(mysql_error().'<p>SQL:<br />'.$sql.'</p>');

	$conn = mysql_connect("localhost", "root", "") or die('Could not connect to the database: '.mysql_error()); 
	mysql_select_db("countries", $conn) or die('Could not select the database: '.mysql_error());
	$sql = "update forts set Forts = 0 where convert(country using utf8) = 'Costa Rica' limit 1";
	$res = mysql_query($sql) or die(mysql_error().'<p>SQL:<br />'.$sql.'</p>');

	$conn = mysql_connect("localhost", "root", "") or die('Could not connect to the database: '.mysql_error()); 
	mysql_select_db("countries", $conn) or die('Could not select the database: '.mysql_error());
	$sql = "update forts set Forts = 0 where convert(country using utf8) = 'Philippines' limit 1";
	$res = mysql_query($sql) or die(mysql_error().'<p>SQL:<br />'.$sql.'</p>');

	$conn = mysql_connect("localhost", "root", "") or die('Could not connect to the database: '.mysql_error()); 
	mysql_select_db("countries", $conn) or die('Could not select the database: '.mysql_error());
	$sql = "update forts set Forts = 0 where convert(country using utf8) = 'China' limit 1";
	$res = mysql_query($sql) or die(mysql_error().'<p>SQL:<br />'.$sql.'</p>');

	$conn = mysql_connect("localhost", "root", "") or die('Could not connect to the database: '.mysql_error()); 
	mysql_select_db("countries", $conn) or die('Could not select the database: '.mysql_error());
	$sql = "update forts set Forts = 0 where convert(country using utf8) = 'Puerto Rico' limit 1";
	$res = mysql_query($sql) or die(mysql_error().'<p>SQL:<br />'.$sql.'</p>');

	$conn = mysql_connect("localhost", "root", "") or die('Could not connect to the database: '.mysql_error()); 
	mysql_select_db("countries", $conn) or die('Could not select the database: '.mysql_error());
	$sql = "update forts set Forts = 0 where convert(country using utf8) = 'Cuba' limit 1";
	$res = mysql_query($sql) or die(mysql_error().'<p>SQL:<br />'.$sql.'</p>');

?>

Posted: Wed Dec 07, 2005 8:54 pm
by shiznatix
yes you can simplify it by taking out all of those mysql_connects and mysql_select_dbs

you only have to connect to mysql ONCE for each time you run a script, that means connect and select the db at the top of the script then run as many queries as you like.

Posted: Wed Dec 07, 2005 9:43 pm
by neophyte
What if you put all the contries in an array and then:

Code: Select all

$countries = array( array( 'forts' => 1,
                                          'country' => 'United States'),
                                 array('forts' => 2,
                                           'country' => 'France'));

$conn = mysql_connect("localhost", "root", "") or die('Could not connect to the database: '.mysql_error());
mysql_select_db("countries", $conn) or die('Could not select the database: '.mysql_error());
foreach ($countries as $key => $val){
   $forts = $val['forts'];
   $country = $val['country'];
    $sql = "update forts set Forts = '$forts' where convert(country using utf8) = '$country' limit 1";
    $res = mysql_query($sql) or die(mysql_error().'<p>SQL:<br />'.$sql.'</p>'); 
}
Not tested....

Posted: Fri Dec 09, 2005 8:50 am
by InnerShadow
so, would this be right for three variables instead of just 2?

Code: Select all

<?php
	$countries = array( array( 'resource' => array('Coal', 'Iron', 'Timber', 'Fruit', 'Vegetables', 'Cotton', 'Sugar', 'Tobacco', 'Silver', 'Gold'), 'amount' => array( 0, 0, 0, 10, 6, 0, 11, 0, 4, 5) , 'country' => 'United States',
			    array( 'resource' => array('Coal', 'Iron', 'Timber', 'Fruit', 'Vegetables', 'Cotton', 'Sugar', 'Tobacco', 'Silver', 'Gold'), 'amount' => array( 0, 0, 5, 8, 8, 6, 11, 4, 4, 4) , 'country' => 'Great Britain',
 			    array( 'resource' => array('Coal', 'Iron', 'Timber', 'Fruit', 'Vegetables', 'Cotton', 'Sugar', 'Tobacco', 'Silver', 'Gold'), 'amount' => array( 4, 5, 2, 10, 5, 6, 11, 7, 3, 4) , 'country' => 'France',
		    	    array( 'resource' => array('Coal', 'Iron', 'Timber', 'Fruit', 'Vegetables', 'Cotton', 'Sugar', 'Tobacco', 'Silver', 'Gold'), 'amount' => array( 6, 8, 4, 6, 5, 6, 8, 6, 2, 2) , 'country' => 'Spain',
		    	    array( 'resource' => array('Coal', 'Iron', 'Timber', 'Fruit', 'Vegetables', 'Cotton', 'Sugar', 'Tobacco', 'Silver', 'Gold'), 'amount' => array( 0, 0, 1, 4, 3, 0, 4, 1, 0, 1) , 'country' => 'Nicaragua',
	    	    	    array( 'resource' => array('Coal', 'Iron', 'Timber', 'Fruit', 'Vegetables', 'Cotton', 'Sugar', 'Tobacco', 'Silver', 'Gold'), 'amount' => array( 0, 0, 1, 7, 2, 0, 4, 0, 0, 0) , 'country' => 'Hawaii',
	    	    	    array( 'resource' => array('Coal', 'Iron', 'Timber', 'Fruit', 'Vegetables', 'Cotton', 'Sugar', 'Tobacco', 'Silver', 'Gold'), 'amount' => array( 0, 1, 1, 2, 3, 0, 7, 2, 2, 2) , 'country' => 'Panama',
			    array( 'resource' => array('Coal', 'Iron', 'Timber', 'Fruit', 'Vegetables', 'Cotton', 'Sugar', 'Tobacco', 'Silver', 'Gold'), 'amount' => array( 0, 0, 0, 2, 2, 2, 2, 1, 1, 0) , 'country' => 'Jamaica',
     	    	    	    array( 'resource' => array('Coal', 'Iron', 'Timber', 'Fruit', 'Vegetables', 'Cotton', 'Sugar', 'Tobacco', 'Silver', 'Gold'), 'amount' => array( 4, 2, 5, 0, 2, 4, 0, 0, 3, 0) , 'country' => 'India',
	    	    	    array( 'resource' => array('Coal', 'Iron', 'Timber', 'Fruit', 'Vegetables', 'Cotton', 'Sugar', 'Tobacco', 'Silver', 'Gold'), 'amount' => array( 1, 0, 0, 0, 0, 1, 1, 1, 2, 3) , 'country' => 'Sudan',
	    	    	    array( 'resource' => array('Coal', 'Iron', 'Timber', 'Fruit', 'Vegetables', 'Cotton', 'Sugar', 'Tobacco', 'Silver', 'Gold'), 'amount' => array( 0, 1, 0, 0, 0, 1, 1, 1, 1, 1) , 'country' => 'Libya',
	    	    	    array( 'resource' => array('Coal', 'Iron', 'Timber', 'Fruit', 'Vegetables', 'Cotton', 'Sugar', 'Tobacco', 'Silver', 'Gold'), 'amount' => array( 0, 0, 0, 0, 0, 2, 2, 2, 3, 2) , 'country' => 'Algeria',                        
	    	    	    array( 'resource' => array('Coal', 'Iron', 'Timber', 'Fruit', 'Vegetables', 'Cotton', 'Sugar', 'Tobacco', 'Silver', 'Gold'), 'amount' => array( 0, 0, 2, 2, 0, 1, 2, 1, 0, 0) , 'country' => 'Vietnam',
	    	    	    array( 'resource' => array('Coal', 'Iron', 'Timber', 'Fruit', 'Vegetables', 'Cotton', 'Sugar', 'Tobacco', 'Silver', 'Gold'), 'amount' => array( 0, 0, 0, 3, 1, 0, 3, 2, 0, 1) , 'country' => 'Costa Rica',
	    	    	    array( 'resource' => array('Coal', 'Iron', 'Timber', 'Fruit', 'Vegetables', 'Cotton', 'Sugar', 'Tobacco', 'Silver', 'Gold'), 'amount' => array( 2, 2, 3, 3, 2, 0, 5, 2, 0, 1) , 'country' => 'Philippines',
	    	    	    array( 'resource' => array('Coal', 'Iron', 'Timber', 'Fruit', 'Vegetables', 'Cotton', 'Sugar', 'Tobacco', 'Silver', 'Gold'), 'amount' => array( 2, 6, 3, 3, 2, 2, 5, 0, 0, 2) , 'country' => 'China',
	    	    	    array( 'resource' => array('Coal', 'Iron', 'Timber', 'Fruit', 'Vegetables', 'Cotton', 'Sugar', 'Tobacco', 'Silver', 'Gold'), 'amount' => array( 0, 0, 0, 3, 3, 2, 4, 1, 0, 0) , 'country' => 'Puerto Rico',
	    	    	    array( 'resource' => array('Coal', 'Iron', 'Timber', 'Fruit', 'Vegetables', 'Cotton', 'Sugar', 'Tobacco', 'Silver', 'Gold'), 'amount' => array( 0, 0, 2, 5, 2, 3, 4, 2, 0, 1) , 'country' => 'Cuba');



	$conn = mysql_connect("localhost", "root", "") or die('Could not connect to the database: '.mysql_error()); 
	mysql_select_db("countries", $conn) or die('Could not select the database: '.mysql_error()); 
	foreach ($countries as $key => $val){ 
	   $resource = $val['resource'];
	   $amount = $val['amount'] 
	   $country = $val['country']; 
	    $sql = "update baseadd set '$resource' = '$amount' where convert(country using utf8) = '$country' limit 1"; 
	    $res = mysql_query($sql) or die(mysql_error().'<p>SQL:<br />'.$sql.'</p>'); 
	}
?>