Jump to content

Undefined index


anteater

Recommended Posts

Hi,

 

I'm practicing with PHP, but I get the error: Notice: Undefined index: petName in C:\Floor\WebsiteAnt\TestPHP\getdata.php on line 13 Notice: Undefined index: petDescription in C:\Floor\WebsiteAnt\TestPHP\getdata.php on line 14

 

In this script:

/* Program: getdata.php
* Desc:    Gets data from a database using a function
*/
?>
<html>
<head><title>Pet Catalog</title></head>
<body>
<?php

  $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></html>

<?php
function getPetInfo($petName)
{
  $user="catalog";
  $host="localhost";
  $password="";
  $cxn = mysqli_connect($host,$user,$password)
         or die ("Couldn't connect to server");
  $db = mysqli_select_db($cxn,"PetCatalog")
        or die ("Couldn't select database");
  $query = "SELECT * FROM Pet WHERE petName='$petName'";
  $result = mysqli_query($cxn,$query)
            or die ("Couldn't execute query.");
  return mysqli_fetch_assoc($result);
}

 

Can somebody help me? Thanks in advance

Link to comment
Share on other sites

The query found some results after I edit it like this:

 

/* Program: getdata.php
* Desc:    Gets data from a database using a function
*/
?>
<html>
<head><title>Pet Catalog</title></head>
<body>
<?php

  $petInfo = getPetInfo("Lion");      //call function
  $petName = "Petname";
  $petDescription= "PetDescription";
  $f_price = number_format($petInfo['price'],2);
  echo "<p><b>PetName:{$petName['petName']}</b><br>\n
        Description: {$petDescription['petDescription']}<br>\n
        Price: \${$petInfo['price']}\n"
?>
</body></html>

<?php
function getPetInfo($petName)
{
  $user="catalog";
  $host="localhost";
  $password="";
  $cxn = mysqli_connect($host,$user,$password)
         or die ("Couldn't connect to server");
  $db = mysqli_select_db($cxn,"PetCatalog")
        or die ("Couldn't select database");
  $query = "SELECT * FROM Pet WHERE petName='$petName'";
  $result = mysqli_query($cxn,$query)
            or die ("Couldn't execute query.");
  return mysqli_fetch_assoc($result);
}

 

This is the result:

 

PetName:P

Description: P

Price: $2000.00

 

But this is not what I expected.

Link to comment
Share on other sites

When I do that:

 

<html>
<head><title>Pet Catalog</title></head>
<body>
<?php

  $petInfo = getPetInfo("Lion");      //call function
  $petName = print_r($petInfo);
  $petDescription = print_r($petInfo);
  $f_price = number_format($petInfo['price'],2);
  echo "<p><b>PetName:{$petName['petName']}</b><br>\n
        Description: {$petDescription['petDescription']}<br>\n
        Price: \${$petInfo['price']}\n"
?>
</body></html>

<?php
function getPetInfo($petName)
{
  $user="root";
  $host="localhost";
  $password="wortel";
  $cxn = mysqli_connect($host,$user,$password)
         or die ("Couldn't connect to server");
  $db = mysqli_select_db($cxn,"PetCatalog")
        or die ("Couldn't select database");
  $query = "SELECT * FROM Pet WHERE petName='$petName'";
  $result = mysqli_query($cxn,$query)
            or die ("Couldn't execute query.");
  return mysqli_fetch_assoc($result);
}

 

I get this:

 

Array ( [PetID] => 3 [PetName] => Lion [PetType] => 2 [PetDescription] => Large; Mane on neck [price] => 2000.00 [pix] => /pics/lion.jpg [colorID] => 2 ) Array ( [PetID] => 3 [PetName] => Lion [PetType] => 2 [PetDescription] => Large; Mane on neck [price] => 2000.00 [pix] => /pics/lion.jpg [colorID] => 2 )

 

PetName:

Description:

Price: $2000.00

 

What did I do wrong now?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.