Jump to content

[SOLVED] mysql_fetch_array


eon201

Recommended Posts

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 :D

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

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??

Link to comment
Share on other sites

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();

 

?>

Link to comment
Share on other sites

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 ;)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.