←  Code Block

Fallout Studios Forums

»

[MySQL] "SELECT COUNT()"

Sgt. Rho's Photo Sgt. Rho 07 Mar 2008

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>
Quote

CodeCat's Photo CodeCat 07 Mar 2008

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.
Quote

Sgt. Rho's Photo Sgt. Rho 07 Mar 2008

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.
Quote

Slightly Wonky Robob's Photo Slightly Wonky Robob 07 Mar 2008

<? $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>
Quote

CodeCat's Photo CodeCat 07 Mar 2008

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.
Quote

Sgt. Rho's Photo Sgt. Rho 08 Mar 2008

	$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.
Quote