1

I am getting a parse error when I run my script

Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in C:\Program Files (x86).................\get.php on line 24

I cant see whats wrong, can someone help me with it?

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "database;

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname) ;
// Check connection
if ($conn->connect_error) 
    {
    die('Connection failed: ' . $conn->connect_error) ;
    } 
else 
    {
    $product1 = filter_input(INPUT_POST, 'id', FILTER_SANITIZE_NUMBER_INT) ;

    $query = 'SELECT price FROM forms WHERE name=' . $product1 . ' ' ;

    $res = mysql_query($query) ;
    if (mysql_num_rows($res) > 0) 
    {
    $result = mysql_fecth_assoc($res) ;
        echo json_encode($result['price']);
    }
    else
        {
        echo json_encode('no results') ;
        }

    }
?>
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
John
  • 904
  • 8
  • 22
  • 56

3 Answers3

3

There are quite a few errors in your code, besides the missing quote in $dbname = "database;.

You're mixing MySQL APIs here. mysql_ does NOT intermix with the mysqli_ API.

So, you need to change all instances of mysql_ to mysqli_ and pass the connection parameter in the query.

Then, mysql_fecth_assoc was mispelled and corrected with the added i.

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "database";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname) ;
// Check connection
if ($conn->connect_error) 
    {
    die('Connection failed: ' . $conn->connect_error) ;
    } 
else 
    {
    $product1 = filter_input(INPUT_POST, 'id', FILTER_SANITIZE_NUMBER_INT) ;

    $query = 'SELECT price FROM forms WHERE name=' . $product1 . ' ' ;

    $res = mysqli_query($conn, $query) ;
    if (mysqli_num_rows($res) > 0) 
    {
    $result = mysqli_fetch_assoc($res) ;
        echo json_encode($result['price']);
    }
    else
        {
        echo json_encode('no results') ;
        }

    }

Check for errors also:

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
1

Change

$dbname = "database;

To

$dbname = "database";
Adam Hull
  • 214
  • 2
  • 10
1

You're missing a " in this line:

$dbname = "database;

It should be:

$dbname = "database";

Hope this helps, thanks!

Panda
  • 6,955
  • 6
  • 40
  • 55