eon201 Posted November 15, 2007 Share Posted November 15, 2007 Hi, Im currently pulling in a load of data from my sql table. I found that using the mysql_fetch_array function was the way to pull it all in as an array. The only problem? I cant manage to get it to store a value into another name eg $ip = $row['ip'] and count how many instance's are contained within it. I need to do similar things to the 'url' and the 'time' but as I cant access the data there isnt much I can do! Can anyone help me?? I havnt used the 'while' statement or the mysql_fetch_array function before so im feeling quite lost. $row = mysql_fetch_array( $result ); while($row = mysql_fetch_array($result)){ echo $row['ip']. " - ". $row['url']. " - ". $row['date']; echo "<br />"; } Thanks as always. Eon201 Quote Link to comment https://forums.phpfreaks.com/topic/77472-solved-mysql_fetch_array/ Share on other sites More sharing options...
kratsg Posted November 15, 2007 Share Posted November 15, 2007 Try the list function, may be of more use... $row = mysql_fetch_array( $result ); while(list($ip,$url,$date) = mysql_fetch_array($result)){ echo $ip." - ".$url." - ".$date; echo "<br />"; } The list function works like this, if each set of a row is an array, then list the array elements in order from 0th index to nth index with $ip, $url, $date (as the 0th, 1st, and 2nd). Quote Link to comment https://forums.phpfreaks.com/topic/77472-solved-mysql_fetch_array/#findComment-392184 Share on other sites More sharing options...
eon201 Posted November 15, 2007 Author Share Posted November 15, 2007 Thanks Kratsg, so far the code is now like this.. $row = mysql_fetch_array( $result ); while(list($ip,$url,$date) = mysql_fetch_array($result)){ echo $ip." - ".$url." - ".$date; echo "<br />"; } $visitstoday = count($ip); //This is what I need to do. It currently returns a big fat 0 echo "$visitstoday"; mysql_close(); Good tip on the list function - very handy What I need to do though is count how many ip's occur within the array without having to display the whole thing! Any ideas?? Quote Link to comment https://forums.phpfreaks.com/topic/77472-solved-mysql_fetch_array/#findComment-392190 Share on other sites More sharing options...
rajivgonsalves Posted November 15, 2007 Share Posted November 15, 2007 try this <? $row = mysql_fetch_array( $result ); $arrData = array() while(list($ip,$url,$date) = mysql_fetch_array($result)){ if (!isset($arrData[$ip])) { $arrData[$ip] = array(); } $arrData[$ip][] = array("url"=>$url,"date"=>$date); } $visitstoday = count($arrData); //This is what I need to do. It currently returns a big fat 0 echo "$visitstoday"; mysql_close(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/77472-solved-mysql_fetch_array/#findComment-392203 Share on other sites More sharing options...
kenrbnsn Posted November 15, 2007 Share Posted November 15, 2007 Please show us the MySQL query you're using. Ken Quote Link to comment https://forums.phpfreaks.com/topic/77472-solved-mysql_fetch_array/#findComment-392204 Share on other sites More sharing options...
eon201 Posted November 15, 2007 Author Share Posted November 15, 2007 Ok here is the full code... (without sql login info!) mysql_connect("localhost",$username,$password); @mysql_select_db($database) or die( "Unable to select database"); echo 'CONNECT TO MYSQL SUCCESSFUL<br/><br/> '; // Retrieve all the data from the "log" table $result = mysql_query("SELECT * FROM logtable") or die(mysql_error()); echo "I have got the data from the table - logtable <br/><br/>"; // store the record of the "log" table into $row $row = mysql_fetch_array( $result ); while($row = mysql_fetch_array( $result )) { echo $row['ip']. " -- "; echo $row['url']. " -- "; echo $row['date']. "<br/><br/>"; $ip[] = $row['ip']; $url[] = $row['url']; $date[] = $row['date']; } Literally all I would like to do is be able to: Count each value compare each value to another ie $url / $ip and echo each value It should be so so simple. Yet this all behaves differently now im calling it back from the mysql. Thanks for the help so far Quote Link to comment https://forums.phpfreaks.com/topic/77472-solved-mysql_fetch_array/#findComment-392208 Share on other sites More sharing options...
kenrbnsn Posted November 15, 2007 Share Posted November 15, 2007 Remove this line <?php $row = mysql_fetch_array( $result ); ?> It is causing your code to miss the first row of data. Ken Quote Link to comment https://forums.phpfreaks.com/topic/77472-solved-mysql_fetch_array/#findComment-392209 Share on other sites More sharing options...
eon201 Posted November 15, 2007 Author Share Posted November 15, 2007 Ok thanks for that. But does anyone know how to solve my original problem??? Quote Link to comment https://forums.phpfreaks.com/topic/77472-solved-mysql_fetch_array/#findComment-392241 Share on other sites More sharing options...
eon201 Posted November 16, 2007 Author Share Posted November 16, 2007 Hmmm... I Still havnt figured this out. Is it becuase im using the wron function of mysql_fetch_array in the first place??? Thanks. Eon201 Quote Link to comment https://forums.phpfreaks.com/topic/77472-solved-mysql_fetch_array/#findComment-392750 Share on other sites More sharing options...
eon201 Posted November 16, 2007 Author Share Posted November 16, 2007 Im an idiot! It works fine now! Must have just been a problem in my syntax somewhere!! Thanks to all who have lent a hand! Quote Link to comment https://forums.phpfreaks.com/topic/77472-solved-mysql_fetch_array/#findComment-392756 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.