Check if a MySql database has been updated in PHP - php

I am trying to detect whether a MySql database has been updated the second it has been updated. So far I've just found triggers but it, https://www.siteground.com/kb/mysql-triggers-use/, said I need superuser privileges and, unfortunately, the domain I use is a shared server.
So currently there is one group working on inserting all user information into the database like so.
<form method="post">
-> user post data
</form>
<?php
-> post data to varibles, $x $y as examples
$servername = "localhost";
$username = "user";
$password = "pass";
$dbname = "name";
// Check connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
if(!$conn){
die("Connection failed: " . mysqli_connect_error());
}
$sql = "INSERT INTO Test (Col1, Col2)
VALUES (". $x .",". $y .")";
mysqli_close($conn);
?>
And then my group, in a separate file, is supposed to get the data from the database when it is updated. Our file set up looks like this.
<?php
$servername = "localhost";
$username = "user";
$password = "pass";
$dbname = "name";
// Check connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
if(!$conn){
die("Connection failed: " . mysqli_connect_error());
}
->trigger on column update
$sql = "SELECT Col1, Col2, time FROM Test";
$result = mysqli_query($conn, $sql);
->etc.
mysqli_close($conn);
?>
So far I've only found triggers that are in MySql but there doesn't seem to be a way to directly do it in PHP ex.
mysql> CREATE TRIGGER agecheck BEFORE INSERT ON people FOR EACH ROW;

Related

PHP MySQL doesn't perform update using substring

I am facing an issue with PHP which doesn't perform one query in my script.
The SQL query works well in my MYSQL console but nothing is happening. Year column stays NULL:
$UpdateYear='UPDATE `pat` SET `Year` = SUBSTRING(`Prepa`,7,4)';
mysqli_query($connWarehouse,$UpdateYear) or die(mysqli_error($connWarehouse));
I don't know what I am doing wrong. Here the full script:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$db="datawarehouse";
// Create connection
$connWarehouse = new mysqli($servername, $username, $password, $db);
// Check connection
if ($connWarehouse->connect_error) {
die("Connection failed: " . $connWarehouse->connect_error);
}
$UpdateYear='UPDATE `pat` SET `Year` = SUBSTRING(`Prepa`,7,4)';
mysqli_query($connWarehouse,$UpdateYear) or die(mysqli_error($connWarehouse));
mysqli_close($connWarehouse));
?>
I finally managed to solve. I don't know if it is a correct way to do it. I have to open a new connection to the database in order to perform.
alter.php:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$db="datawarehouse";
// Create connection
$connWarehouse = new mysqli($servername, $username, $password, $db);
// Check connection
if ($connWarehouse->connect_error) {
die("Connection failed: " . $connWarehouse->connect_error);
}
$AddYear='ALTER TABLE `pat` ADD COLUMN `Year` YEAR;';
mysqli_query($connWarehouse,$AddYear) or die(mysqli_error($connWarehouse));
mysqli_close($connWarehouse));
include 'uppat.php';
?>
uppat.php
<?php
require_once 'config-datawarehouse.php';
$UpdateYear='UPDATE `pat` SET `Year` = SUBSTRING(`DatePrepa`,7,4)';
mysqli_query($conn,$UpdateYear) or die(mysqli_error($conn));
mysqli_close($conn);
?>
On that way it performed the update query.

How to fix: Error- No database selected

