Jump to content


[MySQL] "SELECT COUNT()"


5 replies to this topic

#1 Sgt. Rho

    Kerbal Rocket Scientist

  • Project Leader
  • 6870 posts
  • Projects: Scaring Jebediah.

Posted 07 March 2008 - 10:06

I got a problem with that function. It either says "1", "10", or "Resource ID#2"....

<? $result1 = mysql_query&#40;&#34;SELECT COUNT&#40;*&#41; FROM users&#34;&#41;; ?>
<table border=0  class=middle>
 <tr>
  <td>
   Wir wir haben <b> <? echo $result1; ?> </b> Mitglieder.
  </td>
 </tr>
</table>


#2 CodeCat

    It's a trap!

  • Gold Member
  • 6111 posts

Posted 07 March 2008 - 11:54

That's because mysql_query doesn't return the results directly, but rather returns a 'resource' that you need to use for subsequent functions that access the data.

$result = mysql_query('whatever', $connection);

$array = mysql_fetch_assoc($result)
Returns one row of the result, as an associative array, with $array['column name'] = 'value';

$array = mysql_fetch_row($result)
Returns one row of the result, as a numeric array, with indexes ordered like the SELECT query. I.e. the first column in the SELECT is index 0, second is index 1, etc.
CodeCat

Posted Image
Posted Image

Go dtiomsaítear do chód gan earráidí, is go gcríochnaítear do chláir go réidh. -Old Irish proverb

#3 Sgt. Rho

    Kerbal Rocket Scientist

  • Project Leader
  • 6870 posts
  • Projects: Scaring Jebediah.

Posted 07 March 2008 - 19:11

So I'd need to change it to this?:

<? $result1 = mysql_query&#40;&#34;SELECT * FROM users&#34;&#41;; 
			  $nousers = mysql_fetch_assoc&#40;$result1&#91;&#39;ID&#39;&#93;&#41;; ?>
<table border=0  class=middle>
<tr>
  <td>
   Wir wir haben <b> <? echo $nousers&#59; ?> </b> Mitglieder.
  </td>
</tr>
</table>

Edited by Master_Chief, 07 March 2008 - 19:12.


#4 Slightly Wonky Robob

    Not a Wonky Gent.

  • Administrator
  • 9333 posts

Posted 07 March 2008 - 23:07

<? $query = mysql_query&#40;&#34;SELECT COUNT&#40;*&#41; FROM users&#34;&#41;; 
$entry= mysql_fetch_array&#40;$query&#41;
?>
<table border=0  class=middle>
<tr>
  <td>
   Wir wir haben <b> <? echo $entry&#91;0&#93;; ?> </b> Mitglieder.
  </td>
</tr>
</table>

Posted Image
F O R T H E N S
Posted Image

#5 CodeCat

    It's a trap!

  • Gold Member
  • 6111 posts

Posted 07 March 2008 - 23:47

Bob is right. If you do it your way, you're technically trying to echo an entire array. All that will do is print the word 'Array' on the screen.
CodeCat

Posted Image
Posted Image

Go dtiomsaítear do chód gan earráidí, is go gcríochnaítear do chláir go réidh. -Old Irish proverb

#6 Sgt. Rho

    Kerbal Rocket Scientist

  • Project Leader
  • 6870 posts
  • Projects: Scaring Jebediah.

Posted 08 March 2008 - 23:08

	$make = $_GET&#91;&#39;make&#39;&#93;;
													   
   if &#40;$make == &#34;upgroups&#34;&#41; {
   
   $Name = $_POST&#91;&#39;Name&#39;&#93;;
   $ID = $_POST&#91;&#39;ID&#39;&#93;;

   echo  $Name.&#34;<br>&#34;.$ID;
	include&#40;&#39;inc/acces.inc.php&#39;&#41;;
	mysql_connect&#40;$sqlserv, $sqluser, $sqlpass&#41;;
   mysql_select_db&#40;&#34;usr_web23_3&#34;&#41;;
	$sql = &#34;UPDATE groups SET Name=&#39;$Name&#39; WHERE &#39;GID&#39; = &#39;$ID&#39;&#34;;
	if &#40;mysql_query&#40;$sql&#41;&#41; { echo &#34;geht&#34;; } else { echo &#34;geht nicht&#34; . mysql_error&#40;&#41; . &#34;<br>&#34;; }


I got another problem...this script won't work :P

EDIT: It misteriously works suddenly O.o

Edited by Master_Chief, 09 March 2008 - 10:53.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users