Truncate table mysql php providing error [duplicate] - php

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 6 years ago.
I am receiving this error "Parse error: syntax error, unexpected 'TABLE' (T_STRING)" when trying to truncate a table before more data is entered.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "members";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
TRUNCATE TABLE users;
{
header ("Location: order.php");
exit;
}
mysqli_close($conn);
?>

You can't just put MySQL queries in plain text in PHP. Write it as a string inside mysqli_query() along with your current open connection as shown below:
mysqli_query($conn, "TRUNCATE TABLE `users`");
Your entire code should be:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "members";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
else {
mysqli_query($conn, "TRUNCATE TABLE `users`");
header ("Location: order.php");
exit;
}
mysqli_close($conn);
?>

Related

How to use PHP SQL with where Clause? [duplicate]

This question already has answers here:
How to include a PHP variable inside a MySQL statement
(5 answers)
Closed 2 years ago.
I want to select an email from the DB which needs to return the related Account_ID
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "connected accounts details";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT acc_id FROM users
WHERE
email = $user_email";
$result = $conn->query($sql);
echo "$result";
$conn->close();
This code returns nothing, just blank.
While using the same connection/db data is being inserted in db successfully.
Try this code
// Php Sql Select From DB....
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "connected accounts details";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT `acc_id` FROM `users` WHERE `email` = ?";
$result->$conn->prepare($sql);
$result->bind_param('s',$user_email);
$result->execute();
$data = $result->get_result();
// Fetch all
$data->fetch_all(MYSQLI_ASSOC);
print_r($data);
$mysqli -> close();
// #link http://php.net/manual/en/mysqli-result.fetch-all.php
$sql = "SELECT acc_id FROM users
WHERE
email LIKE '%$user_email%' ";
$result = mysqli_query($conn, $sql);
$row = $result->fetch_assoc();
$id = $row['acc_id'];
echo "$id";
$conn->close();
Got the wanted Record, trying this way.
Thanks everyone answering on this post.

How to display number of rows in a sql table using php?

I'm trying to get lines' number from the result of the sql query, so I started by connecting to the database, then checking if the connection was established and finally I tried to count the number of lines, here's the code :
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "db_test";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$res = $conn->query('SELECT COUNT (id_offre) AS nb FROM offres');
$data = $res->fetch();
$nb = $data['nb'];
echo $nb;
?>
I get these errors :
Uncaught Error: Call to a member function fetch() on boolean
Call to a member function fetch() on boolean
I used echo to check the 'nb' value, what is the problem?
Your query fails which makes $conn->query() to return false.
So, why does the query fail?
SQL doesn't allow for any spaces between function names and the parentheses, like you have at COUNT ().
So if you change:
SELECT COUNT (id_offre) AS nb FROM offres
to
SELECT COUNT(id_offre) AS nb FROM offres
...it should work.
Note: I would highly recommend you to have a read about mysqli::error() and add some error handling to your code.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "db_test";
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (! $conn) {
die("Connection failed: " . mysqli_connect_error());
}
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: ".$conn - > connect_error);
}
$res = $conn->query('SELECT * FROM offres');
$data_count = mysqli_num_rows($res);
echo($data_count);
?>
check this out!

SQL database connection in PHP successful, but I can't query it [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 6 years ago.
<?php
$servername = "localhost";
$username = "user";
$password = "pass";
$dbname = "test";
$tablename = "mapcoords";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error)
{
echo "Failure";
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
$sql = "SELECT (lat, lng) FROM mapcoords";
$result = $conn->query($sql);
while($row = $result->fetch_assoc())
{
echo "ok";
}
$conn->close();
?>
Here is the code. So like I said, it can connect successfully, but the code won't successfully query. What's weird is that if I copy and paste the same code, which seems to be EXACTLY the same, it works. It makes no sense. I can't find a single difference between their code and my code besides the way they space things and the way I space things. Here is their code:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT (lat, lng) FROM mapcoords";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo $row["lat"]. " " . $row["lng"] . "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
The problem is that this query:
SELECT (lat, lng) FROM mapcoords
returns the folloowing error:
[21000][1241] Operand should contain 1 column(s)
You have to change the query to
SELECT lat, lng FROM mapcoords

How To Get a Single Number From a Database in PHP [duplicate]

This question already has answers here:
Can I mix MySQL APIs in PHP?
(4 answers)
Closed 6 years ago.
I need to echo the number 1 or 0 whatever the database has in that cell. I cant seem to get it to work. What am I doing wrong people of the internet?
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "testfp";
$conn = new mysqli($servername, $username, $password, $dbname);
$sql = "SELECT hookup FROM testfp WHERE name = 100100" ;
$bob = mysql_query($sql);
echo $bob;
?>
You can not use mysql and mysqli together. So you can try the following code (using mysqli only, since mysql is deprecated) :
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "testfp";
$conn = new mysqli($servername, $username, $password, $dbname);
//Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT hookup FROM testfp WHERE name = 100100" ;
$result = $conn->query($sql);
if($result->num_rows>0)
{
while($row = $result->fetch_assoc()) {
echo $row["hookup"];
}
}
else
{
echo "No results found!";
}
?>

Why I can't run this query? [duplicate]

This question already has answers here:
Can I mix MySQL APIs in PHP?
(4 answers)
Closed 7 years ago.
I'm trying to run this code in php but does not print anything.
My database is not empty.
<?php
$dbname = "mytest";
$servername = "***";
$username = "mohsen";
$passwords = "****";
// Create connection
$conn = new mysqli($servername, $username, $passwords, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$User=$_POST['username'];
$sql="SELECT * FROM Money WHERE Debtor='$User' OR Creditor='$User' ";
$result = $conn->query($sql);
//
while($field=mysql_fetch_assoc($result))
{
print $field["Debtor"]."|";
print $field["Creditor"]."|";
print $field["Cost"]."|";
print $field["Status"]."|"."\n";
}
$conn->close();
?>
EX:
I have record with this user in my database
User=mohsen
link:
http://test.kholaseketab.ir/Update.php
You are mixing object oriented and procedural style query.
<?php
$dbname = "mytest";
$servername = "***";
$username = "mohsen";
$passwords = "****";
// Create connection
$conn = new mysqli($servername, $username, $passwords, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$User=$_POST['username'];
$sql="SELECT * FROM Money WHERE Debtor='$User' OR Creditor='$User' ";
$result = $conn->query($sql);
//
while($field = $result->fetch_object())
{
print $field->Debtor."|";
print $field->Creditor."|";
print $field->Cost."|";
print $field->Status."|"."\n";
}
$conn->close();
?>
<?php
$dbname = "mytest";
$servername = "***";
$username = "mohsen";
$passwords = "****";
// Create connection
$conn = new mysqli($servername, $username, $passwords, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$User=$_POST['username'];
$sql="SELECT * FROM Money WHERE Debtor='$User' OR Creditor='$User' ";
$result = $conn->query($sql);
while($field=$result->fetch_assoc())
{
print $field["Debtor"]."|";
print $field["Creditor"]."|";
print $field["Cost"]."|";
print $field["Status"]."|"."\n";
}
$conn->close();
?>

Categories