PHP MySQL doesn't perform update using substring - php

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.

Related

Check if a MySql database has been updated in 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;

Accessing a MySQL database with php file

I have a MySQL database and I am trying to figure out how to access it with a php file. I keep getting an error, so I was wondering if that was because my hostname is incorrect. If so, could anyone help me identify exactly what it is? The database is hosted on this website: https://christopherliao2002.000webhostapp.com/.
Below is my HTML code.
<?php
//Step1
$db = mysqli_connect('localhost','***********','password','******')
or die('Error connecting to MySQL server.');
?>
<html>
<head>
</head>
<body>
<h1>PHP connect to MySQL</h1>
<?php
//Variables
$servername = "localhost";
$username = "*****";
$password = "****";
$dbname = "******";
//Connection to database
$conn = new mysqli($servername, $username, $password, $dbname);
//Test connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
</body>
</html>
Try this format
$servername = "localhost"; // This is fixed for 000webhost.
$username = "id***_name";
$password = "****";
$dbname = "id***_name";
Try this without the try / catch:
//Variables
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database_name";
//Connection to database
$conn = new mysqli($servername, $username, $password, $dbname);
//Test connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());}
Reference: https://www.w3schools.com/php/php_mysql_connect.asp
Here the problem is with your hostname. Normally the hostname will be 'localhost' and in 000webhost the hostname is something like mysql#.000webhost.com and '#' needs to be replaced with the number for that you need to check your hosting providers members area.
And please don't post your username and password publicly
$db = mysqli_connect('mysql#.000webhost.com','xxxxx','password','xxxxxx')
or die('Error connecting to MySQL server.');
?>

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

Deletion of row in MySQL using php

I have been developing a CRUD application using PHP & MySQL database.
I was succeeded by creating, displaying, updation parts. But I stuck at the deletion part of a row from a database table.
I tried my best solving all the PHP shown errors but now in final it is now showing a message which I wrote to echo in case of failure.
I request someone to please help me with this problem.
Thankyou in advance.
Code I wrote for deletion:
//include database connection
include 'db_connect.php';
//$mysqli->real_escape_string() function helps us prevent attacks such as SQL injection
$query = "DELETE
FROM `grocery`
WHERE `GrocerID` ='".$mysqli->real_escape_string($_GET['id'])."'
limit 0,1";
//execute query
if( $mysqli->query($query) ){
//if successful deletion
echo "User was deleted.";
}else{
//if there's a database problem
echo "Database Error: Unable to delete record.";
}
$mysqli->close();
?>
Code I wrote for delete link in display table:
//just preparing the delete link to delete the record
echo "<a href='delete.php?id={$GrocerID}'>Delete</a>";
Code I wrote for db config:
<?php
//set connection variables
$host = "localhost";
$username = "root";
$password = "secret";
$db_name = "crud"; //database name
//connect to mysql server
$mysqli = new mysqli($host, $username, $password, $db_name);
//check if any connection error was encountered
if(mysqli_connect_errno()) {
die("Connection failed: " . $conn->connect_error);
exit;
}
?>
I tried this and got working, can you update the code and see if this works?
$host = "localhost";
$username = "root";
$password = "secret";
$db_name = "crud"; //database name
//connect to mysql server
$mysqli = new mysqli($host, $username, $password, $db_name);
//check if any connection error was encountered
if(mysqli_connect_errno()) {
die("Connection failed: " . $conn->connect_error);
exit;
}
// Delete row
if ($mysqli->query (sprintf( "DELETE FROM grocery WHERE email = '".$mysqli->real_escape_string($_GET['id'])."' LIMIT 1") )) {
printf ( "Affected Rows %d rows.\n", $mysqli->affected_rows );
}
I hope this helps.
Provide a connection :
if( $mysqli->query($con, $query) ){

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