anteater Posted July 6, 2010 Share Posted July 6, 2010 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 More sharing options...
klaude Posted July 6, 2010 Share Posted July 6, 2010 Check if your table in the database "PetCatalog" contains fields "petDescription" and "petName". Link to comment https://forums.phpfreaks.com/topic/206852-undefined-index/#findComment-1081772 Share on other sites More sharing options...
anteater Posted July 6, 2010 Author Share Posted July 6, 2010 The table "PetCatalog" containes the fields "petDescription" and "petName" Link to comment https://forums.phpfreaks.com/topic/206852-undefined-index/#findComment-1081773 Share on other sites More sharing options...
trq Posted July 6, 2010 Share Posted July 6, 2010 Check your query actually found some results before returning what you assume is a record. Link to comment https://forums.phpfreaks.com/topic/206852-undefined-index/#findComment-1081789 Share on other sites More sharing options...
anteater Posted July 6, 2010 Author Share Posted July 6, 2010 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 More sharing options...
klaude Posted July 6, 2010 Share Posted July 6, 2010 Try print_r($petInfo) for both cases. Link to comment https://forums.phpfreaks.com/topic/206852-undefined-index/#findComment-1081799 Share on other sites More sharing options...
anteater Posted July 6, 2010 Author Share Posted July 6, 2010 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.