0

Probably it was similar question but I didn't find the right answer. I want to delete from table all rows with the same name but I want to get this name by ID. Here's what I'm doing in php:

if ($stmt = mysqli_prepare($link, "SELECT FROM projects WHERE id=?")) {

mysqli_stmt_bind_param($stmt, "s", $projectId);
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $project_name);

   }


while( $row = mysqli_stmt_fetch($stmt) ){

         $stmt = mysqli_prepare($link, "DELETE FROM projects WHERE name=?");
         mysqli_stmt_bind_param($stmt, "s", $project_name);
          mysqli_stmt_execute($stmt);
   }

It doesn't work for some reason.

user2867504
  • 95
  • 1
  • 9
  • 2
    `SELECT FROM`...you forgot to specify any columns to select – ADyson Mar 22 '21 at 08:05
  • 1
    P.s. if you turn on mysqli error reporting it'll tell you the problem immediately – ADyson Mar 22 '21 at 08:06
  • 4
    P.s. why don't you just delete by ID if you already know the ID? Or could there be other rows with the same name which you also want to delete? – ADyson Mar 22 '21 at 08:07
  • 1
    You might also want to have a look at https://stackoverflow.com/questions/4562787/how-to-delete-from-select-in-mysql – Nigel Ren Mar 22 '21 at 08:08
  • 2
    it seems to be weird why you'd have to delete by name when you already have the id to target the row you'd want to be removed – Kevin Mar 22 '21 at 08:10
  • There are several rows with the same name but different IDs. I need to delete all instances with the same name, but I pass the ID in order to avoid issues with names – user2867504 Mar 22 '21 at 08:56
  • Ok, I changed this one: "SELECT FROM projects WHERE id=?" to "SELECT name FROM projects WHERE id=?" but still the same – user2867504 Mar 22 '21 at 09:03
  • See my second comment...get yourself an error message. And/or do some debugging and ensure your query actually selected some rows – ADyson Mar 22 '21 at 09:10
  • Also take note of the comments mentioning that you don't need two separate queries for this – ADyson Mar 22 '21 at 09:12
  • You are getting out of sync error. Use buffered queries instead. – Dharman Mar 22 '21 at 10:56
  • Enable error reporting https://stackoverflow.com/questions/22662488/mysqli-fetch-assoc-expects-parameter-call-to-a-member-function-bind-param – Dharman Mar 22 '21 at 11:01

0 Answers0