Please help me with this simple SQL code

A

Anonymous

Guest
Hi!

I need to get all the data out from the database. Maybe I have to get the data into a variable and then I can do stuff with it? (I'm doing stuff with the data in C#)

<?php

$con = mysqli_connect('topsecret');

if (mysqli_connect_errno())
{
echo "connection failed";
exit();
}

$sql = "SELECT * FROM data;";
mysqli_query($con, $sql) or die("Failed to get data");

echo "OK";

?>
 
The "data" is a reserved mysql keyword (https://dev.mysql.com/doc/refman/8.0/en/keywords.html).
To be sure the query is valid use this syntax:
Code:
SELECT * FROM `data`
 
Well, you need to update your code with the below one.

<?php

$con = mysqli_connect('topsecret');

if (mysqli_connect_errno()) {
echo "Connection failed";
exit();
}

$sql = "SELECT * FROM data;";
$result = mysqli_query($con, $sql) or die("Failed to get data");

while ($row = mysqli_fetch_assoc($result)) {
// Process each row of data
$id = $row['id'];
$name = $row['name'];
// Retrieve other columns as needed

echo "ID: $id, Name: $name <br>";
}

echo "Data retrieval successful";

mysqli_close($con);

?>


Thanks
 
Back
Top