8 Replies - 271 Views - Last Post: 06 November 2010 - 03:11 PM Rate Topic: -----

#1 kinggeorge1987  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 12
  • View blog
  • Posts: 130
  • Joined: 22-September 10

mysql_fetch_array question

Posted 06 November 2010 - 12:36 AM

$newid=$_POST['product_id'];
$c="SELECT quota FROM packages WHERE vendorid= '3'";
$b=mysql_query($c) or die(mysql_error());
$count=mysql_num_rows($B)/>;
$val=mysql_fetch_array($B)/>;
	foreach($val as $q){
		echo $q;
	}



Hey everyone, this code is working to retrieve the quota, but it is returning it twice. There is only ever going to be one entry with that id, but i cant just exit after the first entry becuase I have to carry the value onto the next pages, any idea why it is returning the same value twice ?

Is This A Good Question/Topic? 0
  • +

Replies To: mysql_fetch_array question

#2 amitchhajer  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 78
  • Joined: 30-January 10

Re: mysql_fetch_array question

Posted 06 November 2010 - 12:48 AM

try using mysql_fetch_assoc also check the database again
Was This Post Helpful? 1
  • +
  • -

#3 kinggeorge1987  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 12
  • View blog
  • Posts: 130
  • Joined: 22-September 10

Re: mysql_fetch_array question

Posted 06 November 2010 - 12:54 AM

Worked great, thank you.

What is the difference between the two ?
Was This Post Helpful? 0
  • +
  • -

#4 no2pencil  Icon User is offline

  • Original Digital Gansta
  • member icon

Reputation: 4466
  • View blog
  • Posts: 24,916
  • Joined: 10-May 07

Re: mysql_fetch_array question

Posted 06 November 2010 - 01:02 AM

fetch_array just pulls the values back as you requested them. Say you pulled back id,password then id would be in array element zero & password would be in array element 1. By pulling them back with assoc, it pulls them back by string value.
Was This Post Helpful? 1
  • +
  • -

#5 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 2899
  • View blog
  • Posts: 7,555
  • Joined: 08-June 10

Re: mysql_fetch_array question

Posted 06 November 2010 - 01:08 AM

mysql_fetch_array() by default fetches the result set numerically and associatively indexed. you can change that with the appropriate flag or use (as mentioned) mysql_fetch_assoc()

This post has been edited by Dormilich: 06 November 2010 - 01:08 AM

Was This Post Helpful? 1
  • +
  • -

#6 kinggeorge1987  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 12
  • View blog
  • Posts: 130
  • Joined: 22-September 10

Re: mysql_fetch_array question

Posted 06 November 2010 - 03:31 AM

Thanks guys, very informative. Rep+++
Was This Post Helpful? 0
  • +
  • -

#7 amitchhajer  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 78
  • Joined: 30-January 10

Re: mysql_fetch_array question

Posted 06 November 2010 - 04:26 AM

i think you got the difference now :)
Was This Post Helpful? 1
  • +
  • -

#8 kinggeorge1987  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 12
  • View blog
  • Posts: 130
  • Joined: 22-September 10

Re: mysql_fetch_array question

Posted 06 November 2010 - 04:43 AM

Sure do, thanks again.
Was This Post Helpful? 0
  • +
  • -

#9 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 2899
  • View blog
  • Posts: 7,555
  • Joined: 08-June 10

Re: mysql_fetch_array question

Posted 06 November 2010 - 03:11 PM

I usually avoid such problems by using PDO, which allows me to use foreach()*.



* - foreach() works on the values, not on the keys.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1