Hi i am trying to find our what city my users are in from their ip address to be able to display a list of users in my database close to them/in their area.
I have been looking at this api:
http://ipinfodb.com/ip_location_api.php
and i have registered and got an api key, but im not sure where to go from here. Here is the example code they have used to get the users location:
<?php
include('ip2locationlite.class.php');
//Load the class
$ipLite = new ip2location_lite;
$ipLite->setKey('<api key>');
//Get errors and locations
$locations = $ipLite->getCity($_SERVER['REMOTE_ADDR']);
$errors = $ipLite->getError();
//Getting the result
echo "<p>\n";
echo "<strong>First result</strong><br />\n";
if (!empty($locations) && is_array($locations)) {
foreach ($locations as $field => $val) {
echo $field . ' : ' . $val . "<br />\n";
}
}
echo "</p>\n";
//Show errors
echo "<p>\n";
echo "<strong>Dump of all errors</strong><br />\n";
if (!empty($errors) && is_array($errors)) {
foreach ($errors as $error) {
echo var_dump($error) . "<br /><br />\n";
}
} else {
echo "No errors" . "<br />\n";
}
echo "</p>\n";
?>
but how would i now set a mysql query that says if the user location is london and users are listed as in london in the database then to display these to the user?
i would really appreciate a push in the right direction here if anyone can help, thanks.
Here is an example (without knowing your database structure and untested ;). Insert this in the foreach ($locations as $field => $val) loop as below:
foreach ($locations as $field => $val) {
echo $field . ' : ' . $val . "<br />\n";
$mysqli = new mysqli('<mysql server>', '<mysql user>', '<mysql password>', '<database>');
if(mysqli_connect_errno()) {
echo 'Error: Unable to connect to Database; ';
} else {
if ($stmt_evt = $mysqli -> prepare('SELECT <other_users>, ... FROM <your table> WHERE <user_location> LIKE ? ORDER BY <whatever>')) {
$stmt_evt -> bind_param('s', $val);
$stmt_evt -> execute();
$stmt_evt -> bind_result($<other_users>, $...);
while ($stmt_evt->fetch()) {
echo 'Other users in your area: '.$<other_users>;
}
} else {
echo 'Error: Unable to prepare Event statement.';
}
}
}
Related
I am having trouble echoing row data within the page I want to print it out to.
My function works, but only echoes the information because it is local (within the same function).
I'm trying to get this function to echo the database's information to another .php file (same program).
public function findByUsername($username) {
$sql = 'SELECT * FROM events WHERE username=? ';
$statement = DatabaseHelper::runQuery($this->connection, $sql, Array($username));
while ($row = $statement->fetch()) {
echo $row['username'] . "<br />";
echo $row['date'] . "<br />";
}
}
Here is the part of the other file I need to print the information from the function to:
<?php
if (isset($_SESSION["username"])) {
$eventDAO = new EventDAO($connection);
$event = $eventDAO->findByUsername($_SESSION["username"]);
foreach((array)$event as $e) {
echo $e->getUsername() . ' ' . $e->getDate() . '<br>';
}
}
?>
I'm trying to output the username/date.
Not 100% on this concept.
If you're simply trying to output username/date from one file to another, try using sessions.
public function findByUsername($username) {
$sql = 'SELECT * FROM events WHERE username=? ';
$statement = DatabaseHelper::runQuery($this->connection, $sql, Array($username));
while ($row = $statement->fetch()) {
echo $row['username'] . "<br />";
echo $row['date'] . "<br />";
$_SESSION['getuname'] = $row['username'];
$_SESSION['getdate'] = $row['date'];
}
}
You can then output the information on another php file simply by echoing it out.
echo "Username is ". $_SESSION['getuname'];
I created a table to store the results of my sql query.
My goal is to display the first line of the table and when I click on a button it shows me the following line:
$questions = array();
$resultat = $pdo -> query("SELECT question, rep1, rep2, rep3, rep4 FROM proposition");
while($row=$resultat -> fetch(PDO::FETCH_ASSOC)){
$questions[] = array($row['question'], $row['rep1'], $row['rep2'],
$row['rep3'], $row['rep4']);
}
foreach($questions as $cle1 => $valeur1)
{
echo "personne n°:" . $cle1 . "<br />";
foreach ($valeur1 as $cle2=>$valeur2)
{
echo "Clé : ".$cle2 .", Valeur: " . $valeur2 . "<br />\n";
}
}
I have a loop that looks like this:
/* For Loop for all sheets */
for($i=0;$i<$totalSheet;$i++) {
$Reader->ChangeSheet($i);
foreach ($Reader as $Row) {
//variables here
$query = "INSERT INTO schools (
//code here
)";
if ($mysqli->query($query) === TRUE) {
echo "<br> New record created successfully <br>";
} else {
echo "Error: " . $query . "<br>" . $mysqli->error;
}
}
}
How do I turn this:
if ($mysqli->query($query) === TRUE) {
echo "<br> New record created successfully <br>";
into a code that will show something like:
XX inputted successfully
{list of schools with ID listed here}
XX not inpputed due to errors
{list of schools not inputted}
Would like to see a simple summary of the entire loop rather than seeing a repeated result of each loop that occurred.
Collect all the information you want in variables, and print them at the end.
$success = $fail = "";
$success_count = $fail_count = 0;
for($i=0;$i<$totalSheet;$i++) {
$Reader->ChangeSheet($i);
foreach ($Reader as $Row) {
//variables here
$query = "INSERT INTO schools (
//code here
)";
if ($mysqli->query($query)) {
$success .= "<li>" . $Row['school_name'] . "</li>";
$success_count++;
} else {
$fail .= "<li>" . $Row['school_name'] . "</li>";
$fail_count++;
}
}
}
echo $success_count . " inputted successfully:<br><ul>" . $success . "</ul>";
echo $fail_count . " not inputted due to errors:<br><ul>" . $fail . "</ul>";
Replace $Row['school_name'] with whatever the correct variable is for the school name in your data.
I am trying to figure out how to use this in a loop. Any help will be appreciated.
$conn = mysql_connect("localhost", "some_user", "password");
if (!$conn) {
echo "Unable to connect to DB: " . mysql_error();
exit;
}
if (!mysql_select_db("some_db")) {
echo "Unable to select mydbname: " . mysql_error();
exit;
}
$sql = "SELECT favid FROM ajaxfavourites";
$result = mysql_query($sql);
if (!$result) {
echo "Could not successfully run query ($sql) from DB: " . mysql_error();
exit;
}
while ($row = mysql_fetch_assoc($result)) {
echo $row["favid"];
}
mysql_free_result($result);
Currently it displays results as:
116677889922
I need them to show them as (the way they are displayed in DB):
1166
7788
9922
PS I am aware that this function is deprecated, I am just trying to fix one of my older sites.
choose One of these ways:
echo $row["favid"]."<br>";
echo $row["favid"]."\n";
echo $row["favid"].PHP_EOL;
while ($row = mysql_fetch_assoc($result)) {
echo $row["favid"];
echo "\r\n";
}
You can simply echo the value with '<br/>' or '<p>' like following:
while ($row = mysql_fetch_assoc($result)) {
echo $row["favid"] . '<br/>';
}
OR
while ($row = mysql_fetch_assoc($result)) {
echo '<p>' . $row["favid"] . '</p>';
}
Also you can just put all favid into array and then in another loop, can customize how to show them, like following:
while ($row = mysql_fetch_assoc($result)) {
$ids[] = $row["favid"];
}
foreach($ids AS $idv) {
echo '<p>' . $idv . '</p>';
}
I have written this PHP code and worked correctly... then I wanted a particular code segment to be worked as a function, but as soon I did this I didn't get the correct result... I'm confused what has gone wrong, can somebody please help me with this... Thanks a lot...
Here's the code gives me the error...
$arr1=array();
$date = date("D");
$link = mysql_connect ('localhost', 'root', '');
$db = mysql_select_db ('dayevent', $link);
function grabData($arr){ //works properly NOT as a function, but I want to make this code part act like a funciton
$i=0;
$sql = "SELECT event FROM events WHERE day = '$date'";
$sel = mysql_query($sql);
echo $sel; //this prints Resource id #3
if (mysql_num_rows($sel) > 0) { // but doesn't go into if block
while($row = mysql_fetch_array($sel)) {
echo $row['event'] . '<br />';
//storing DB query result in array
$arr[$i]=$row['event'];
$i=$i+1;
}
foreach($arr as $key => $value) {
echo $key . " " . $value . "<br />";
}
} else echo 'Nothing returned!'; //prints this instead of the correct result
}
grabData($arr1);
mysql_close();
Move this inside your function: $date = date("D");. The way it is now, $date is not defined. If you run with error_reporting(E_ALL) you would have caught it right away.
Test Below Code :
EDITED
$arr1=array();
$date = date("D");
$link = mysql_connect ('localhost', 'root', '');
$db = mysql_select_db ('dayevent', $link);
function grabData()
{
global $link,$date;
$arr = array();
$i=0;
$sql = "SELECT event FROM events WHERE day = '$date'";
$sel = mysql_query($sql,$link);
echo $sel; //this prints Resource id #3
if (mysql_num_rows($sel) > 0)
{
while($row = mysql_fetch_array($sel))
{
echo $row['event'] . '<br />';
$arr[$i]=$row['event'];
$i=$i+1;
}
foreach($arr as $key => $value)
{
echo $key . " " . $value . "<br>";
}
} else echo 'Nothing returned!'; //prints this instead of the correct result
return $arr;
}
print_r( grabData() );
mysql_close();