Mysqli Update not working properly - php

$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "UPDATE Cube SET xValue=15 WHERE Index=1";
mysqli_query($conn, $sql);
mysqli_close($conn);
This seems like it should be pretty straight forward, but for some reason, the xValue field won't change, and I get no errors whatsoever. Been trying this for too long.

as u_mulder said, index is a reserved word in mySQL so you got 2 options:
$sql = "UPDATE Cube SET xValue=15 WHERE Cube.Index=1";
or
$sql = "UPDATE Cube SET xValue=15 WHERE `Index`=1";

Related

Mysql - Select WHERE AND - VARIABLE does not work

I have the following problem:
//$sql = "SELECT name, number, letter FROM queues WHERE number='100' AND letter='S'";
$sql = "SELECT name, number, letter FROM queues WHERE number='".$contentnumber."' AND
letter='".$contentletter."'";
See both queries above.
Both give the following out when I execute: var_dump($sql);
string(121) "SELECT name, number, letter FROM queues WHERE number='100' AND letter='S'"
Output by both is the same but with the version of the variables data from the database is not loaded. But with the version where I put the values for numbers and letters, it works fine.
Does anyone know where the problem or the mistake is?
Here is a little bit detailed:
ob_start();
echo "{{tokens[0].token_letter}}";
$contentletter = ob_get_clean();
ob_start();
echo "{{tokens[0].token_number}}";
$contentnumber = ob_get_clean();
$conn = mysqli_connect($serverName, $userName, $password, $dbName);
if (!$conn) { die("Connection failed: " . mysqli_connect_error()); }
$sql = "SELECT name, number, letter FROM queues WHERE
number='".$contentnumber."' AND letter='".$contentletter."'";
var_dump($sql);

Updated a MySQL table with PHP

This seems so easy. My code checks out, and even if I run the resetting fuction in phpMyAdmin it works, but for some reason the table never updates when I run the php.
<?php
$servername="localhost" ;
$username="***" ;
$password="***" ;
$dbname="***" ;
$conn=new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT `Alert` FROM `poonpad_daveAlert` WHERE 1";
$result = $conn->query($sql);
if ($result == 1) {
echo "wow, stuff is happening. resetting."; resetting();
} else {
echo "this place is dead";
}
function resetting() {
$sql = "UPDATE `poonpad_daveAlert` SET `Alert`=2 WHERE 1";
}
$conn->close();
?>
First, I'd use the below to execute the query.
$result = mysqli_query($conn,$sql);
Second, instead of (result==1) which I'm not sure is intended for what purpose, you should use (mysqli_num_rows($result)>0).
Third, for the resetting function, I think you need to mention the column name for the WHERE condition which you set to 1 as well as the main $sql query, and you don't need the WHERE if you have to select all rows.
"UPDATE `poonpad_daveAlert` SET `Alert`=2 WHERE [column-name]=1";

php Update sql and Query

I want to do a query in php, output the data on the page and then modify it in the database.
How do I do that?
Currently I do it like this but it dose not work:
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM pics WHERE id = '$id'";
$result = $conn->query($sql);
// output data of each row
while($row = $result->fetch_assoc()) {
$dir = $row["dir"];
$likes = $row["likes"];
}
$sqlq = "UPDATE pics SET likes='$likes+1' WHERE id='$id'";
$conn->query($sqlq);
$conn->close();
But the like dose not add to the database.
If you echo your $sqlq out using
echo $sqlq;
you'll see that the '$likes+1' isn't doing what you expect.
You could really simplify it by doing
$sqlq = "UPDATE pics SET likes=likes+1 WHERE id='$id'";
which removes any risk of two users updating the database at teh same time overwriting each other.
But you should really check out using "parameterized queries" as that would solve all your problems (and may your queries safer). Check the examples in the manual http://php.net/manual/en/mysqli-stmt.bind-param.php

failed in count rows in php sql

Thank you in advance!
Please tell me! what is mistake in this code. I want to show total rows of sql on my site. I searched many of codes be like this but all failed. Please help me.
$servername = "localhost";
$username = "root";
$password = "";
// Create connection
$conn = new mysqli($servername, $username, $password, 'sahiwalservices');
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "hunnysheikh99";
;
mysqli_select_db("sahiwalservices", $conn);
$result = mysqli_query("select count(1) FROM login");
$row = mysqli_fetch_array($result);
$total = $row[0];
echo "Total rows: " . $total;
mysqli_close($conn);
Posting as a community wiki; I don't want rep from this.
mysqli_select_db() - The syntax for that is:
Db connection comes first
Database name is second
Yet, you don't need that line since you're already passing that in:
$conn = new mysqli($servername, $username, $password, 'sahiwalservices');
Then, you didn't pass db connection to the query.
Example from the manual:
if ($result = mysqli_query($link, "SELECT Name FROM City LIMIT 10"))
Reference:
http://php.net/manual/en/mysqli.query.php
Check for errors on the query also:
http://php.net/manual/en/mysqli.error.php

Find out the difference in days between two dates taken from two columns in a database and place that difference into a variable for use in php

I thought mysql could calculate it so I made a variable called $sqlDateDiff.
$servername = "localhost";$username = "root";$password = "";$dbname = "maplibrary";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sqlDateDiff = "SELECT DATEDIFF(BorrowedDate, ReturnedDate)
FROM bookstakenout WHERE BorrowNumberID = $bnid" ;
$result = mysqli_query($conn, $sqlDateDiff);
Then I would like to use $result to create a nested if statement. How can I store the difference in a variable to use in a if statement.
In the bookstakenout table the ReturnedDate/BorrowedDate are both yyyy-mm-dd
Blockquote
Query: DATEDIFF(datepart,start date,end date ) as DIFFDATE

Categories