pro_se Posted July 10, 2007 Share Posted July 10, 2007 Hi, I am working on a project where I need to select rows from a table called 'hub' and take the rows where 'to_id' = '$to_id' and put them into an array. How can I get MySql info into an array? Any help is appreciated. Quote Link to comment Share on other sites More sharing options...
teng84 Posted July 10, 2007 Share Posted July 10, 2007 learn the basic i suggest http://w3schools.com/php/php_mysql_select.asp read Quote Link to comment Share on other sites More sharing options...
pro_se Posted July 10, 2007 Author Share Posted July 10, 2007 Well, I can make queries like that shows. What I want to do with inserting the selected table into an array is what I am unsure about. Quote Link to comment Share on other sites More sharing options...
teng84 Posted July 10, 2007 Share Posted July 10, 2007 heres one of the content of the site i gave you <?php $con = mysql_connect("localhost","peter","abc123"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("my_db", $con); $result = mysql_query("SELECT * FROM person"); while($row = mysql_fetch_array($result))//see this $row here in array from the db ???? { echo $row['FirstName'] . " " . $row['LastName']; echo "<br />"; } mysql_close($con); ?> Quote Link to comment Share on other sites More sharing options...
pro_se Posted July 10, 2007 Author Share Posted July 10, 2007 So, what I am doing is adding integers from an array (the one I pulled out of the mysql db) together, how can I add the multiple rows I am pulling out... I was thinking of using array_sum, but I did not know how to go about using it.. Quote Link to comment Share on other sites More sharing options...
pro_se Posted July 12, 2007 Author Share Posted July 12, 2007 <?php $pre_toid = explode("?id=", $_SERVER['HTTP_REFERER']); $toid = $pre_toid[1]; $result = mysql_query("SELECT * FROM `hub` where to_id='".$toid."'"); $nvotes=mysql_num_rows($result); while($row=mysql_fetch_array($result)) { $dbratings = $row["rating"]; $votes_array = array("votes" => $dbratings); } $added_votes = array_sum($votes_array); $divided_votes = $added_votes/$nvotes; $total_rating = $divided_votes; ?> Ok, I got that far, but there is something wrong with the while loop. When I try and print or add the array it only prints or gets the sum of one vote from the array, and I know for a fact that there are at least 30 entries in the table. Quote Link to comment Share on other sites More sharing options...
pro_se Posted July 12, 2007 Author Share Posted July 12, 2007 Anyone? Quote Link to comment Share on other sites More sharing options...
suma237 Posted July 12, 2007 Share Posted July 12, 2007 in the select statement you use one condition where id=...check whether the id have how many entries? Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted July 12, 2007 Share Posted July 12, 2007 You need to make sure you don't error out on 0 because you ill get an error right now try this to help <?php /* $pre_toid = explode("?id=", $_SERVER['HTTP_REFERER']); $toid = $pre_toid[1]; */ //try this to save you time since id is a get var $toid = $_GET['id']; $result = mysql_query("SELECT * FROM `hub` where to_id='".$toid."'"); $nvotes=mysql_num_rows($result); if ($nvotes >0){ while($row=mysql_fetch_array($result)) { //dbratings = $row["rating"] Pointless line just use below since you never use dbratings $votes_array = array("votes" => $row['rating']); } } $added_votes = array_sum($votes_array); $divided_votes = $added_votes/$nvotes; $total_rating = $divided_votes; ?> Quote Link to comment Share on other sites More sharing options...
pro_se Posted July 12, 2007 Author Share Posted July 12, 2007 WOW! Thanks man, that really helped me out! I appreciate it! Quote Link to comment Share on other sites More sharing options...
pro_se Posted July 12, 2007 Author Share Posted July 12, 2007 actually, wait..... I have like 30 entries in the table right, and its not making sense because when I do <?php print_r($votes_array); ?> its only printing out one of the rows. I want to get the sum of all the rows, not just one of them... Quote Link to comment Share on other sites More sharing options...
pro_se Posted July 12, 2007 Author Share Posted July 12, 2007 like, when I do array_sum($votes_array), i get a small number like 4. its supposed to be counting all the rows in the table..... something is not right with the inserting into an array part of it... Quote Link to comment Share on other sites More sharing options...
pro_se Posted July 12, 2007 Author Share Posted July 12, 2007 hmpff. anyone? Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted July 12, 2007 Share Posted July 12, 2007 <?php /* $pre_toid = explode("?id=", $_SERVER['HTTP_REFERER']); $toid = $pre_toid[1]; */ //try this to save you time since id is a get var $toid = $_GET['id']; $result = mysql_query("SELECT * FROM `hub` where to_id='".$toid."'"); $nvotes=mysql_num_rows($result); if ($nvotes >0){ while($row=mysql_fetch_array($result)) { //dbratings = $row["rating"] Pointless line just use below since you never use dbratings $votes_array['votes'][] = $row['ratings']; //appends to a multi level array $votes_array['votes'] } } echo $nvotes."<br/><br/>"; //To see how many results print_r($votes_array['votes'][]; //Try and see what this gives you $added_votes = array_sum($votes_array['votes']); $divided_votes = $added_votes/$nvotes; $total_rating = $divided_votes; ?> Quote Link to comment 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.