4 Replies - 133 Views - Last Post: 28 April 2019 - 02:53 PM Rate Topic: -----

#1 lfrost2015   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 08-March 15

PDO Exception Erorr

Posted 27 April 2019 - 03:06 PM

At the moment I am trying to convert some CSV data into a JSON array. This is done by having a database file that sets up he databse connection and an output php file that outputs the content as a JSON document.

db.php:

<?php

$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$dbname = "table1";

try{
	$dbcon = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);
	$dbcon->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $ex){
	die($ex->getMessage());
}


?>




output.php:

<?php
require_once 'db.php';

$sqlQuery = "select * from table 1";

$query = $dbcon->prepare($sqlQuery);
$query->execute();

$fetch = array();

while($row = $query->fetch(PDO::FETCH_ASSOC)) {
	$fetch['table 1'][] = $row;
}

echo json_encode($fetch);



?>



When I try to look at output.php this error appears:
Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'table 1' at line 1' in C:\xampp\htdocs\output.php:7 Stack trace: #0 C:\xampp\htdocs\output.php(7): PDOStatement->execute() #1 {main} thrown in C:\xampp\htdocs\output.php on line 7



The error that appeared before was that it didn't recognise the database table. Once I solved that error the above error appeared and I'm confused as to why as nothing should be wrong with the code?

Is This A Good Question/Topic? 0
  • +

Replies To: PDO Exception Erorr

#2 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2835
  • View blog
  • Posts: 11,125
  • Joined: 03-December 12

Re: PDO Exception Erorr

Posted 27 April 2019 - 03:09 PM

What is “table 1”?
Was This Post Helpful? 0
  • +
  • -

#3 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3814
  • View blog
  • Posts: 13,870
  • Joined: 08-August 08

Re: PDO Exception Erorr

Posted 27 April 2019 - 08:57 PM

If that really is your table name, put back ticks around it:
select * from `table 1`

This post has been edited by CTphpnwb: 28 April 2019 - 07:06 AM

Was This Post Helpful? 0
  • +
  • -

#4 andrewsw   User is online

  • quantum multiprover
  • member icon

Reputation: 6792
  • View blog
  • Posts: 28,045
  • Joined: 12-December 12

Re: PDO Exception Erorr

Posted 28 April 2019 - 12:54 AM

Better yet, use a meaningful name without spaces.
Was This Post Helpful? 0
  • +
  • -

#5 lfrost2015   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 08-March 15

Re: PDO Exception Erorr

Posted 28 April 2019 - 02:53 PM

View PostCTphpnwb, on 27 April 2019 - 08:57 PM, said:

If that really is your table name, put back ticks around it:
select * from `table 1`


Sorted it with this and then changed the table name to something more meaningful. Thanks!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1