November 27, 201015 yr I'm copying an example from PHP&MySQL for dummies and I'm pretty sure I typed everything in correctly, but when I try to execute the code I get, "Couldn't execute query." I think it's weird because I know it's connecting correctly (I don't get an error for that), and if I put the mysql query I'm trying to execute it in terminal, it works fine. Anyhow, here's the pertinent code. <?php /* Program: PetCatalog.php * Desc: Displays a list of pet categories from the PetType table. Includes descriptions. Displays radio button for user to check. * */ ?> <html> <head><title>Pet Types</title></head> <body> <style type="text/css"> body{ background:#222222; font-size:12px; color:#444444; font-family:tahoma; } #middle{ width:600px; background:#eeeeee; margin:70 auto; padding:20px; } </style> </head> <body> <div id="middle"> <?php include("misc.php"); $cxn = mysqli_connect($host,$user,$passwd,$dbname) or die ("couldn't connect to server"); /* Select all categories from PetType table */ $query = "SELECT * FROM PetType ORDER BY petType"; $result = mysqli_query($cxn,$query) or die ("Couldn't execute query."); ...
November 27, 201015 yr I'm copying an example from PHP&MySQL for dummies and I'm pretty sure I typed everything in correctly, but when I try to execute the code I get, "Couldn't execute query." I think it's weird because I know it's connecting correctly (I don't get an error for that), and if I put the mysql query I'm trying to execute it in terminal, it works fine. Anyhow, here's the pertinent code. <?php /* Program: PetCatalog.php * Desc: Displays a list of pet categories from the PetType table. Includes descriptions. Displays radio button for user to check. * */ ?> <html> <head><title>Pet Types</title></head> <body> <style type="text/css"> body{ background:#222222; font-size:12px; color:#444444; font-family:tahoma; } #middle{ width:600px; background:#eeeeee; margin:70 auto; padding:20px; } </style> </head> <body> <div id="middle"> <?php include("misc.php"); $cxn = mysqli_connect($host,$user,$passwd,$dbname) or die ("couldn't connect to server"); /* Select all categories from PetType table */ $query = "SELECT * FROM PetType ORDER BY petType"; $result = mysqli_query($cxn,$query) or die ("Couldn't execute query."); ... replace ur query with $result = mysqli_query($cxn,$query) or die (mysqli_error($cxn)); Tell us what mysqli error it puts out
November 27, 201015 yr Strange...change $result = mysqli_query($cxn,$query) or die ("Couldn't execute query."); to $result = mysqli_query($cxn,$query) or die (mysqli_error($cxn)); and post the error message
November 27, 201015 yr Author Says, "No database selected." By the way, the connection info is here (misc.php). Doesn't it specify the db here? <?php $user="root"; $host="localhost"; $passwd="root"; $database="PetCatalog"; ?>
November 27, 201015 yr Author Ah $database="PetCatalog"; should be $dbname="PetCatalog"; Ah! yes! Thank you so much! There was another typo similar to that in the book. *frowns and grabs pencil to fix* Needs a better editor.
Create an account or sign in to comment