Jump to content

Totaling Numbers In The Database


Faracus
 Share

Recommended Posts

I am wondering how I can total numbers that are in the database up.The numbers are 1-77, and I was thinking that I could do something like

$result = mysql_query(\"SELECT * FROM numbers\");while ($row = mysql_fetch_array($result)){$total = $row[\'number\']+;}

would that work or am I way off?EDIT: Just thinking I could declare them separately, by including the where variable, such as

$number1 = mysql_query(\"SELECT 1 FROM numbers\")$number2 = mysql_query(\"SELECT 2 FROM numbers\")and on and on till all 77 have been declared then,$total = $number1+$number2+$number3 and on and on.

Edited by Faracus
Link to comment
Share on other sites

What I am doing is, users get to select a number from 1-77 then when they submit it, the server then updates the percent off chosen numbers. I am sure it would be easier to do in another language, but unfortunatly I don't know any other languages :)

Link to comment
Share on other sites

In the database there will be numbers from 1 all the way to 77, the user will select one or two numbers, when they submit it to the database it will add +1 count to the numbers they selected, then the server will update the percentages, say there are 5 votes for #1, 2 votes for #5, and 3 votes for #10.Then the server will update the percentages so that #1 = 50%, #5 = 20% and #10 = 30%.Then when another user selects say #1 and #10, on the next screen it will print out that the results are now: #1 = 50% #5 = 16.66% and #10 = 33.33%using the formula, "number of votes/total votesX100=percent of votes"

Link to comment
Share on other sites

In the database there are 78 fields, 1-77 then 78 is total.I came up with this as the showing page

<?phpecho "<head><title>ShackGuys | BETA | " . $_GET["lottery"] . "</title></head><body>Here are your " . $_GET["lottery"] . " numbers and their chances of showing up.<br /><br />";$con = mysql_connect("localhost","username","password");if (!con)	{	die ('Could not connect: ' .mysql_error());	}mysql_select_db("lottery, $con");$number1 = mysql_query("SELECT 1 FROM " . $_GET["lottery"] . "")$number2 = mysql_query("SELECT 2 FROM " . $_GET["lottery"] . "")$number3 = mysql_query("SELECT 3 FROM " . $_GET["lottery"] . "")$number4 = mysql_query("SELECT 4 FROM " . $_GET["lottery"] . "")$number5 = mysql_query("SELECT 5 FROM " . $_GET["lottery"] . "")$number6 = mysql_query("SELECT 6 FROM " . $_GET["lottery"] . "")$number7 = mysql_query("SELECT 7 FROM " . $_GET["lottery"] . "")$number8 = mysql_query("SELECT 8 FROM " . $_GET["lottery"] . "")$number9 = mysql_query("SELECT 9 FROM " . $_GET["lottery"] . "")$number10 = mysql_query("SELECT 10 FROM " . $_GET["lottery"] . "")$number11 = mysql_query("SELECT 11 FROM " . $_GET["lottery"] . "")$number12 = mysql_query("SELECT 12 FROM " . $_GET["lottery"] . "")$number13 = mysql_query("SELECT 13 FROM " . $_GET["lottery"] . "")$number14 = mysql_query("SELECT 14 FROM " . $_GET["lottery"] . "")$number15 = mysql_query("SELECT 15 FROM " . $_GET["lottery"] . "")$number16 = mysql_query("SELECT 16 FROM " . $_GET["lottery"] . "")$number17 = mysql_query("SELECT 17 FROM " . $_GET["lottery"] . "")$number18 = mysql_query("SELECT 18 FROM " . $_GET["lottery"] . "")$number19 = mysql_query("SELECT 19 FROM " . $_GET["lottery"] . "")$number20 = mysql_query("SELECT 20 FROM " . $_GET["lottery"] . "")$number21 = mysql_query("SELECT 21 FROM " . $_GET["lottery"] . "")$number22 = mysql_query("SELECT 22 FROM " . $_GET["lottery"] . "")$number23 = mysql_query("SELECT 23 FROM " . $_GET["lottery"] . "")$number24 = mysql_query("SELECT 24 FROM " . $_GET["lottery"] . "")$number25 = mysql_query("SELECT 25 FROM " . $_GET["lottery"] . "")$number26 = mysql_query("SELECT 26 FROM " . $_GET["lottery"] . "")$number27 = mysql_query("SELECT 27 FROM " . $_GET["lottery"] . "")$number28 = mysql_query("SELECT 28 FROM " . $_GET["lottery"] . "")$number29 = mysql_query("SELECT 29 FROM " . $_GET["lottery"] . "")$number30 = mysql_query("SELECT 30 FROM " . $_GET["lottery"] . "")$number31 = mysql_query("SELECT 31 FROM " . $_GET["lottery"] . "")$number32 = mysql_query("SELECT 32 FROM " . $_GET["lottery"] . "")$number33 = mysql_query("SELECT 33 FROM " . $_GET["lottery"] . "")$number34 = mysql_query("SELECT 34 FROM " . $_GET["lottery"] . "")$number35 = mysql_query("SELECT 35 FROM " . $_GET["lottery"] . "")$number36 = mysql_query("SELECT 36 FROM " . $_GET["lottery"] . "")$number37 = mysql_query("SELECT 37 FROM " . $_GET["lottery"] . "")$number38 = mysql_query("SELECT 38 FROM " . $_GET["lottery"] . "")$number39 = mysql_query("SELECT 39 FROM " . $_GET["lottery"] . "")$number40 = mysql_query("SELECT 40 FROM " . $_GET["lottery"] . "")$number41 = mysql_query("SELECT 41 FROM " . $_GET["lottery"] . "")$number42 = mysql_query("SELECT 42 FROM " . $_GET["lottery"] . "")$number43 = mysql_query("SELECT 43 FROM " . $_GET["lottery"] . "")$number44 = mysql_query("SELECT 44 FROM " . $_GET["lottery"] . "")$number45 = mysql_query("SELECT 45 FROM " . $_GET["lottery"] . "")$number46 = mysql_query("SELECT 46 FROM " . $_GET["lottery"] . "")$number47 = mysql_query("SELECT 47 FROM " . $_GET["lottery"] . "")$number48 = mysql_query("SELECT 48 FROM " . $_GET["lottery"] . "")$number49 = mysql_query("SELECT 49 FROM " . $_GET["lottery"] . "")$number50 = mysql_query("SELECT 50 FROM " . $_GET["lottery"] . "")$number51 = mysql_query("SELECT 51 FROM " . $_GET["lottery"] . "")$number52 = mysql_query("SELECT 52 FROM " . $_GET["lottery"] . "")$number53 = mysql_query("SELECT 53 FROM " . $_GET["lottery"] . "")$number54 = mysql_query("SELECT 54 FROM " . $_GET["lottery"] . "")$number55 = mysql_query("SELECT 55 FROM " . $_GET["lottery"] . "")$number56 = mysql_query("SELECT 56 FROM " . $_GET["lottery"] . "")$number57 = mysql_query("SELECT 57 FROM " . $_GET["lottery"] . "")$number58 = mysql_query("SELECT 58 FROM " . $_GET["lottery"] . "")$number59 = mysql_query("SELECT 59 FROM " . $_GET["lottery"] . "")$number60 = mysql_query("SELECT 60 FROM " . $_GET["lottery"] . "")$number61 = mysql_query("SELECT 61 FROM " . $_GET["lottery"] . "")$number62 = mysql_query("SELECT 62 FROM " . $_GET["lottery"] . "")$number63 = mysql_query("SELECT 63 FROM " . $_GET["lottery"] . "")$number64 = mysql_query("SELECT 64 FROM " . $_GET["lottery"] . "")$number65 = mysql_query("SELECT 65 FROM " . $_GET["lottery"] . "")$number66 = mysql_query("SELECT 66 FROM " . $_GET["lottery"] . "")$number67 = mysql_query("SELECT 67 FROM " . $_GET["lottery"] . "")$number68 = mysql_query("SELECT 68 FROM " . $_GET["lottery"] . "")$number69 = mysql_query("SELECT 69 FROM " . $_GET["lottery"] . "")$number70 = mysql_query("SELECT 70 FROM " . $_GET["lottery"] . "")$number71 = mysql_query("SELECT 71 FROM " . $_GET["lottery"] . "")$number72 = mysql_query("SELECT 72 FROM " . $_GET["lottery"] . "")$number73 = mysql_query("SELECT 73 FROM " . $_GET["lottery"] . "")$number74 = mysql_query("SELECT 74 FROM " . $_GET["lottery"] . "")$number75 = mysql_query("SELECT 75 FROM " . $_GET["lottery"] . "")$number76 = mysql_query("SELECT 76 FROM " . $_GET["lottery"] . "")$number77 = mysql_query("SELECT 77 FROM " . $_GET["lottery"] . "")$total = mysql_query("SELECT total FROM " . $_GET["lottery"] . "")$total1 = $number1/$total*100$total2 = $number2/$total*100$total3 = $number3/$total*100$total4 = $number4/$total*100$total5 = $number5/$total*100$total6 = $number6/$total*100$total7 = $number7/$total*100$total8 = $number8/$total*100$total9 = $number9/$total*100$total10 = $number10/$total*100$total11 = $number11/$total*100$total12 = $number12/$total*100$total13 = $number13/$total*100$total14 = $number14/$total*100$total15 = $number15/$total*100$total16 = $number16/$total*100$total17 = $number17/$total*100$total18 = $number18/$total*100$total19 = $number19/$total*100$total20 = $number20/$total*100$total21 = $number21/$total*100$total22 = $number22/$total*100$total23 = $number23/$total*100$total24 = $number24/$total*100$total25 = $number25/$total*100$total26 = $number26/$total*100$total27 = $number27/$total*100$total28 = $number28/$total*100$total29 = $number29/$total*100$total30 = $number30/$total*100$total31 = $number31/$total*100$total32 = $number32/$total*100$total33 = $number33/$total*100$total34 = $number34/$total*100$total35 = $number35/$total*100$total36 = $number36/$total*100$total37 = $number37/$total*100$total38 = $number38/$total*100$total39 = $number39/$total*100$total40 = $number40/$total*100$total41 = $number41/$total*100$total42 = $number42/$total*100$total43 = $number43/$total*100$total44 = $number44/$total*100$total45 = $number45/$total*100$total46 = $number46/$total*100$total47 = $number47/$total*100$total48 = $number48/$total*100$total49 = $number49/$total*100$total50 = $number50/$total*100$total51 = $number51/$total*100$total52 = $number52/$total*100$total53 = $number53/$total*100$total54 = $number54/$total*100$total55 = $number55/$total*100$total56 = $number56/$total*100$total57 = $number57/$total*100$total58 = $number58/$total*100$total59 = $number59/$total*100$total60 = $number60/$total*100$total61 = $number61/$total*100$total62 = $number62/$total*100$total63 = $number63/$total*100$total64 = $number64/$total*100$total65 = $number65/$total*100$total66 = $number66/$total*100$total67 = $number67/$total*100$total68 = $number68/$total*100$total69 = $number69/$total*100$total70 = $number70/$total*100$total71 = $number71/$total*100$total72 = $number72/$total*100$total73 = $number73/$total*100$total74 = $number74/$total*100$total75 = $number75/$total*100$total76 = $number76/$total*100$total77 = $number77/$total*100echo "1: " . $total1 . "<br />2: " . $total2 . "<br/>3: " . $total3 . "<br/>4: " . $total4 . "<br/>5: " . $total5 . "<br/>6: " . $total6 . "<br/>7: " . $total7 . "<br/>8: " . $total8 . "<br/>9: " . $total9 . "<br/>10: " . $total10 . "<br/>11: " . $total11 . "<br/>12: " . $total12 . "<br/>13: " . $total13 . "<br/>14: " . $total14 . "<br/>15: " . $total15 . "<br/>16: " . $total16 . "<br/>17: " . $total17 . "<br/>18: " . $total18 . "<br/>19: " . $total19 . "<br/>20: " . $total20 . "<br/>21: " . $total21 . "<br/>22: " . $total22 . "<br/>23: " . $total23 . "<br/>24: " . $total24 . "<br/>25: " . $total25 . "<br/>26: " . $total26 . "<br/>27: " . $total27 . "<br/>28: " . $total28 . "<br/>29: " . $total29 . "<br/>30: " . $total30 . "<br/>31: " . $total31 . "<br/>32: " . $total32 . "<br/>33: " . $total33 . "<br/>34: " . $total34 . "<br/>35: " . $total35 . "<br/>36: " . $total36 . "<br/>37: " . $total37 . "<br/>38: " . $total38 . "<br/>39: " . $total39 . "<br/>40: " . $total40 . "<br/>41: " . $total41 . "<br/>42: " . $total42 . "<br/>43: " . $total43 . "<br/>44: " . $total44 . "<br/>45: " . $total45 . "<br/>46: " . $total46 . "<br/>47: " . $total47 . "<br/>48: " . $total48 . "<br/>49: " . $total49 . "<br/>50: " . $total50 . "<br/>51: " . $total51 . "<br/>52: " . $total52 . "<br/>53: " . $total53 . "<br/>54: " . $total54 . "<br/>55: " . $total55 . "<br/>56: " . $total56 . "<br/>57: " . $total57 . "<br/>58: " . $total58 . "<br/>59: " . $total59 . "<br/>60: " . $total60 . "<br/>61: " . $total61 . "<br/>62: " . $total62 . "<br/>63: " . $total63 . "<br/>64: " . $total64 . "<br/>65: " . $total65 . "<br/>66: " . $total66 . "<br/>67: " . $total67 . "<br/>68: " . $total68 . "<br/>69: " . $total69 . "<br/>70: " . $total70 . "<br/>71: " . $total71 . "<br/>72: " . $total72 . "<br/>73: " . $total73 . "<br/>74: " . $total74 . "<br/>75: " . $total75 . "<br/>76: " . $total76 . "<br/>77: " . $total77 . "</body>";?>

But unfortunatly I am getting the error "Parse error: syntax error, unexpected T_VARIABLE" on the $number2

Link to comment
Share on other sites

You are missing semicolons after your calls to mysql_query.You can't use a number as a field name. If you say 'SELECT 40', it's going to return the number 40, always. Numbers are not field names, they're numbers. You can also return strings or any other data using SELECT:SELECT 'some text' AS text_fieldSELECT true AS bool_fieldSELECT 42 AS num_fieldSELECT PASSWORD('1234') AS password_fieldSELECT NOW() AS date_field

Link to comment
Share on other sites

quick question:what is wrong with the following?$number1 = mysql_query("SELECT number$_POST['number1'] FROM $_POST['lottery']");i'm getting the error Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING

Link to comment
Share on other sites

You need brackets if you want to use an array in a string.$number1 = mysql_query("SELECT number{$_POST['number1']} FROM {$_POST['lottery']}");It's also dangerous to put anything from $_GET, $_POST, or $_COOKIE directly into a SQL query. You're just asking for attacks.

Edited by justsomeguy
Link to comment
Share on other sites

You need brackets if you want to use an array in a string.$number1 = mysql_query("SELECT number{$_POST['number1']} FROM {$_POST['lottery']}");It's also dangerous to put anything from $_GET, $_POST, or $_COOKIE directly into a SQL query. You're just asking for attacks.
That's awesome, and here's me writing things likeWHERE id='".$_SESSION["id"]."'";Braces are far easier.
Link to comment
Share on other sites

$number1 = mysql_query("SELECT number{$_POST["number1"]} FROM {$_POST["lottery"]}");$number2 = mysql_query("SELECT number{$_POST["number2"]} FROM {$_POST["lottery"]}");$number3 = mysql_query("SELECT number{$_POST["number3"]} FROM {$_POST["lottery"]}");$number4 = mysql_query("SELECT number{$_POST["number4"]} FROM {$_POST["lottery"]}");$number5 = mysql_query("SELECT number{$_POST["number5"]} FROM {$_POST["lottery"]}");$number6 = mysql_query("SELECT number{$_POST["number6"]} FROM {$_POST["lottery"]}");$number7 = mysql_query("SELECT number{$_POST["number7"]} FROM {$_POST["lottery"]}");$number8 = mysql_query("SELECT number{$_POST["number8"]} FROM {$_POST["lottery"]}");$total = mysql_query("SELECT total FROM {$_POST["lottery"]}");$newnumber1 = $number1+1;$newnumber2 = $number2+1;$newnumber3 = $number3+1;$newnumber4 = $number4+1;$newnumber5 = $number5+1;$newnumber6 = $number6+1;$newnumber7 = $number7+1;$newnumber8 = $number8+1;$newtotal = $total+1;mysql_query("UPDATE {$_POST["lottery"]} SET {$_POST["number1"]}  = '$newnumber1'");mysql_query("UPDATE {$_POST["lottery"]} SET {$_POST["number2"]}  = '$newnumber2'");mysql_query("UPDATE {$_POST["lottery"]} SET {$_POST["number3"]}  = '$newnumber3'");mysql_query("UPDATE {$_POST["lottery"]} SET {$_POST["number4"]}  = '$newnumber4'");mysql_query("UPDATE {$_POST["lottery"]} SET {$_POST["number5"]}  = '$newnumber5'");mysql_query("UPDATE {$_POST["lottery"]} SET {$_POST["number6"]}  = '$newnumber6'");mysql_query("UPDATE {$_POST["lottery"]} SET {$_POST["number7"]}  = '$newnumber7'");mysql_query("UPDATE {$_POST["lottery"]} SET {$_POST["number8"]}  = '$newnumber8'");mysql_query("UPDATE {$_POST["lottery"]} SET total  = '$newtotal'");echo "You have now updated:<br />";echo $number1;echo " to ";echo $newnumber1;echo "<br />";echo "You have now updated:<br />";echo $number2;echo " to ";echo $newnumber2;echo "<br />";echo "You have now updated:<br />";echo $number3;echo " to ";echo $newnumber3;echo "<br />";echo "You have now updated:<br />";echo $number4;echo " to ";echo $newnumber4;echo "<br />";echo "You have now updated:<br />";echo $number5;echo " to ";echo $newnumber5;echo "<br />";echo "You have now updated:<br />";echo $number6;echo " to ";echo $newnumber6;echo "<br />";echo "You have now updated:<br />";echo $number7;echo " to ";echo $newnumber7;echo "<br />";echo "You have now updated:<br />";echo $number8;echo " to ";echo $newnumber8;echo "<br />";echo "The database has been updated ";echo $newtotal;echo "times";echo "You chose the numbers: <br />" . $_POST["number1"] . ", " . $_POST ["number2"] . ", " . $_POST["number3"] . ", " . $_POST["number4"] . ", " . $_POST["number5"] . ", " . $_POST["number6"] . ", " . $_POST["number7"] . ", " . $_POST["number8"] . ".";

This is the current code I am using currently, the numbers at the beginning are not being taken from the database. the numbers are going through as I have checked with the bottom.

Edited by Faracus
Link to comment
Share on other sites

You only have one record in your database? Remember that mysql_query() returns a resource, you need to use mysql_fetch_assoc() to get the actual values. Also, what you are doing is extremely inefficient, it would be far faster to only do one query. And why are you using `number{$_POST["number1"]}` etc. for your field names - are they really called that?

$result = mysql_query("SELECT * FROM {$_POST["lottery"]}");while ($row = mysql_fetch_assoc($result)) {	for ($i = 1; $i <= 77; $i++) {		$number = "number$i";		$$number = $row[$number];	}  }

Link to comment
Share on other sites

What about just doing this:"UPDATE {$_POST['lottery']} SET {$_POST['number1']} = {$_POST['number1']} + 1"That's going to be better for several reasons, one being that it's just quicker.And again, it's a terrible practice to store field names or table names in post.

Edited by justsomeguy
Link to comment
Share on other sites

Ok so getting the table to update is now working, thanks for all your help, but that's only part one.Part two is getting the information to show up.

$number1 = mysql_query("SELECT number1 FROM n649");$total = mysql_query("SELECT numbertotal FROM n649");$total1 = $number1/$total*100;echo "1: $total1

Is the code I am using to display it and calculate it then display it, but unfortunatly even though the "numbertotal" is 2, I still get the error "Warning: Division by zero"Any ideas on why it's not working?

Link to comment
Share on other sites

Ok so now I think it hates me haha.I am now using

$sql = mysql_query("SELECT * FROM n649");$result = mysql_query($sql);while ($row = mysql_fetch_assoc($result)) 	{	echo $row;	echo "<br />";	}

which is pretty much how it is set up on the PHP site, just without all the error dumping, and i am still getting the error "Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource"EDIT: fixed the problem, thanks for everyone's help.-CLOSE TOPIC-

Edited by Faracus
Link to comment
Share on other sites

You run this:$sql = mysql_query("SELECT * FROM n649");So after that, $sql contains the results of the query. Then you run this:$result = mysql_query($sql);So now you're calling mysql_query and instead of sending it a SQL query, now you're sending it the result from the last query. Also, once you do this:while ($row = mysql_fetch_assoc($result)) $row is an array of fields. If you print $row, it's going to print the word "Array". Check the example again to see how to print an individual field.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...