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
https://forums.phpfreaks.com/topic/206852-undefined-index/
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
https://forums.phpfreaks.com/topic/206852-undefined-index/#findComment-1081793
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
https://forums.phpfreaks.com/topic/206852-undefined-index/#findComment-1081803
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.