9 Replies - 511 Views - Last Post: 17 February 2011 - 12:58 PM Rate Topic: -----

#1 cgannon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 26-November 09

Storing values from a db while using an array in the query

Posted 17 February 2011 - 11:48 AM

Hi,

I hame selecting values from an database & everytime the next batch of values are selected they overwrite the last values. I am using an array in my query & I think that is what the problem is?
I have used print outs and as the loop goes around it prints out what I need but the next time it goes it will over write the previous values.
I have been at this for several days & can't make any progress.

Thanks
$y = 0 ;
		$x = 0 ;
		while($y < $amount )
		{
			$personaldetails= mysql_query ("SELECT  Name, Address, Contact_Number, Email
			FROM personal
			WHERE Personal_No = '$personalids[$y]' ") ;//getting users id who MATCH %100 the employers education match
			$personalid = $personaldetails ;
			$numofrows7 = mysql_num_rows($personaldetails) //getting the rows to be used
			if ($numofrows7!= 0 )
			{
				$personalid  = array($amount); //dynamically creating the size of the arrays
				$Name = array($amount) ;//values to store information
				$Address = array($amount) ;
				$Contactnumber = array($amount) ;
				$Email = array($amount) ;
				
				while ($row = mysql_fetch_assoc($personaldetails))
				{
					$y++ ;
					$personalid[$x] = $row['Personal_No'];//personal id of user who matches the job title
					$Name[$x] = $row['Name'] ;//getting personal details of the users who match.
					$Address[$x] = $row['Address'] ;
					$Contactnumber[$x] = $row['Contact_Number'] ;
					$Email[$x] = $row['Email'] ;
					//echo $Name[$x] ;
					echo $y ;
					$x++ ;	
					}
				}
				
			}


This post has been edited by Dormilich: 17 February 2011 - 01:09 PM
Reason for edit:: fixed code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Storing values from a db while using an array in the query

#2 VolcomMky  Icon User is offline

  • D.I.C Regular

Reputation: 74
  • View blog
  • Posts: 315
  • Joined: 13-May 09

Re: Storing values from a db while using an array in the query

Posted 17 February 2011 - 11:54 AM

$numofrows7 = mysql_num_rows($personaldetails)


Is missing a ; at the end, causes a error.

And

$personalid = array($amount); //dynamically creating the size of the arrays
$Name = array($amount) ;//values to store information
$Address = array($amount) ;
$Contactnumber = array($amount) ;
$Email = array($amount) ;


Wont create the size of the array, it will create the array with the first value being whatever $amount is.

For example $Name[0] would be whatever $amount is

This post has been edited by VolcomMky: 17 February 2011 - 11:57 AM

Was This Post Helpful? -1
  • +
  • -

#3 cgannon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 26-November 09

Re: Storing values from a db while using an array in the query

Posted 17 February 2011 - 12:02 PM

Hi Thanks for the post, I actually have a ; there just not showing up on the example... I have the arrays changed but problem still there, When I print out the values for the 1st set all the valuse are the size of the array but the next set are ok example Name dave age 10 etc.
Was This Post Helpful? 0
  • +
  • -

#4 VolcomMky  Icon User is offline

  • D.I.C Regular

Reputation: 74
  • View blog
  • Posts: 315
  • Joined: 13-May 09

Re: Storing values from a db while using an array in the query

Posted 17 February 2011 - 12:15 PM

Well your post was obviously messed up.. no reason to give a bad rep

Thats not all the code because there is nothing in there to display the arrays.

* REMOVED SOMETHING I MISSED *

Set your arrays up like this.
$personalid[$y] = array($amount);
$Name[$y] = array($amount) ;
$Address[$y] = array($amount) ;
$Contactnumber[$y] = array($amount) ;
$Email[$y] = array($amount) ;


And
$personalid[$y][$x] = $row['Personal_No'];
$Name[$y][$x] = $row['Name'] ;
$Address[$y][$x] = $row['Address'] ;
$Contactnumber[$y][$x] = $row['Contact_Number'] ;
$Email[$y][$x] = $row['Email'] ;



And give me my Rep back please....

This post has been edited by VolcomMky: 17 February 2011 - 12:39 PM

Was This Post Helpful? 1
  • +
  • -

#5 cgannon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 26-November 09

Re: Storing values from a db while using an array in the query

Posted 17 February 2011 - 12:30 PM

Thanks again sorry about the rep!

The while ( $y < $amount )is there to make sure it executes the right amount of times.

declaring the arrays like $Name[$x][$y] gives an error & I think it makes them a 2 deminsional arrays..
My problem I think is that while $x does increment it does not move the array up & so the values are over
written

while ($row = mysql_fetch_assoc($personaldetails))
					{
						
				$personalid[$x] = $row['Personal_No'];	/
				$Name[$x] = $row['Name'] ;		
				$Address[$x] = $row['Address'] ;
				$Contactnumber[$x] = $row['Contact_Number'] ;
				$Email[$x] = $row['Email'] ;
				//echo $Name[$x] ;
				//echo $y ;
				$x++ ;	
				$y++ ;
		}


if i print out echo "$Name[0]" ; it will print out the first value that should come out
but when it loops again it has been overwritten as if the $x++ didnt do anything
but if i print that out it shows that it is incrementing....

thanks again

This post has been edited by Dormilich: 17 February 2011 - 01:10 PM
Reason for edit:: please use [code] /* your code source here */ [/code] tags when posting code

Was This Post Helpful? 0
  • +
  • -

#6 VolcomMky  Icon User is offline

  • D.I.C Regular

Reputation: 74
  • View blog
  • Posts: 315
  • Joined: 13-May 09

Re: Storing values from a db while using an array in the query

Posted 17 February 2011 - 12:36 PM

How many times should it loop?

I noticed you have $y++ inside of the WHILE Loop, that is inside the top WHILE Loop


So before the First WHILE loop goes through once, it will add to the $y variable multiple times because its updated inside the inner WHILE Loop instead of inside of the OUTTER WHILE loop

Is it supposed to be that way?

Example being, while first loop (comparing $y to $amount), it updates the $y multiple times before the first loop finishes.

And yes, it would make the Arrays 2 dimensional, but you would want to swap the $y and $x, you set it to
$Name[$x][$y]

This post has been edited by VolcomMky: 17 February 2011 - 12:38 PM

Was This Post Helpful? 1
  • +
  • -

#7 cgannon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 26-November 09

Re: Storing values from a db while using an array in the query

Posted 17 February 2011 - 12:43 PM

Yea the $y was not meant to be there but the problem still remains...I had it in there in case that when it executed in while ($row = mysql_fetch_assoc($personaldetails))
the $y would ++ incase the '$personalids[$y]' was executed again.

For this example there are only 2 results being returned, one set of details to jane and one to john,
I can print out janes when they come in but when it loops to do johns it overwrites janes with nothing and
puts johns details into the second spots on the arrays? I don't know why it is overwriting janes details in
the first space in the arrays ie $Name[0] would be for jane & $Name[1] is fro johns...
Was This Post Helpful? 0
  • +
  • -

#8 VolcomMky  Icon User is offline

  • D.I.C Regular

Reputation: 74
  • View blog
  • Posts: 315
  • Joined: 13-May 09

Re: Storing values from a db while using an array in the query

Posted 17 February 2011 - 12:46 PM

Try moving

$personalid = array($amount); 
$Name = array($amount) ;
$Address = array($amount) ;
$Contactnumber = array($amount) ;
$Email = array($amount) ;


Outside of BOTH while loops, problem could be that they are re-declaring/restarting
Was This Post Helpful? 1
  • +
  • -

#9 cgannon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 26-November 09

Re: Storing values from a db while using an array in the query

Posted 17 February 2011 - 12:53 PM

That did it! Thanks alot, between today & Monday I'vve spent 7 hours on it! Cheers!
Was This Post Helpful? 0
  • +
  • -

#10 VolcomMky  Icon User is offline

  • D.I.C Regular

Reputation: 74
  • View blog
  • Posts: 315
  • Joined: 13-May 09

Re: Storing values from a db while using an array in the query

Posted 17 February 2011 - 12:58 PM

No problem, sorry for the run-around, but as alot of people now its all about trial and error if something doesn't work lol :bananaman:
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1