SERVER RESPONSE
Error: INSERT INTO epiz_19848473_Liste1 (Rowcount, Level1) VALUES (0, 'any Value')
No database selected
PHP CODE
UPDATE
<?php
$servername = "sql308.epizy.com";
$username = "epiz_19848473";
$password = "huihuibuh";
$dbname = "Liste1";<--UPDATED
// Create connection
$conn = mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO Liste1 <--UPDATED (Rowcount, Level1)
VALUES (0, 'any Value')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
This should work, right?
NEW STATUS
Connection failed: Access denied for user 'epiz_19848473'#'192.168.0.%' to database 'Liste1'
The code is perfectly all right , the problem is with mysql configuration .
Please login to mysql as **root user ** and do the following,
GRANT ALL PRIVILEGES ON . TO 'epiz_19848473'#'192.168.0.%' (use ip of php server)
FLUSH PRIVILIGES;
The above allows the user to connect to mysql table from the ip specified.
you did not defined the database name in the connection
<?php
$servername = "sql308.epizy.com";
$username = "epiz_19848473";
$password = "huihuibuh";
$dbname = "DATABASE_NAME_HERE";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
else you can define define the database name in the connection
$conn = new mysqli($servername, $username, $password, "DATABASE_NAME_HERE");
Just define the dbname,like this:
<?php
$servername = "sql308.epizy.com";
$username = "epiz_19848473";
$password = "huihuibuh";
$dbname = "test"; //You should create this db in mysql
....
You Can Use This:
CREATE TABLE [database_name].[table_name] (
[your_table_things]
);

When I point my browser to example.com/test.php, PHP from example.com/index.php is also being loaded

I have looked for an answer to why this is doing this and I can't seem to figure it out. I am trying to make a simple stat counter, on my index.php I have
$fromstr = 'index';
$country = 'TEST';
$ip = $_SERVER['REMOTE_ADDR'];
$servername = "localhost";
$username = "*****";
$password = "*****";
$dbname = "*****";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "INSERT INTO counter (Country, IP, referrer)
VALUES ('$country', '$ip', '$fromstr')";
mysqli_query($conn, $sql);
$conn->close();
When I would visit index.php, it was adding the same entry into the database twice. I tried testing why it was happening so made an additional file test.php. I put almost the same code, just changing the $fromstr and leaving leaving the $country field blank. My code is:
$fromstr = 'test';
$ip = $_SERVER['REMOTE_ADDR'];
$servername = "localhost";
$username = "*****";
$password = "*****";
$dbname = "*****";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "INSERT INTO counter (Country, IP, referrer)
VALUES ('$country', '$ip', '$fromstr')";
mysqli_query($conn, $sql);
$conn->close();
When I visit test.php, it correctly adds the entry with Country->NULL IP->(ip address) referrer->"test" however it ALSO adds the query that the index.php is supposed to add (Country->"TEST" IP->(ip address) referrer->"index").
TL;DR: When I visit index.php my server executes the query to insert something into a database, however it (for some odd reason) does it twice. I made another file test.php to insert a different query and it inserts the correct query once then also the query on the index page, even though the index page is never visited. Why would it be doing this?
If this is your actual source code you have a syntax error already. You forgot to add a single quote after the word TEST in following line:
$country = 'TEST;

How to extract desired row from Mysql database using PHP script

