I am trying to display last visit of a user in my page.
The commands seems ok, but it doesn't work.
I have tried everything.
DATABASE SELECTION-OK
TABLE SELECTION-OK
if($result)- EXECUTES
but the UPDATE and the details I can not fetch from database.
Here's the code:
$conn = mysqli_connect("localhost","root","","counter");
$qry = "SELECT * FROM nodupes WHERE ids_hash = 'ids_hash'";
$result = mysqli_query($conn,$qry);
if($result)
{
$data = mysqli_fetch_assoc($result);
$lastvisit = $data["lastvisit"];
$timex = time() - $lastvisit;
}
$curr_time = time();
$qry2 = "update nodupes set lastvisit='$curr_time' WHERE ids_hash='ids_hash'";
$result2 = mysqli_query($conn,$qry2);
What should I do? The lastvisit in the database always shows 0
Related
I am taking a users input and storing it in a database, however I want to be able to update the records if a user adds more information. So I want to search the database find the server with the same name and update the the last downtime and the number of downtimes.
$connect = mysqli_connect("localhost", "Username", "Password","Test_downtime");
if (!$connect)
{
die("Connection failed: " . mysqli_connect_error());
}else
{
echo "Connected successfully\n";
}
$servername = $_GET["server_name"];
$downtime = $_GET["downtime"];
$time_now = time();
$result = mysqli_query($connect, "SELECT COUNT(*) FROM `Test_downtime`.`Downtime` WHERE `Server_Name` = '$servername'");
$row = mysqli_fetch_array($result);
// If no downtime have been reported before
if ($row[0] == 0){
$sql = mysqli_query($connect, "INSERT INTO `Test_downtime`.`Downtime` (ID, Server_name, First_downtime, Last_downtime, Num_of_downtime,Total_downtime) VALUES (NULL, '$servername', '$time_now','$time_now',1,'$downtime'); ");
if ($sql==true) {
echo $servername . " has has its first downtime recorded\n";
}
}
//If users is already in the database
else{
$numdowntime = ($row["Num_of_downtime"] + 1);
$id = ($row["ID"]);
$sqlupdate = "UPDATE `Test_downtime`.`Downtime` SET `Num_of_downtime` = $numdowntime, `Last_downtime` = now() WHERE `Server_Name` = '$servername'";
if ($sqlupdate == TRUE) {
echo "Oh No! " . $servername . " has had ". $numdowntime ." of downtimes" ;
}
}
?>
The program works fine if the server is not already in the database, the problems arise if the server is already in the database. I get the message saying it has been updated yet nothing happens to the database. How do i make it so it search and updates the records for the searched item.
So nothing append since you do not execute the sql statement ^^
Take a look here :
$sqlupdate = "UPDATE `Test_downtime`.`Downtime` SET `Num_of_downtime` = $numdowntime, `Last_downtime` = now() WHERE `Server_Name` = '$servername'";
You need to use :
$sql = mysqli_query($connect, $sqlupdate);
Just after it in order to execute it.
Or at least change it to
$sqlupdate = mysqli_query($connect, "UPDATE `Test_downtime`.`Downtime` SET `Num_of_downtime` = $numdowntime, `Last_downtime` = now() WHERE `Server_Name` = '$servername'" );
Btw there is other problem but here is the main one [check out the other answer in order to found another one ]
you are fetching the result as indexed array
mysqli_fetch_array($result);
and here you are accessing results as associative array
$numdowntime = ($row["Num_of_downtime"] + 1);
change your query to
mysqli_fetch_assoc($result);
use
mysqli_num_rows($result);
to checking if you have any data
change
if ($row[0] == 0){}
to
if(mysqli_num_rows($result) ==0){}
A good approach for increasing a count in a column is using SQL to increase that.
$sqlupdate = mysqli_query($connect, "UPDATE `Test_downtime`.`Downtime` SET `Num_of_downtime` = (`Num_of_downtime` + 1), `Last_downtime` = now() WHERE `Server_Name` = '$servername'" );
This way you can skip your $numdowntime calculation, and the result is more accurate.
In your current setup, two users may fire the event at the same time, they both retrieve the same number from the database (ie. 9), both increasing it with one (ie. 10), and writing the same number in the database.
Making your count one short of the actual count.
SQL takes care of this for you by locking rows, and you are left with a more accurate result using less logic :)
You miss the mysqli_query() function, which actually queries the database.
$sqlupdate = mysqli_query("
UPDATE `Test_downtime`.`Downtime`
SET `Num_of_downtime` = $numdowntime, `Last_downtime` = now()
WHERE `Server_Name` = '$servername'"
);
im busy trying to set multiple fields in my DB and it is not working for some reason. Can you take a look and let me know where i have gone wrong? Thank you
<?php
error_reporting('E_ALL');
include 'db_header.php';
$id = $_GET['ID'];
$bronzeTokens = $_GET['bronzeTokens'];
$silverTokens = $_GET['silverTokens'];
$goldTokens = $_GET['goldTokens'];
$platinumTokens = $_GET['platinumTokens'];
$sql = "UPDATE Player SET bronzeTokens = $bronzeTokens, goldTokens = $goldTokens, silverTokens = $silverTokens, platinumTokens = $platinumTokens WHERE ID = $id";
$result = $conn->query($sql);
?>
Basically, I have been having some trouble with sending a request to a MySQL server and receiving the data back and checking if a user is an Admin or just a User.
Admin = 1
User = 0
<?php
$checkAdminQuery = "SELECT * FROM `users` WHERE `admin`";
$checkAdmin = $checkAdminQuery
mysqli_query = $checkAdmin;
if ($checkAdmin == 1) {
echo '<h1>Working!</h1>';
}else {
echo '<h1>Not working!</h1>';
}
?>
Sorry that this may not be as much info needed, I am currently new to Stack Overflow.
Firstly, your SQL query is wrong
SELECT * FROM `users` WHERE `admin`
It's missing the rest of the WHERE clause
SELECT * FROM `users` WHERE `admin` = 1
Then you're going to need fetch the result from the query results. You're not even running the query
$resultSet = mysqli_query($checkAdminQuery)
Then from there, you'll want to extract the value.
while($row = mysqli_fetch_assoc($resultSet))
{
//do stuff
}
These are the initial problems I see, I'll continue to analyze and find more if needed.
See the documentation here
http://php.net/manual/en/book.mysqli.php
You need to have something like user id if you want to check someone in database. For example if you have user id stored in session
<?php
// 1. start session
session_start();
// 2. connect to db
$link = mysqli_connect('host', 'user', 'pass', 'database');
// 3. get user
$checkAdminQuery = mysqli_query($link, "SELECT * FROM `users` WHERE `id_user` = " . $_SESSION['id_user'] );
// 4. fetch from result
$result = mysqli_fetch_assoc($checkAdminQuery);
// 5. if column in database is called admin test it like this
if ($result['admin'] == 1) {
echo '<h1>Is admin!</h1>';
}else {
echo '<h1>Not working!</h1>';
}
?>
// get all admin users (assumes database already connected)
$rtn = array();
$checkAdminQuery = "SELECT * FROM `users` WHERE `admin`=1";
$result = mysqli_query($dbcon,$checkAdminQuery) or die(mysqli_error($dbconn));
while($row = mysqli_fetch_array($result)){
$rtn[] = $row;
}
$checkAdminQuery = "SELECT * FROM `users` WHERE `admin`"; !!!!
where what ? you need to specify where job = 'admin' or where name ='admin'
you need to specify the column name where you are adding the admin string
I stuck in some code here E.G:
Database table :
IMG_ID|IMG_SRC|EXPIRES|ACTION
------------------------------
4 | st.jpg |12546564| temp
So i try to delete the image from DB and from directory path so far i got this :
function removeTempPic(){
$uploadsDirectory1 = $_SERVER['DOCUMENT_ROOT'].'test/uploads/temp/';
//remove after 10 minute if unused
$timeout = time()-TEMPIC_TIMEOUT*60;
$q = "SELECT * FROM picsmanager WHERE expires < $timeout AND action=temp";
mysql_query($q, $this->connection);
foreach ($row = mysql_fetch_array($q)) {
$q = "delete * from picsmanager where=??"
//#unlink($uploadsDirectory1.$uploadFilename);
}
}
So what i try to do is select all from database table , and where timeout expire in some row delete each image from db and from directory
But this wont work because i dont know how to make it properly , Thanks.
you have multiple errors, in PHP and SQL
// enclose the string "temp" in single quotes
$q = "SELECT * FROM picsmanager WHERE expires < $timeout AND action='temp'";
$result = mysql_query($q, $this->connection);
// you need the result, not $q again
while($row = mysql_fetch_array($result)){
// run a new query to delete the extracted image
mysql_query("delete from picsmanager where IMG_ID=" . $row['IMG_ID'] . " LIMIT 1", $this->connection);
// delete the corresponding file
unlink($uploadsDirectory1.$row['IMG_SRC']);
}
Also, don't use the mysql_* functions anymore, they are becoming deprecated and will be removed from PHP in the future. Your code will stop working then. Learn how to use mysqli_* or PDO objects.
function removeTempPic(){
$uploadsDirectory1 = $_SERVER['DOCUMENT_ROOT'].'test/uploads/temp/';
//remove after 10 minute if unused
$timeout = time()-TEMPIC_TIMEOUT*60;
$q = "SELECT * FROM picsmanager WHERE expires < $timeout AND action='temp'";
mysql_query($q, $this->connection);
foreach($row = mysql_fetch_array($q)){
$q = sprintf("delete * from picsmanager where IMG_ID = %d", (int)$row['IMG_ID']);
//#unlink($uploadsDirectory1.$uploadFilename);
}
}
I execute this query with php and odbc driver
$sql="DECLARE #Auftrag int;
DECLARE #date_now datetime = getdate();
EXEC #Auftrag=EHS.dbo.SP_ANZEIGE
#Tablet=1,
#Status=0,
#KuNr='K015538';
SELECT 'generatedID'=#Auftrag;";
$res = odbc_exec($db1_link, $sql) or die(odbc_errormsg()); // returns resource(13)
$firstRow = odbc_fetch_array($res); // dies error
If i do odbc_fetch_array the error "No tuples available at this result index" is thrown.
If I run the exact same query in Management Studio everything works fine. It shows me the computed generatedID. What is the difference?
greets Alex
Try to prefix the query with:
set nocount on
That prevents SQL Server from sending rowcount updates, which UNIX clients sometimes mistake for actual rowsets
I had this same problem. In my case I was executing like this
$sql = "SELECT * FROM table1";
$resultSet = odbc_exec($sqllink, $sql);
while ($data = odbc_fetch_array($resultSet)) {
$sql = "SELECT * FROM table2";
$resultSet2 = odbc_exec($sqllink, $sql);//failed here
while ($data2 = odbc_fetch_array($resultSet2)) {
//something here
}
}
and I changed like this and it worked
$sql = "SELECT * FROM table1";
$resultSet = odbc_exec($sqllink, $sql);
// Create an array and store the results
$queryResult = array();
while ($data = odbc_fetch_array($resultSet)) {
// push the required content into the array
$queryResult[$data['id']]['name'] = $data[name];
}
foreach($queryResult as $row) {
$sql = "SELECT * FROM table2";
$resultSet2 = odbc_exec($sqllink, $sql);
while ($data2 = odbc_fetch_array($resultSet2)) {
// something here
}
}