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

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'];

Related

SELECT execute([]) Parse error: syntax error, unexpected '[', expecting ')' in [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 3 years ago.
I'm adding [] this symbol within an execute() in PDO, and it returns error.
I'm working in WAMP5
$sqlStatement="
SELECT *
FROM $table_results
WHERE title = ? AND id_category = ?
";
$stmt = $bdConection->prepare($sqlStatement);
$stmt->execute([$title, $id_category]);
echo $stmt->rowCount();
It works well if I delete WHERE title = ? AND id_category = ? and [$title, $id_category] bur returns more results, instead of adding those ... then
IT RETURNS:
Parse error: syntax error, unexpected '[', expecting ')' in
I can reproduce this error with PHP 5.2. Documentation about PHP array's syntax states:
... As of PHP 5.4 you can also use the short array syntax, which
replaces array() with []. ...
What you can do in your case is to define an array using array():
<?php
$sqlStatement = "
SELECT *
FROM $table_results
WHERE title = ? AND id_category = ?
";
...
$stmt = $bdConection->prepare($sqlStatement);
$stmt->execute(array($title, $id_category));
echo $stmt->rowCount();
...
?>

How to assign Array to a String in PHP? [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 7 years ago.
I am trying to assign the contents of a 2D array to a string in PHP.
$sql="SELECT Order_Code FROM Order WHERE CUST_CODE = '$output['username']';";
I know the problem exists in how I'm writing the $output variable assignment.
The following line of code outputs the correct data from the variable:
echo $output['username'];
The following error is being thrown:
Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING)
Parameters surrouned by curly brackets will work well in your case. Here is what i mean. {$array['key']}
And for your example:
$sql="SELECT Order_Code FROM Order WHERE CUST_CODE = '{$output['username']}';";
You need to concatenate this string as the multiple single quotes from accessing the array element are mixing up the string.
$sql="SELECT Order_Code FROM Order WHERE CUST_CODE = '" . $output['username'] ."';";
Try this:
$sql="SELECT Order_Code FROM Order WHERE CUST_CODE = '".$output['username']."'";
You can also try this:
$user_name = $output['username'];
$sql = "SELECT Order_Code FROM Order WHERE CUST_CODE = $user_name";

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.

How to fix "Unexpected T_ENCAPSED_AND_WHITESPACE" error in PHP? [duplicate]

This question already has an answer here:
Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE) [duplicate]
(1 answer)
Closed 8 years ago.
Here is the error:
Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in C:\xampp\htdocs\new-training-site\config\info.php on line 4;
Here is line 4:
$link = mysql_query("SELECT * FROM users WHERE id='$_SESSION['MM_Username']' AND password='$_SESSION['MM_PASSWORD']'");
$link = mysql_query("SELECT * FROM users WHERE id='{$_SESSION['MM_Username']}' AND password='{$_SESSION['MM_PASSWORD']}'");
Try this one
$link = mysql_query(sprintf(
"SELECT * FROM users WHERE id='%s' AND password='%s'",
mysql_real_escape_string($_SESSION['MM_Username']),
mysql_real_escape_string($_SESSION['MM_PASSWORD']),
));
It will help you to avoid sql-injection. And please don't use mysql_ functions as it were mentioned in comments

How to get data from database and echo on php page? [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 3 years ago.
How to get the date from database and echo on PHP page?
$query = $pdo->prepare('SELECT * FROM shop WHERE shopname=:shopname');
$query->bindParam(':shopname', $shopname, PDO::PARAM_STR);
$query->execute();
$result = $query->fetchAll(PDO::FETCH_ASSOC);
echo "$result['shopid']";
This gives me the following error:
Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE),
expecting identifier (T_STRING) or variable (T_VARIABLE) or number
(T_NUM_STRING)
echo "$result['shopid']";
This line is incorrect
echo $result["shopid"];
// OR
echo "{$result['shopid']}";
remove the double quotes, change
echo "$result['shopid']";
with
echo $result['shopid'];

Categories