I have created one HTML form which takes input from user ,Now I need to search user inputed name in Mysql database and print details related to that user inputed name which is stored in Mysql database.
Below script is creating HTML form to take user input, Saved as "ProcessTracking.html".
<form action="details.php" method="get"/>
<h3 align="center"><FONT color=#CCFF66>ENTER SO NUMBER</h3>
<p align="center">
<input type="text" id="SO_Number" name="SO_Number"/>
</p>
<div style="text-align:center">
<button type="submit" value="SEARCH">
<img alt="ok" src=
"http://www.blueprintcss.org/blueprint/plugins/buttons/icons/tick.png"/>
SEARCH
</button>
</form>
Below PHP script named as "details.php"
<?php
$userinput = $_GET['SO_Number'];
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "ProcessTrackingSystem";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_errno) {
printf("Connect failed: %s\n", $conn->connect_error);
exit();
}
$result = mysqli_query($conn, "SELECT * FROM ProcessTrackingSystem.ProcessDetails WHERE SO_Number = '$userinput'") or die(mysqli_error($conn));
$row = mysqli_fetch_assoc($result);
#printf ("SO_Number: %s \n",$row["SO_Number"])
#print_r($row);
printf ("SO_Number:");
printf($row["SO_Number"]);
printf ('--||--');
printf ("Name:");
printf($row["Name"]);
printf ('--||--');
$conn->close();
?>
Firstlly you are not using the $_GET['SO_Number'] parameter in a WHERE of SQL statement. Secondlly you are using both mysql and mysqli which are totaly diffrent and don't work together. For usage see mysqli_fetch_row() and mysqli_query(). Also use print_r($row);.
Here is the corrected code:
<?php
$userinput = $_GET['SO_Number'];
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "ProcessTrackingSystem";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_errno) {
printf("Connect failed: %s\n", $conn->connect_error);
exit();
}
$result = mysqli_query($conn, "SELECT * FROM ProcessTrackingSystem.ProcessDetails WHERE SO_Number = '$userinput'") or die(mysqli_error($conn));
$row = mysqli_fetch_row($result);
print_r($row);
$conn->close();
?>
EDIT: Added code example.
You have mixed mysql and mysqli api together. Try using either one.
Note: mysql api is deprectaed as of php 5.5.0
1st Error
As saty says it is because of the syntax error you have in this line
print_r"$row";
which should be as print_r($row)
2nd Error
You're mixing mysql & mysqli
I am not sure about the table that you have.
Recommendation :
I would recommend you to turn on the error_reporting if not those errors will be in your errors_log file
Also for debugging your sql, you can first construct your sql query, run in the phpmyadmin or related tools for your query, then fire the query and make this done.
Note :
If you are using these code in online then the error_log will be in the directory where you execute this page. (But it may change according to your hosting)
If you are running in local machine the error log may locate according to the server you use...
You can find by printing the php's configuration by phpinfo and find for
error_log
May this thing will fix your issue
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "ProcessTrackingSystem";
$so = $_POST['SO_Number'];
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$stmt = $conn->prepare("SELECT * FROM ProcessDetails WHERE SO_Number=$so");
$stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_assoc();
print_r($row[SO_Number]);
$conn->close();
?>

php access error using credentials that work in another script

I'm an iOS developer trying to update a bit 'o php to work with the mysqli_* stuff instead of the deprecated mysql_* stuff.
I know next to nothing about php/mysql, and I'm stuck. I'm using a script found at http://www.w3schools.com/php/php_mysql_select.asp, with changes to reflect my field names etc
When I call the following from a browser I get an access error (Connection failed: Access denied for user 'whoisit7'#'localhost' (using password: YES)). The server name, password and username etc I'm using all work with an existing script in a browser but not with this new one.
Can anyone point me at what I'm getting wrong?
<?php
$servername = "localhost";
$userName = "whoisit7_ios";
$password = "blahblahblah";
$dbName = "whoisit7_scathing";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT name, latitude, longitude FROM location where status = 1";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "name: " . $row["name"]. " - latlong: " . $row["latitude"]. " " . $row["longitude"]. "<br>";
}
} else {
echo "0 results";
}
mysqli_close($conn);
?>
In PHP, variable names are case-sensitive. So, Change your connection variable names to:
$servername = "localhost";
$username = "whoisit7_ios";
$password = "blahblahblah";
$dbname = "whoisit7_scathing";
Read it if you are new to PHP: http://php.net/manual/en/language.variables.basics.php
Php is case sensitive, so change the below line from:
$conn = mysqli_connect($servername, $username, $password, $dbname);
to
$conn = mysqli_connect($servername, $userName, $password, $dbName);
You have made error in $username which is $userName and $dbname which should be $dbName as you declared above.
Just change -->
localhost to 127.0.0.1 //you need to change params in mysql config if u want to use it as localhost
And
$username to $userName //As u need to keep these variables similar :)
this Problem seems to be coming from database privileges as i have also have encountered this before. Give this fix a try.
GRANT ALL PRIVILEGES ON database_name TO usernamehere#host IDENTIFIED BY 'yourpassword';
FLUSH PRIVILEGES;
Just change the lower case letters to your own credentials.

Categories