jackmcnally Posted November 20, 2011 Share Posted November 20, 2011 Hi, I have a 'pre-release' sign up form, where people put in their email address and name, and it transmits to a MYSQL database. That's working fine. What I want to do is to have a piece of text that reads: (insert pre release amount of signer upperers here) have signed up. Will you? I'm guessing that the amount of rows on the table, would end up being the number. How do I do this? Thanks, Jack Quote Link to comment https://forums.phpfreaks.com/topic/251471-pull-amount-of-rows-in-mysql-table-and-display/ Share on other sites More sharing options...
Psycho Posted November 20, 2011 Share Posted November 20, 2011 SELECT COUNT(*) FROM table_name Quote Link to comment https://forums.phpfreaks.com/topic/251471-pull-amount-of-rows-in-mysql-table-and-display/#findComment-1289690 Share on other sites More sharing options...
jackmcnally Posted November 20, 2011 Author Share Posted November 20, 2011 Thanks for your answer. How would I connect this to a database? Where do I put the database name, and do I need to provide a cPanel username and password? Thanks, I'm a total PHP noob, but I'm learning... slowly! Quote Link to comment https://forums.phpfreaks.com/topic/251471-pull-amount-of-rows-in-mysql-table-and-display/#findComment-1289691 Share on other sites More sharing options...
Psycho Posted November 20, 2011 Share Posted November 20, 2011 [quote author=jackmcnally link=topic=348213.msg1643088#msg1643088 date=1321769606 I have a 'pre-release' sign up form, where people put in their email address and name, and it transmits to a MYSQL database. That's working fine. If you have what you say you do above, then you should already know how to do all that. Unless that is you didn't write the code for the sign up form. Quote Link to comment https://forums.phpfreaks.com/topic/251471-pull-amount-of-rows-in-mysql-table-and-display/#findComment-1289692 Share on other sites More sharing options...
jackmcnally Posted November 27, 2011 Author Share Posted November 27, 2011 Hi, Sorry for my very late reply, I have been busy! I did copy the script, but I have now wrote it myself. I have the form submitting to a PHP file (code shown below), that connects to the database and table and inputs the information provided. <?php header( 'Location: http://www.xxxxxx.com/yes.php' ) ; $con = mysql_connect("localhost","xxxxxx","xxxxxxx"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("xxxxx", $con); $sql="INSERT INTO benotified (email, firstname, lastname) VALUES ('$_POST[email]','$_POST[firstname]','$_POST[lastname]')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "1 record added"; mysql_close($con) ?> It works like a charm. I have tried using a modified version of it, including your function (as shown below), but Dreamweaver is telling me I have a syntax error. <?php $con = mysql_connect("localhost","xxxxx","xxxxx"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("xxxxx", $con); SELECT COUNT(*) FROM xxxxx mysql_close($con) ?> As previously stated, I'm a wee bit of a PHP noob, but I am making pretty good progress with some tutorials! I learnt how to echo and write some basic stuff today without assistance! Yay! Thanks for all the help, I really appreciate it! Quote Link to comment https://forums.phpfreaks.com/topic/251471-pull-amount-of-rows-in-mysql-table-and-display/#findComment-1291545 Share on other sites More sharing options...
Drongo_III Posted November 27, 2011 Share Posted November 27, 2011 Sounds like you want: $result = mysql_query("SELECT * FROM tablename", $con); $num_rows = mysql_num_rows($result); echo $num_rows . " " . "have signed up. Will you? "; Num rows counts how many rows of data you have in your specified table. http://uk.php.net/manual/en/function.mysql-num-rows.php Drongo Quote Link to comment https://forums.phpfreaks.com/topic/251471-pull-amount-of-rows-in-mysql-table-and-display/#findComment-1291560 Share on other sites More sharing options...
Pikachu2000 Posted November 27, 2011 Share Posted November 27, 2011 A COUNT() query is more efficient than selecting every record just to use mysql_num_rows(). Quote Link to comment https://forums.phpfreaks.com/topic/251471-pull-amount-of-rows-in-mysql-table-and-display/#findComment-1291605 Share on other sites More sharing options...
Psycho Posted November 27, 2011 Share Posted November 27, 2011 <?php $con = mysql_connect("localhost","xxxxx","xxxxx"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("xxxxx", $con); SELECT COUNT(*) FROM xxxxx mysql_close($con) ?> I gave you a query to run. You can't simple write a query into the PHP code and expect it to do anything. This is very basic stuff. Besides you shouldn't have different DB connection scripts. You should only have one that any page which needs DB access should include. $con = mysql_connect("localhost","xxxxx","xxxxx"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("xxxxx", $con); $query = "SELECT COUNT(*) FROM xxxxx"; $result = mysql_query($query); $count = mysql_result($result, 0); echo "{$count} have signed up. Will you? "; mysql_close($con) Quote Link to comment https://forums.phpfreaks.com/topic/251471-pull-amount-of-rows-in-mysql-table-and-display/#findComment-1291616 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.