PHP Parse: Syntax Error with echo "\PDOStatement::errorInfo():\n - php

I am trying to create a page that will delete a row from a database but I keep getting syntax error and when I fix one I get a new error. For example, in the first code I tried re-typing the single quotation and it fixed the first error which was:
PHP Parse error: syntax error, unexpected ''];' (T_CONSTANT_ENCAPSED_STRING), expecting ']' in /banking_delete.php on line 11
The Code:
<?php
include_once 'banking_db.php';
include 'banking_display.php';
# form data
$customer_name=$_POST['customer_name'];
$sql = "delete from customer where customer_name = :customer_name;";
$stmt = $conn->prepare($sql);
# data stored in an associative array
$data = array ('customer_name' => $customer_name);
if($stmt->execute($data)){
$rows_affected = $stmt->rowCount();
echo "<h2>".$rows_affected." row deleted sucessfully!</h2>";
display("select customer_name as customer_name, customer_city as customer_city, customer_street as customer_street from customer;”);
} else
{
echo "\PDOStatement::errorInfo():\n";
print_r($stmt->errorInfo());
}
$stmt = null;
$conn = null;
?>
After that I got this new error:
PHP Parse error: syntax error, unexpected '\' (T_NS_SEPARATOR) in /banking_delete.php on line 16
When I changed echo "\PDOStatement::errorInfo():\n"; to echo "\nPDOStatement::errorInfo():\n"; I still get the same error message

You've got a ” instead of a " at the end of
display("select customer_name as customer_name, customer_city as customer_city, customer_street as customer_street from customer;”);

Related

get value of mysql colum with numbers in name [duplicate]

This question already has answers here:
How can I access an object attribute that starts with a number?
(3 answers)
Closed 2 years ago.
I would like to get a value of my mysql table.
the column name has got numbers.
$temp= array();
$sql = "SELECT `00003A` FROM `table`";
$statement = $mysqli->prepare($sql);
$statement->execute();
$result = $statement->get_result();
while($row = $result->fetch_object()) {
temp[] = $row->00003A;
}
I get this error:
Parse error: syntax error, unexpected '00003' (T_LNUMBER), expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$' on line 19
This is line 19:
temp[] = $row->00003A;
You can try save this value in a variable like:
$prop = '00003A';
temp[] = $row->$prop;
Or get data with
temp[] = $row['00003A'];

Extract associative array index name to automatch the database column name [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 7 years ago.
Below is my code -
$insert_details = array("username"=>"pavan", "firstname"=>"pavan", "lastname"=>"r", "profile_about"=>"My name is Pavan R.");
$connection->insert($insert_details);
public function insert(array $insert_details) {
$insert_query = "INSERT INTO user (username,firstname,lastname,profile_about) VALUES ($insert_details['username'],$insert_details['firstname'],$insert_details['lastname'],insert_details['profile_about'])";
$run_insert_query = mysqli_query($this->mysql_con, $insert_query);
if ($run_insert_query) {
$select_query = "SELECT * FROM user ORDER BY id DESC LIMIT 1";
$run_select_query = mysqli_query($this->mysql_con, $select_query);
while ($selected_row = mysqli_fetch_array($run_select_query)) {
$id = $selected_row['id'];
$username = $selected_row['username'];
$firstname = $selected_row['firstname'];
$lastname = $selected_row['lastname'];
$profile_about = $selected_row['profile_about'];
}
$es_insert = array();
$es_insert['body'] = array('id' => $id, 'username' => $username, 'firstname' => $firstname, 'lastname' => $lastname, 'profile_about' => $profile_about);
$es_insert['index'] = 'test';
$es_insert['type'] = 'jdbc';
$es_insert['id'] = $id;
$check_insert = $this->es_con->index($es_insert);
if($check_insert) {
echo nl2br("Successfully inserted to both database and elasticsearch\n");
}
}
else {
echo nl2br("Failed to insert into database hence closing the connection\n");
}
}
When I run the code I get the following error -
PHP Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in /var/www/html/es/combined.php on line 38
This is because of the SQL query ($insert_query). Can someone please help me debug this?
Also is there a way to extract the index names from array and pass it to database fields.In the above code, I've declared an associative array with index names same as my database column names. Is it possible to get those array index names and optimize the SQL query to just -
$insert_query = "INSERT INTO user VALUES ($insert_details['username'],$insert_details['firstname'],$insert_details['lastname'],insert_details['profile_about'])";
It should automatically extract the suitable column names from the array index name.
You cannot quote array keys in "-quoted strings:
php > echo "$arr['foo']";
PHP Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in php shell code on line 1
Either go with
$sql = "... $insert_details[username] ..."
^-------^---no quotes
or
$sql = "... {$insert_details['username']} ..."
^---------------------------^---brace syntax
And note that you are vulnerable to sql injection attacks.

PHP MySQL Request Error: Parse error: syntax error, unexpected '%'

I have a query on my PHP code:
$result = mysqli_query($con,"UPDATE operasyonkayitlari SET tarihgun=FROM_UNIXTIME(UNIX_TIMESTAMP(NOW())-28800, "%Y.%m.%d"), tezgah='".$_REQUEST['tezgah']."', operatoradi='".$_REQUEST['operator']."', ayarepoch=UNIX_TIMESTAMP(NOW()), durum='AYARDA' where isemri='".$_REQUEST['isemri']."' and operasyonno='".$_REQUEST['operasyonno']."'");
Look at this closely:
FROM_UNIXTIME(UNIX_TIMESTAMP(NOW())-28800, "%Y.%m.%d")
The characters with % symbol gives me this syntax error:
Parse error: syntax error, unexpected '%' in C:\wamp\www\ayarabasla.php on line 4
How did I write the query with % symbols?
The syntax highlighter show your error. It's a quote issue. Escape your inner double quotes:
$result = mysqli_query($con,"UPDATE operasyonkayitlari SET tarihgun=FROM_UNIXTIME(UNIX_TIMESTAMP(NOW())-28800, \"%Y.%m.%d\"), tezgah='".$_REQUEST['tezgah']."', operatoradi='".$_REQUEST['operator']."', ayarepoch=UNIX_TIMESTAMP(NOW()), durum='AYARDA' where isemri='".$_REQUEST['isemri']."' and operasyonno='".$_REQUEST['operasyonno']."'");
You can use single quotes, too:
$result = mysqli_query($con,"UPDATE operasyonkayitlari SET tarihgun=FROM_UNIXTIME(UNIX_TIMESTAMP(NOW())-28800, '%Y.%m.%d'), tezgah='".$_REQUEST['tezgah']."', operatoradi='".$_REQUEST['operator']."', ayarepoch=UNIX_TIMESTAMP(NOW()), durum='AYARDA' where isemri='".$_REQUEST['isemri']."' and operasyonno='".$_REQUEST['operasyonno']."'");

sql select count query error to get number of table records

i'm just trying to get the count of records in a certain table within my data base , my code:
<?php
require_once("/../includes/connectDB.php");
$q = "SELECT COUNT(*) AS Count FROM system_users";
$result = mysql_query($q);
$result = mysql_fetch_assoc($result)
$count = $result['Count'];
?>
i'm just getting this error:
Parse error: syntax error, unexpected '$count' (T_VARIABLE) in
C:\xampp\htdocs\sik\sections\Statistics.php on line 7
You are missing semicolon ;
<?php
require_once("/../includes/connectDB.php");
$q = "SELECT COUNT(*) AS Count FROM system_users";
$result = mysql_query($q);
$result = mysql_fetch_assoc($result);
^^^^^
$count = $result['Count'];
?>
Your script have the semicolon problem on line 6
$result = mysql_fetch_assoc($result)
change
$result = mysql_fetch_assoc($result);
you missed semicolon in line 6 but it showing error in line 7. why? becouse
Every PHP statement ends with a semicolon (;). PHP doesn't stop reading a statement until it reaches a semicolon. If you leave out the semicolon at the end of a line, PHP continues reading the statement on the following line. For example, consider the following statement:
$test = 1
echo $test;
These statements don't make sense to PHP; it reads the two lines as one statement, so it complains with an error message, such as the following:
Parse error: parse error in c:\test.php on line 2

Why do I get "Unexpected T_AS" in PHP PDO request?

I receive the following error...
Parse error: syntax error, unexpected T_AS in ....\index.php on line 98
for the following script...
<?php
try {
$db = new PDO('mysql:host=localhost;dbname=db', 'user', 'pw');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->beginTransaction();
$stmt = $db->prepare("SELECT * FROM tablename");
$stmt->execute();
while($db->fetch(PDO_FETCH_ASSOC) as $row) {
$id= $row['id'];
$name= $row['name'];
}
$db->commit();
}
catch (PDOException $e)
{
$db->rollback();
echo "There was a system error.<br>".$e->getMessage();
}
?>
Any idea what is throwing the error? I have checked for missing semicolons, commas, and the works but got nothing!
Parse error: syntax error, unexpected T_AS in ....\index.php on line 98
T_AS is the token for as in the PHP interpreter. It was unexpected when trying to parse your code's syntax.
as is only valid in a foreach loop, and you are using a while.
Change your while loop to a foreach loop.
Update
Fatal error: Call to undefined method PDO::fetch() in index.php on line 113
This is a run time error - the PDO object has no method called fetch(). Are you calling fetch() on the right object?
Check out the documentation.
As Wrikken states in the comments, it will be a method of your $stmt object.
Because you are using the 'as' keyword in a 'while' loop, which is not valid. Change the 'while' to 'foreach' and you are good to go.

Categories