Simple delete query based on variable - php

I want to delete the record when the id column is equal to $id. Would this work?
$query = "DELETE FROM $TableName WHERE id=$id";
As a side i also want to set the number of rows that can be deleted to a
limit of 1. I know i have to use limit but i am unsure on its parameters

You could do
mysqli_query = ($con, "DELETE FROM `$TableName` WHERE id='$id' LIMIT 1");
That should do it exactly my friend

Related

How to get columns values after insert using php mysql?

In this code, after insert values to DB.I am doing select query for selecting invoiceNo($sql1= "select invoiceNo from invoices order by invoiceID desc limit 1"; ).Instead of selecting from DB how to get InvoiceNo?
For eg: Assume two users are there.Two users inserts InvoiceID at a same time.While doing "select invoiceNo from invoices order by invoiceID desc limit 1";this will get last coming invoiceID .I need to get specific invoiceID (for particular user) .How to get it?
$query = "select * from invoices order by invoiceID desc limit 1";
$result = $link->query($query);
$row = $result->fetch_assoc();
$invoiceNo = $row["invoiceNo"];
$getinvoiceNo = str_pad($invoiceNo + 1, 4, 0, STR_PAD_LEFT); //inserting like 0000
$sql = "INSERT INTO invoices (invoiceNo)
VALUES ('$getinvoiceNo')";
if ($link->query($sql) === TRUE) {
//echo "1";
$sql1 = "select invoiceNo from invoices order by invoiceID desc limit 1";
$last_id = mysqli_insert_id($link, $sql1);
$result1 = mysqli_query($link, $sql1);
$row1 = mysqli_fetch_array($result1);
echo json_encode($row1);
} else {
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
mysql_close($link);
If i understand your question correctly, you are concerned about possible data corruption from the concurrent update of the record.
I think you should give a look to mysql SELECT ... FOR UPDATE syntax, it should do what you ask: lock the selected row until an update is fired. Then the lock will be released.
For example:
SELECT table_field FROM table_name WHERE table_id_field = id_param FOR UPDATE
will lock the selected row until
UPDATE table_name SET table_field = table_field + 1 WHERE table_id_field = id_param
If you're looking to prevent collisions in invoice numbers, all you need to do is create your table as
CREATE TABLE invoices (
invoiceID INTEGER NOT NULL AUTO_INCREMENT,
other columns . . .
PRIMARY KEY (invoiceID)
);
Then when you do your INSERT, don't insert the invoiceID and let MySQL do it.
This will ensure that each new invoice has a unique invoiceID.

how to get the id of last affected rows in php mysqldatabase?

My code
$query = "select * from others";
But it takes previous id . when I insert the first row , it takes the id value will be 0 , then again insert the second row, it takes the id value will be 1.
Please Use my simple code it will be helpful for you
$selectquery="SELECT id FROM tableName ORDER BY id DESC LIMIT 1";
$result = $mysqli->query($selectquery);
$row = $result->fetch_assoc();
echo $row['id'];

How to do two DB queries at once in PHP?

I have two queries but can I have them as just one
$sql = ("SELECT * FROM table WHERE title='$title' LIMIT 1");
$sql = ("UPDATE table SET views = views+1 where title='$title' ");
$query = mysql_query($sql) or die("Error Connecting To db");
At the moment the 2nd one works but not the first one.
This is your code:
$sql = ("SELECT * FROM table WHERE title='$title' LIMIT 1");
$sql = ("UPDATE table SET views = views+1 where title='$title' ");
$query = mysql_query($sql) or die("Error Connecting To db");
First, the first $sql will not run because you are overwriting the contents of that first $sql with new content in the next line.
Then just looking at your logic, here is your first query:
SELECT * FROM table WHERE title='$title' LIMIT 1
I assume that is to get the entry with the title equalling $title and nothing else, right? But then your next query is this:
UPDATE table SET views = views+1 where title='$title'
You are updating the value where title='$title' anyway. So the first query is not even needed. So problem solved, right? Well you might want to add the LIMIT 1 to the second query like this:
UPDATE table SET views = views+1 where title='$title' LIMIT 1
But honestly the logic of updating a DB based on whether an item title matches seems messy. What if two items have the same title? Yes, you are limiting things by 1 but by what criteria? If you have three items with the same title, which gets updated?
You need more differentiation for your app structure. But I think that is fair for what is essentially a newbie question.
Run the two queries separately
$sql1 = ("SELECT * FROM table WHERE title='$title' LIMIT 1");
$qry = mysql_query($sql1);
if (mysql_num_rows($qry) > 0)
{
while ($res = mysql_fetch_object($qry))
{
// -- contents --
}
$sql = ("UPDATE table SET views = views+1 where title='$title' ");
$query = mysql_query($sql) or die("Error Connecting To db");
}

Update table based on condition (While Loop)

So I am trying to update my table based on a singe parameter:
The dateEntered field must be blank.
And I want to randomly select 50 rows, and update the blank ownerID fields to "Tester"
Here is what I have:
<?php
include("includes/constants.php");
include("includes/opendb.php");
$query = "SELECT * FROM contacts WHERE dateEntered='' ORDER BY RAND() LIMIT 50";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_assoc($result)){
$firstid = $row['id'];
$query2 = mysql_query("UPDATE contacts
SET ownerID = 'Tester'
WHERE id = '$firstid'");
$result2 = mysql_query($query2) or die(mysql_error());
}
?>
It will update a single record, then quit and give me:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1' at line 1
The first part that selects the records works fine, its query2 that won't update all 50 records, just one. Maybe I am writing this wrong.
mysql_query needs only one time
$query2 = mysql_query("UPDATE contacts
SET ownerID = 'Tester'
WHERE id = '$firstid'");
$result2 = mysql_query($query2) or die(mysql_error());
to
$result2 = mysql_query("UPDATE contacts
SET ownerID = 'Tester'
WHERE id = '$firstid'");
These answers are spot on, so I will only add some additional information, and a suggestion. When you are querying mysql the first time, $query1 is being set to the result resource, which for
$query1 = mysql_query("UPDATE contacts SET ownerID = 'Tester' WHERE id = '$firstid'");
returns a result of 1 (Boolean TRUE), which is why your second query failed, cause "1" isn't a valid mysql query string. As Greg P stated, you can fix your current script by eliminating the secondary mysql query.
However, you could improve the script entirely, and make fewer sql calls, by using this.
<?php
include("includes/constants.php");
include("includes/opendb.php");
$query = "UPDATE contacts SET owenerID='Tester' WHERE dateEntered='' ORDER BY RAND() LIMIT 50";
$result = mysql_query($query) or die(mysql_error());

I am having problem in "fetching" only one entry from mysql

I want only one single data from that DB but I am not able to "take it out of" $res.
$sql = "SELECT * FROM `study_stuffs_extra`.`tid` ORDER BY `id` DESC LIMIT 1 ";
$res = query($sql);
$tid = $res['tid'];
I have also tried a while loop to do so, but "couldn't do it". Is there any other method to "do it"?
try
$sql = "SELECT * FROM `study_stuffs_extra`.`tid` ORDER BY `id` DESC LIMIT 1 ";
$res = mysql_query($sql);
$res=mysql_fetch_array($res);
$tid = $res['tid'];
SELECT tid FROM study_stuffs_extra ORDER BY `id` DESC LIMIT 1
Also, check what query returns. Is that a mysql result? the whole result set? a row? Do some print_r to see what you get. Check for db errors after executing queries.
You may need to subscript the first member of $res, assuming it is an array.
$firstRow = $res[0];

Categories