-1

I wanted to create a php script to delete a certain row from my mysql database, but it shows

error deleting record:Table 'test.school' doesn't exist.

 <?php
    $servername="localhost";
    $username="root";
    $password="";
    $dbname="test";
    $conn= new mysqli($servername,$username,$password,$dbname);

    if($conn->connect_error)
    {
     die("connection failed:".$conn->connect_error);
    }
     $sql="DELETE from school where rno='5'";
     if($conn->query($sql)===TRUE)
    {
     echo "Record deleted successfully";
    }
     else
    {
     echo "error deleting record:".$conn->error;
    }
     $conn->close();
    ?>
Qirel
  • 25,449
  • 7
  • 45
  • 62
  • 4
    The error is quite clear. There's no table `school` in your database `test` – Qirel Sep 07 '17 at 07:15
  • Actually there is indeed a table 'school' in my database 'test' – Khushboo sharma Sep 07 '17 at 07:18
  • Are you connecting to the correct DB then @Khushboosharma? Do you have the DB hosted in 2 places - and your connecting to the server that has the table and your PHP script is looking at another server. – ajtrichards Sep 07 '17 at 07:28
  • 1
    @Khushboosharma MySQL begs to differ. It wouldn't throw such an error without that being the actual reason of failure.. – Qirel Sep 07 '17 at 08:10

2 Answers2

1
if($conn->connect_error)
{
die("connection failed:".$conn->connect_error);
}
$sql="DELETE from school where rno='5'";
$query = mysqli_query($conn, $sql);
if($query)
{
    echo"Deleted Successfully..";
}
else 
{
echo"Failed..";
}

Try this one,maybe this one will work for you.

Prakhar Sood
  • 159
  • 1
  • 10
  • 1
    Why should anyone "*try this one*"? What did you change? Why would this work? Just because you use procedural instead of OOP? Always explain your answer, don't just dump code. – Qirel Sep 07 '17 at 08:11
  • Turns out it did work! As you can see this answer was accepted. But I will keep that in mind from the next time. Cheers! – Prakhar Sood Sep 07 '17 at 09:07
  • You should still edit your answer and explain *why* this worked out, so its more useful for other readers :-) – Qirel Sep 07 '17 at 09:10
0

Try running the query directly from your SQL client (PhpMyAdmin, Workbench or something similar), if that works, then there is something wrong with your connection.

This has happened to me once before, what I did was I just exported the database/table then deleted it from my client, then re-added the database/table. It usually resolves itself with that (maybe the table wasn't properly saved into the database or something).

Check if the table has been "renamed" by the client, like maybe there was an extra space added to the beginning of the name of the table or something similar.