Member Avatar

Ok, so here is the problem. Below is a fairly simple code that will count the number of members registered to a site. The html works just fine but the php doesn't display anything unless I do something like if($result>0)--- then it will display everything to the right of the zero. If I use the code below, I get nothing. I'm hoping it's some stupid mistake instead of XAMPP not working correctly (the same thing happened on another page I tried to see using XAMPP).

<!DOCTYPE HTML>
<html>
<head>
<script src="javascript/login.js"></script>
<script src="javascript/registerscript.js"></script>
<script src="javascript/unsubscribe.js"></script>
<script src="javascript/password.js"></script>
</head>
<body>
<?php require('connect/registerdb.php');

$result=mysql_query("SELECT COUNT(*) FROM registration") or DIE(mysql_error());

while ($row=mysql_num_rows($result)){
echo 'There are . $result . 'members';}
?>
<form  id="login" action="session/session.php" onsubmit="return login();" method="POST">
<table>
<tr><td>Login Id</td><td><input type="text" name="loginid" id="loginid"/></td></tr>
<tr><td>Password</td><td><input type="password" name="password" id="password"/></td></tr>
<tr><td></td><td><input type="submit" value="Log me in" /></td></tr>
</table>
</form>

<table>

<tr><td></td><td><input type="submit" value="Unsubscribe" onClick="unsub()"/></td></tr>
<tr><td></td><td><input type="submit" value="I forgot my password" onClick="passret()"/></td></tr>
<tr><td></td><td><input type="button" value="I need to register" onClick="loca()"/></td></tr>
<tr><td></td><td><input type="submit" value="Do math" onClick="math.html"/></td></tr>
</table>

</body>
</html>

Recommended Answers

Sounds like Apache isn't running or your php page is outside the localhost. Check the XAMPP control box to see if both Apache and MySQL are running.

Jump to Post

All 5 Replies

well although you are still using mysql
you could do something like

$query = "SELECT COUNT(id) FROM members"; 

$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_array($result)){
    echo "There are ". $row['COUNT(id)'] ." members.";
    echo "<br />";
}

Change this:

echo 'There are . $result . 'members';}

to this:

echo 'There are '.$result.' members.';}

Check out this PHP doc on string operators: http://www.php.net/manual/en/language.operators.string.php, specifically concatenation.

If that's not the issue you could change your select statement and try something like this:

<?php

$result = mysql_query("SELECT COUNT(*) as count FROM registration") or DIE(mysql_error());

if($result['count'] > 0){
    echo 'There are . $result . 'members';
}else{
    echo 'There are no registered members yet!';
}

?>

(Note: untested, probably won't work as-is!)

If that doesn't work let me know and I'll have another look through.

Michael

Member Avatar

Sounds like Apache isn't running or your php page is outside the localhost. Check the XAMPP control box to see if both Apache and MySQL are running.

String concatenation error on line 15 inside while loop.
echo 'There are . $result . 'members';}

Try this:

$result=mysql_query("SELECT COUNT(*) AS total FROM registration") or DIE(mysql_error());

$row = mysql_fetch_row($result);

if($row[0]['total'] > 0) {
    echo 'There are ' . $row[0]['total'] . ' members.';
}

It should work (not tested).

Member Avatar

All great suggestions but it was a problem with XAMPP. It works fine today, with a small change to the code. Thanks for all the advice, it made me notice how little I know about arrays.

<!DOCTYPE HTML> <html> <head> <script src="javascript/login.js"></script> <script src="javascript/registerscript.js"></script> <script src="javascript/unsubscribe.js"></script> <script src="javascript/password.js"></script> </head> <body> <?php 
require('connect/registerdb.php');

$result=mysql_query("SELECT COUNT(name) FROM registration") or DIE(mysql_error());
$row=mysql_num_rows($result);

echo 'There are ' . $row . ' members';

?> <form  id="login" action="session/session.php" onsubmit="return login();" method="POST"> <table> <tr><td>Login Id</td><td><input type="text" name="loginid" id="loginid"/></td></tr> <tr><td>Password</td><td><input type="password" name="password" id="password"/></td></tr> <tr><td></td><td><input type="submit" value="Log me in" /></td></tr> </table> </form> <table> <tr><td></td><td><input type="submit" value="Unsubscribe" onClick="unsub()"/></td></tr> <tr><td></td><td><input type="submit" value="I forgot my password" onClick="passret()"/></td></tr> <tr><td></td><td><input type="button" value="I need to register" onClick="loca()"/></td></tr> <tr><td></td><td><input type="submit" value="Do math" onClick="math.html"/></td></tr> </table> </body> </html>