Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have a website which logs the ip,time and date of every visit. I am trying to write a function that displays how many times a certain ip address has accessed the site. It will say something like you have visited this site .... times.
Here is the php:
<?php
function visitor_counter()
{
require_once 'php_scripts/log_in_script_2.php';
$conn = new mysqli($hn,$un,$pw,$db);
if ($conn->connect_error) die($conn->connect_error);
$ip_var = $_SERVER['REMOTE_ADDR'];
$query = "SELECT COUNT(ip) FROM visitors WHERE ip = '$ip_var'";
$result = $conn1->query($query);
if (!$result) die ($conn->error);
echo $result;
mysqli_close();
}
?>
it is included in the webpage with
<?php require_once "php_scripts/visit_counter.php"; ?>
and the function is called in the code with
You have visited my site: <?php visitor_counter(); ?> times.
It does not output anything, and the rest of the webpage doesn't load after this line. I know the query is correct, I've tested it in phpmyadmin, and I know the login for the function has the correct privilages. Any ideas?
I added the changes from the comments above, but a problem was still there. The problem was that I was trying to echo the query result directly. I changed the query to $query = "select count(ip) as total_visits from visitors where ip = '$ip_var'"; and changed the echo to echo $result->fetch_object()->total_visits;, and it works.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I created a connect.php file in my website that I've hosted in godaddy, but when I include this file in my index.php page. All the content in the page went blank.
this is my connect.php file code
<?php
$host = 166.62.10.223;
$con = mysqli_connect("host","root","");
if(mysqli_connect_errno())
{
echo "error".mysqli_connect_errno();
}
?>
I really need some help
Change your code from
$host = 166.62.10.223;
$con = mysqli_connect("host","root","");
To
$host = 166.62.10.223;
$con = mysqli_connect($host,"root","");
And You need a password too if you are working on live server and username might not be root So replace them by actual values.
Example
$user = "liveserver_username"; //which is created in godaddy mysql wizard section
$password = "liverserver_password"; //which is created in godaddy mysql wizard section
$host = 166.62.10.223;
$con = mysqli_connect($host,"$user","$password");
more information on how to create DB,User and password.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Okay so I am running into an issue with my code since no matter what I change it to, it stays on the index page?
Here is my code
<?php
// Maintenance mode
$system = (mysql_query("SELECT * FROM system"));
if($system['maintenance'] == 1)
{
header('Location: /maintenance');
exit;
}
?>
I want it so if maintenance = 1 it would send you to the maintenance.php page but it doesn't can anyone help me with this? I tried,
if(!$system['maintenance'] == 1)
which works but I have this code in my maintenance.php page
<?php
$system = (mysql_query("SELECT * FROM `system`"));
if(!$system['maintenance'] == 0)
{
header('Location: /index');
exit;
}
?>
What I thought would send me back to the index page once that in the database it shows it is a 0 or that's what I want it to do. Can anyone please explain why my code isn't working?
I am a beginner php, mysql, and C++ coder / programmer so please try to explain it the easiest way possible.
I fixed it thanks to #Script47.
New code and works
<?php
$system = mysql_query("SELECT * FROM system");
$results = mysql_fetch_array($system);
if($results['maintenance'] == 1)
{
header('Location: /maintenance');
exit;
}
?>
The reason I do not need an extension for maintenance is because I have it set in .htaccess to ignore them, but for everyone to know it's a php file.
You need to actually fetch your results from the database. Look in to the link I provided. I will update my post with code.
<?php
$system = mysql_query("SELECT * FROM system");
$results = mysql_fetch_row($system);
if($results[yourColumnKey] == 1)
{
header('Location: /maintenance.php');
exit;
}
?>
http://php.net/manual/en/function.mysql-fetch-row.php
Edit 1
Your code is prone to SQL injection, you are still using MySQL even though it has been deprecated, you should use either MySQLi or PDO with prepared statements.
Edit 2
You don't seem to be specifying a file extension on your header();.
header('Location: /maintenance');
--^
What is maintenance? A PHP file? A HTML file? You need to specify an extension too.
As you are a self-described beginner, it's probably best to learn how to do this using mysqli instead of using old deprecated code.
Below does essentially the same thing, but will last past the current version of php. It's a good habit to get into. (note if you are going to use any variables in your query, you will want to look up Prepared Statements
$conn = new mysqli($host, $username, $password, $dbname);
$system = $conn->query("SELECT maintenance FROM `system`");
while ($row = $system->fetch_assoc()) {
if($row['maintenance'] == 1) {
header('Location: /maintenance');
exit();
}
else {
header('Location: /index');
exit();
}
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I use Linux Debian
Local Lamp Server
i tried to make connection between Database and php :
<?php
$tf_host = 'localhost' ;
$tf_dbname = 'tinyforum' ;
$tf_username = 'root';
$tf_password = '' ;
$tf_handle = mysql_connect($tf_host , $tf_username , $tf_password);
if (!$tf_handle)
die('connection problem ..');
$tf_db_result = mysql_select_db($tf_dbname);
if($tf_db_result)
{
mysql_close($tf_handle);
die('selection problem');
}
die('OK');
mysql_close($tf_handle);
?>
The result :
http://www.foxpic.com/V0HrSdgb.png
pic of phpmyadmin:
http://www.foxpic.com/V0AlRhi6.png
the place where i save the php file :
/var/www/html/
Unless you copied your code incorrectly, your check is not going to result in what you want.
$tf_db_result = mysql_select_db($tf_dbname);
if($tf_db_result)
your selection seems fine so either you should change your code to
if(!$tf_db_result) //note the !
or restructure your code
if($tf_db_result) {
//Do the stuff you want to do when you are connected
} else {
//Die connection
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am in the stages of the class diagram, I was creating the class diagram for a website I am planning to create. It was going fine until I reached the stage of wanting to have a web page that displayed the online users e.g. showing their username and profile picture of all online users. I am not sure on how I would do this, the image is of what I have so far. I would appreciate any help or guidance.
Here is my current class diagram http://imgur.com/sgjJwkc
You could also set up a column for user's status (logged in, logged out) and make it toggle between 0 (logged out) and 1 (logged in). You could update this information every 5 seconds (in the background of course) using an AJAX call. Something like this:
//JAVASCRIPT
<script>
$(document).ready(function() {
setInterval(function() {
$.post('Path To PHP File', {x : Pass Variables, y: If You Want}, function(res)
//Do something with the result (res)
);
}, 5000);
});
</script>
//PHP FILE
<?php
//If you passed any variables to the script:
$x = $_POST['x'];
$y = $_POST['y'];
//Connect to your database
$dbConn = "I hope you're using PDO for this.";
//Create your query
$sql = "SELECT * FROM users WHERE status=1";
$res = $dbConn->prepare($sql);
$res->execute();
//Return/echo results
foreach($res as $x) {
echo "<div id='useTheIdToStyleTheResults'>".$x['name']."</div>";
}
?>
res is whatever your php script returns. You can simply run an SQL query on your database in that script to get all users who are logged in and use a foreach() loop to return each item as an html div element. Style those elements to your liking and there you go. If you have questions, just ask!
EDIT:
After reading a little more of your question, SQL JOIN and UNION are a couple of concepts you might want to look into.
http://www.w3schools.com/sql/sql_join.asp
EDIT #2:
//Define Variables
$hostname = '127.0.0.1';
$username = 'userName';
$password = 'passWord';
$dbname = 'database in use';
//Create Connection
try {
$con = new PDO("mysql:host=$hostname;dbname=$dbname",$username,$password);
$con->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
//echo "Connected to database"; //Uncomment statement to the left to check for connection
} catch (PDOException $e) {
print "Unable to connect: " . $e->getMessage();
mysql_close($con);
die();
}
?>
I would update a timestamp in the user's row every time they load a page and on the load of the online users I would check for timestamps that are fairly recent.
Pseudocode:
SELECT username,avatar FROM users WHERE last_active >= time()-900;
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am trying to add a resend verification action to my login/register system using tampus login system. I am following this tutorial.
Okay I am not a fan of php frameworks so I will make this one for you with MySQL:
if(empty($_POST["user"]) || empty($_POST["pass"])){
echo "Missing values";
} else {
//You would need the mysql connection variable named $db_conn
$db_conn = mysqli_connect("localhost","user","pass","db_name");
$user = $_POST["user"];
$pass = md5($_POST["pass"]);//MD5 encrypt;
$sql = "SELECT id FROM users WHERE username='$user' AND password='$pass'"
$query = mysqli_query($db_conn,$sql);
if(mysql_num_rows($query) > 1){
//Do the login thingys like cookies and redirects
} else {
echo "Check your credentials";
}
}
I think I could not made it easier and better to understand