Hi all, Im designing a database to go with my website project and going through the php for dummies book but I have an error and cannot figure it out. I paste my code in

<?php
/*
	Config file
	@author Imran Rashid
*/

// global variables
$base_url = "http://localhost/petstore/";
$base_path = "/xampplite/htdocs/petstore/";

// database stuff
$host="localhost";
$user="root";
$password="tenchu";
$dbname = "pets";

//firstmethod

$cxn = mysqli_connect($host,$user,$password) or die("could not connect to database");

// now connection2 is set up, select the database
mysqli_select_db($cxn, $dbname);

//$cxn = mysqli_connect($host, $user,$password,$php_blog)
//or die ("could not connect server");
?>

Thats my config file for my connection variables

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Pet Catalog</title>
</head>

<?php

/* Program: getPets.php
*  Desc: Displays list of items from a database
*/

require_once("config.php");

function getPetInfo($petName)





$petInfo = getPetInfo("Unicorn"); //call function

$f_price = number_format($petInfo ['price'] ,2);
echo"<p><b>{$petInfo['petName']}</b><br />\n
Description:{$petInfo['petDescription']}<br />\n
Price: \${$petInfo['price']}\n"



?>



<body>
</body>
</html>

and thats my file im working on

Parse error: syntax error, unexpected $end in C:\xampp\htdocs\petstore\getdata.php on line 37

this displays error, im new to this forum and new to php and programming is my weak point, so hopefully you guys can go easy on me as I could pester you a lot of the times

thanks 🙂

    It looks like you're missing some of your code in the second file. Did you forget to include the definition of the getPetInfo() function?

      Hey thanks for repyling 🙂

      This is what i was thinking, because the code is not reading getPetInfo function and as im aware the book does another php tag that I did not include, i'll show it

      <?php
      function getPetInfo($petName)
      
      $user="Catalog";
      $host="localhost"
      $password=""
      $dbname=""
      $cxn=mysqli_connect($host,$password,$user,$dbname)
      or die("could not connect to server");
      $query ="SELECT * FROM Pet  WHERE petName='$petName' ";
      $result = mysqli_query($cxn. $query)
      or die("couldnt execute query.");
      
      return mysqli_fecth_assoc($result);
      
      }
      
      
      
      ?>
      
      

      there could be a typo in the example that i showed you but im assuming when it shows function getPetInfo($petName) I thought this will be the declaration but im not sure if thats it.

      I did some bits differently as I added a included a config file using require once method so saves me typing in the database connection but im sure you know this 🙂.

      I hope I made sense

        Thanks for replying 🙂.

        I thought the code "function getPetInfo(petName)" was the declaration, but Im not sure as the book displays the code that needed to write down despite i did it in a different way. I did rewrite the code like it showed in the code but it still does not work

        Hope it made sense 🙂

          well you need to use brakets after you define your function.

          as well you also need them for you die statments

          example below.

          
          function getPetInfo($petName)
          {
              $user='Catalog';
              $host='localhost';
              $password=''
              $dbname=''
              $cxn=mysqli_connect($host,$password,$user,$dbname) or die("could not connect to server" . mysqli_error() . mysqli_errno());
              $query ="SELECT * FROM Pet  WHERE petName='$petName' ";
              $result = mysqli_query($cxn , $query)or die("couldnt execute query.");
              return mysqli_fecth_assoc($result);
          }
          

            Hi thanks for replying, I did use brackets when I tried the other method, it did not shoot an error but it still didn't work as it should display

            Unicorn
            Description: spiral horn centered in forehead
            Price: $10,00.00

            But instead it showed me this

            Description:
            Price:$"couldn't execute query"

            Your example showed an error which is odd cause it should of displayed my statement above.

            Parse error: syntax error, unexpected T_VARIABLE in C:\xampp\htdocs\petstore\getdata.php on line 37

            as you can see i included my config file using require_once ("config.php") so it saves me retyping my database as unforunatly the book does not teach you this and my freind reccomended me using this way.

            Anyway you can figure out to call the PetInfo function without retyping your databse connection again?. Sorry if I my explaination does not make any sense, Im in a critical situation as I was supposed to finish my databse for my project by now and have to show my prototype in 6 weeks and most of it has to be done and I won't able to progress if i don't get rid of this error, sadly its the winter break and noone is availble to help 🙁

            Thanks for the help so far i really appreciate it 🙂

              Write a Reply...