Simple Addition in php? [duplicate] - php

This question already has answers here:
How to increment value in MySQL with PHP mysqli
(4 answers)
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 5 years ago.
Ok so I am wondering if there is a simple way to make Addonetothis in my code to add it's current INT + 1 whenever this code is ran?
<?php
$server = "localhost";
$user = "**********";
$pass = "**********";
$dbname = "**********";
//Creating connection for mysqli
$conn = new mysqli($server, $user, $pass, $dbname);
//Checking connection
if ($conn->connect_error) {
die("Connection failed:" . $conn->connect_error);
}
$article_id = $_GET['id'];
if ( ! is_numeric($article_id))
die("Looks like you are lost! <a href='#'>Back to Home</a> ");
$sql = "UPDATE Example SET addonetothis='+ 1' WHERE `ID` =$article_id";
if ($conn->query($sql) === TRUE) {
header("Refresh:1; url=example.php?id=$article_id");
echo "Thank you!";
} else {
echo "Error" . $sql . "<br/>" . $conn->error;
}
$conn->close();

So you would have seen that...
$sql = "UPDATE Example SET Addonetothis='+ 1' WHERE `ID` =$article_id";
will blow up if Addonetothis is defined as an integer in your Database table as you are setting it to a string.
What you are looking for is something like...
$sql = "UPDATE Example SET AddOneToThis = AddOneToThis + 1 WHERE `ID` =$article_id";

Related

error in passing multiple parameters in MySQL stored procedure from php [duplicate]

This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 4 years ago.
i have an stored procedure InsertStudent in MySQL DB which is working fine from database,
now i am calling the above Sp from php by giving all the parameter its giving following error
Error: CALL InsertStudent(Mohd Maaz,455,1,2,0)
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server
version for the right syntax to use near 'Maaz,455,1,2,0)' at line 1
here is my code
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "funed";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$StudentName = "Mohd Maaz";
$StudentClass = 1;
$StudentRollNo = 455;
$StudentSection = 2;
$StudentIsdltd = 0;
$sql = "CALL InsertStudent(".$StudentName.",".$StudentRollNo.",".$StudentClass.",".$StudentSection.",".$StudentIsdltd.")";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
You just forgot to quote the parameters
replace
$sql = "CALL InsertStudent(".$StudentName.",".$StudentRollNo.",".$StudentClass.",".$StudentSection.",".$StudentIsdltd.")";
with
$sql = "CALL InsertStudent('".$StudentName."','".$StudentRollNo."','".$StudentClass."','".$StudentSection."','".$StudentIsdltd."')";
or I prefer to use the variables in the string directly without concatenating
$sql = "CALL InsertStudent('$StudentName','$StudentRollNo','$StudentClass','$StudentSection','$StudentIsdltd')";

Returns Internal Server Error (500) after connecting to MySQL database [duplicate]

This question already has an answer here:
PHP: 500 Error to error page
(1 answer)
Closed 5 years ago.
I want to select data from a MySQL database with PHP. Problem is, that when I try to echo out the $result, I get a 500 Error. When I leave out the echo $result;, I get a 200 OK return.
You guys gut any ideas?
Here's the PHP:
$q = $_GET['q'];
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "test";
//establish connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check Conncetion
if(!$conn) {
die("Connection failes: " . mysqli_connect_error());
}
$sql = "SELECT * FROM phptesting";
$result = mysqli_query($conn, $sql);
echo $result;
/*
while($row = mysqli_fetch_assoc($result)) {
echo "id: " . $row["id"]. "- Name: " . $row["first_name"]. " " . $row["last_name"]";
}
*/
mysqli_close($conn);
For your info, $q is just an integer with an id for testing it out.
echo is used to output primitive data type such as String, Integers.
$result holds the metadata of your SQL query result.
In ur code ur trying to echo the metadata. And This caused PHP fatal error.

error in parsing parameter using php [duplicate]

This question already has answers here:
What is the difference between single-quoted and double-quoted strings in PHP?
(7 answers)
Closed 5 years ago.
This code showing the output as :
string(59) "{"status":{"message":"error parsing
parameter","value":14}}"
but when instead of "file_get_contents" only "echo" is used, it shows correct url as output : http://ws.geonames.org/countryCodeJSON?lat=20.992&lng=73.314&username=****
what is going wrong here ?
<?php
$servername = "localhost";
$username = "*******";
$password = "*************";
$dbname = "id1116502_kk";
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//remove limit 1 is you want multiple data.
$sql = "SELECT degree_n, minute_n,degree_e, minute_e FROM coordinates ORDER BY id DESC limit 1 ";
$result = $conn->query($sql);
$deg_e = "";
$min_e = "";
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$deg_n = $row["degree_n"];
$min_n = $row["minute_n"];
$deg_e = $row["degree_e"];
$min_e = $row["minute_e"];
$url = file_get_contents('http://ws.geonames.org/countryCodeJSON?lat=$deg_n.$min_n&lng=$deg_e.$min_e&username=****');
var_dump($url);
}
} else {
echo "0 results";
}
$conn->close();
?>
you are using single quotes in your file_get_contents
// change this to double quotes
$url = file_get_contents('http://ws.geonames.org/countryCodeJSON?lat=$deg_n.$min_n&lng=$deg_e.$min_e&username=krunal123');
for more info check this post

"SELECT email FROM table WHERE name=$name"; in PHP [duplicate]

This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 6 years ago.
I am facing trouble printing the details of a username from MYSQL. The quotes in WHERE name = "xxx" is the cause.
This is the code:
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT name, email FROM MyTable WHERE name=$name";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["name"]. " - email: " . $row["email"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
How do I replace the WHERE name = $name?
you are missing an ''
should be:
WHERE name = '$name'
notice the quotes
I think you shoudl use this notation (previously be sure of a proper sanitize of $name for preventing potential SQL injection)
"SELECT name, email FROM MyTable WHERE name='$name'";

Resolve MySQLi empty query error [duplicate]

This question already has an answer here:
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(1 answer)
Closed 6 years ago.
Warning received:
Warning: mysqli::query(): Empty query in C:\wamp\www\otp-task\welcome.php on line 62
My database connection is here:
function insertDB($email, $OTP , $phonenumber )
{
$servername = "localhost";
$username = "root";
$password = "";
$db = "dbotp";
$conn = new mysqli($servername, $username, $password, $db);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// insert to db and close the connection, default time attribute is 60 mins
// the table name is onetime and will hold the otp and phonenum and email
$res = mysqli_query($conn, "INSERT INTO onetime (otpnum,email,phoneNum,action) VALUES ('$OTP' , '$email' , '$phonenumber','success')");
// $res = "INSERT INTO onetime (otpnum,email,phoneNum,action) VALUES ('$OTP' , '$email' , '$phonenumber', 'success')";
if ($conn->query($res) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $res . "<br>" . $conn->error;
}
$conn->close();
}
The following two is just a different format for the same thing:
$conn->query(...)
and
mysqli_query($conn, ...)
Once you executed one of them, use the return value of it instead of re-executing the query:
$res = $conn->query(...)
if ($res) {
// ...
}
Please also read about prepared statements: http://php.net/manual/en/mysqli-stmt.prepare.php

Categories