[MySQL] "SELECT COUNT()"
Sgt. Rho 07 Mar 2008
I got a problem with that function. It either says "1", "10", or "Resource ID#2"....
<? $result1 = mysql_query("SELECT COUNT(*) FROM users"); ?> <table border=0 class=middle> <tr> <td> Wir wir haben <b> <? echo $result1; ?> </b> Mitglieder. </td> </tr> </table>
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.
$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.
Sgt. Rho 07 Mar 2008
So I'd need to change it to this?:
Edited by Master_Chief, 07 March 2008 - 19:12.
<? $result1 = mysql_query("SELECT * FROM users"); $nousers = mysql_fetch_assoc($result1['ID']); ?> <table border=0 class=middle> <tr> <td> Wir wir haben <b> <? echo $nousers; ?> </b> Mitglieder. </td> </tr> </table>
Edited by Master_Chief, 07 March 2008 - 19:12.
Slightly Wonky Robob 07 Mar 2008
<? $query = mysql_query("SELECT COUNT(*) FROM users"); $entry= mysql_fetch_array($query) ?> <table border=0 class=middle> <tr> <td> Wir wir haben <b> <? echo $entry[0]; ?> </b> Mitglieder. </td> </tr> </table>
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.
Sgt. Rho 08 Mar 2008
$make = $_GET['make']; if ($make == "upgroups") { $Name = $_POST['Name']; $ID = $_POST['ID']; echo $Name."<br>".$ID; include('inc/acces.inc.php'); mysql_connect($sqlserv, $sqluser, $sqlpass); mysql_select_db("usr_web23_3"); $sql = "UPDATE groups SET Name='$Name' WHERE 'GID' = '$ID'"; if (mysql_query($sql)) { echo "geht"; } else { echo "geht nicht" . mysql_error() . "<br>"; }
I got another problem...this script won't work
EDIT: It misteriously works suddenly O.o
Edited by Master_Chief, 09 March 2008 - 10:53.