In my PHP code, i'm easily writing records to my database but for some reason i can't read anythign out. My PHP code is:
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM companies";
if ($conn->query($sql) === TRUE)
{
echo "query success";
while($row = $result->fetch_assoc())
{
echo "ID: " . $row["ID"]. " - Name: " . $row["name"]. "<br>";
}
}
else
{
echo "query failure";
echo "Error: " . $sql . "<br>" . $conn->error;
}
$sql = "INSERT INTO companies (name)
VALUES ('mycompany')";
if ($conn->query($sql) === TRUE)
{
echo "insert success";
}
else
{
echo "insert failure";
echo "Error: " . $sql . "<br>" . $conn->error;
}
The output I get from the browser when i run it is:
query failureError: SELECT * FROM companies
insert success
I've tried variations of apostrophes, carets, quotes in that $sql string. I've tried running this query in HeidiSQL and it works fine. Any ideas where I'm going wrong? Any suggestions of more basic stuff I can try to narrow down the source of the problem?
thanks!
if ($result = $conn->query($sql)). But that's not the source of your issue, take a look atmysqli_erroranderror_reporting(-1);.