Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Mysterious "Couldn't execute query."

Featured Replies

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.");
...

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

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

  • 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";
?>

  • 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

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.