...

View Full Version : Database query failed



RDLyTN
01-10-2012, 12:46 PM
I always get this error: "Database query failed". Does someone knows what the problem is?

CONTENT.PHP


<?php require_once("includes/functions.php"); ?>
<?php require_once("includes/header.php"); ?>
<?php
if(isset($_GET['subj'])) {
$sel_subj = $_GET['subj'];
$sel_page = "";
} elseif(isset($_GET['page'])) {
$sel_subj = "";
$sel_page = $_GET['page'];
} else {
$sel_subj = "";
$sel_page = "";
}
$sel_subject = get_subject_by_id($sel_subj);
?>
<?php include("includes/connection.php"); ?>
<table id="structure">
<tr>
<td id="navigation">&nbsp;
<ul class="subjects">
<?php
$subject_set = get_all_subjects();

while($subject = mysql_fetch_array($subject_set)) {
echo "<li";
if ($subject["id"] == $sel_subj) { echo " class=\"selected\""; }

echo "><a href=\"content.php?subj=" . urlencode($subject["id"]) .
"\">{$subject["menu_name"]}</a></li>";

$page_set = get_pages_for_subject($subject["id"]);

echo "<ul class=\"pages\">";
while($page = mysql_fetch_array($page_set)) {
echo "<li";
if ($page["id"] == $sel_page) { echo " class=\"selected\""; }
echo "><a href=\"content.php?page=" .
urlencode($page["id"]) . "\">{$page["menu_name"]}</a></li>";
}
echo "</ul>";
}
?>
</ul>
</td>
<td id="page">
<h2><?php echo $sel_subject['menu_name']; ?></h2>
<br />
<?php echo $sel_page; ?><br />
</td>
</tr>
</table>
<?php include("includes/footer.php"); ?>

FUNCTIONS.PHP


<?php
function confirm_query($result_set) {
if(!$result_set) {
die("Database query failed: " .mysql_error());
}
}

function get_all_subjects() {
global $connection;
$query = "SELECT *
FROM subjects
ORDER BY position ASC";
$subject_set = mysql_query($query , $connection);
confirm_query($subject_set);
return $subject_set;
}

function get_pages_for_subject($subject_id) {
global $connection;
$query = "SELECT *
FROM pages
WHERE subject_id = {$subject_id}
ORDER BY position ASC";
$page_set = mysql_query($query, $connection);
confirm_query($page_set);
return $page_set;
}

function get_subject_by_id($subject_id) {
global $connection;
$query = "SELECT * ";
"FROM subject ";
"WHERE id=" . $subject_id ." ";
"LIMIT 1";
$result_set = mysql_query($query, $connection);
confirm_query($result_set);
if($subject = mysql_fetch_array($result_set)) {
return $subject;
} else {
return NULL;
}

}

?>

BluePanther
01-10-2012, 04:08 PM
I always get this error: "Database query failed". Does someone knows what the problem is?

CONTENT.PHP


<?php require_once("includes/functions.php"); ?>
<?php require_once("includes/header.php"); ?>
<?php
if(isset($_GET['subj'])) {
$sel_subj = $_GET['subj'];
$sel_page = "";
} elseif(isset($_GET['page'])) {
$sel_subj = "";
$sel_page = $_GET['page'];
} else {
$sel_subj = "";
$sel_page = "";
}
$sel_subject = get_subject_by_id($sel_subj);
?>
<?php include("includes/connection.php"); ?>
<table id="structure">
<tr>
<td id="navigation">&nbsp;
<ul class="subjects">
<?php
$subject_set = get_all_subjects();

while($subject = mysql_fetch_array($subject_set)) {
echo "<li";
if ($subject["id"] == $sel_subj) { echo " class=\"selected\""; }

echo "><a href=\"content.php?subj=" . urlencode($subject["id"]) .
"\">{$subject["menu_name"]}</a></li>";

$page_set = get_pages_for_subject($subject["id"]);

echo "<ul class=\"pages\">";
while($page = mysql_fetch_array($page_set)) {
echo "<li";
if ($page["id"] == $sel_page) { echo " class=\"selected\""; }
echo "><a href=\"content.php?page=" .
urlencode($page["id"]) . "\">{$page["menu_name"]}</a></li>";
}
echo "</ul>";
}
?>
</ul>
</td>
<td id="page">
<h2><?php echo $sel_subject['menu_name']; ?></h2>
<br />
<?php echo $sel_page; ?><br />
</td>
</tr>
</table>
<?php include("includes/footer.php"); ?>

FUNCTIONS.PHP


<?php
function confirm_query($result_set) {
if(!$result_set) {
die("Database query failed: " .mysql_error());
}
}

function get_all_subjects() {
global $connection;
$query = "SELECT *
FROM subjects
ORDER BY position ASC";
$subject_set = mysql_query($query , $connection);
confirm_query($subject_set);
return $subject_set;
}

function get_pages_for_subject($subject_id) {
global $connection;
$query = "SELECT *
FROM pages
WHERE subject_id = {$subject_id}
ORDER BY position ASC";
$page_set = mysql_query($query, $connection);
confirm_query($page_set);
return $page_set;
}

function get_subject_by_id($subject_id) {
global $connection;
$query = "SELECT * ";
"FROM subject ";
"WHERE id=" . $subject_id ." ";
"LIMIT 1";
$result_set = mysql_query($query, $connection);
confirm_query($result_set);
if($subject = mysql_fetch_array($result_set)) {
return $subject;
} else {
return NULL;
}

}

?>


seo.guru, it might be in his functions or header file. Regardless, what's the mysql_error()? Are you telling us the complete error message?

My guess is a syntax error. It looks like you're only assigning 'SELECT *' as the query from this:


$query = "SELECT * ";
"FROM subject ";
"WHERE id=" . $subject_id ." ";
"LIMIT 1";

$query is only getting assigned on the first line of that snippet - the rest are just strings that aren't assigned to variables.

RDLyTN
01-10-2012, 06:38 PM
seo.guru, it might be in his functions or header file. Regardless, what's the mysql_error()? Are you telling us the complete error message?

My guess is a syntax error. It looks like you're only assigning 'SELECT *' as the query from this:


$query = "SELECT * ";
"FROM subject ";
"WHERE id=" . $subject_id ." ";
"LIMIT 1";

$query is only getting assigned on the first line of that snippet - the rest are just strings that aren't assigned to variables.
I already fixed that, but that wasn't the problem. I still get the same error.

BluePanther
01-10-2012, 07:18 PM
I already fixed that, but that wasn't the problem. I still get the same error.

Have you shown the entire error message? Also, post the code that you claim has fixed that problem, just to make sure.

RDLyTN
01-10-2012, 07:23 PM
Have you shown the entire error message? Also, post the code that you claim has fixed that problem, just to make sure.


<?php
function get_subject_by_id($subject_id) {
global $connection;
$query = "SELECT * ";
$query .= "FROM subjects ";
$query .= "WHERE id=" . $subject_id . " ";
$query .= "LIMIT 1";
$result_set = mysql_query($query, $connection);
confirm_query($result_set);
if($subject = mysql_fetch_array($result_set)) {
return $subject;
} else {
return NULL;
}
}
?>

He only shows: "Database query failed:", he doesn't show the mysql_error()

BluePanther
01-10-2012, 07:30 PM
<?php
function get_subject_by_id($subject_id) {
global $connection;
$query = "SELECT * ";
$query .= "FROM subjects ";
$query .= "WHERE id=" . $subject_id . " ";
$query .= "LIMIT 1";
$result_set = mysql_query($query, $connection);
confirm_query($result_set);
if($subject = mysql_fetch_array($result_set)) {
return $subject;
} else {
return NULL;
}
}
?>

He only shows: "Database query failed:", he doesn't show the mysql_error()

For now, change to this:


$result_set = mysql_query($query, $connection) or die(mysql_error($connection));

That should pump out a better error.

RDLyTN
01-10-2012, 07:31 PM
For now, change to this:


$result_set = mysql_query($query, $connection) or die(mysql_error($connection));

That should pump out a better error.

I already tried that, now I won't get any error at all.

BluePanther
01-10-2012, 07:34 PM
I already tried that, now I won't get any error at all.

On content.php, are these not backwards?


<?php
if(isset($_GET['subj'])) {
$sel_subj = $_GET['subj'];
$sel_page = "";
} elseif(isset($_GET['page'])) {
$sel_subj = "";
$sel_page = $_GET['page'];
} else {
$sel_subj = "";
$sel_page = "";
}
$sel_subject = get_subject_by_id($sel_subj);
?>
<?php include("includes/connection.php"); ?>

Shouldn't the connection be before the get_subject_by_id()? That would explain the lack of mysql_error() messages. If this is the case, I'm very surprised PHP didn't moan a lot about using mysql_* functions without a valid mysql connection identifier.
Woo hoo, my 1000th post! :P

RDLyTN
01-10-2012, 07:40 PM
On content.php, are these not backwards?


<?php
if(isset($_GET['subj'])) {
$sel_subj = $_GET['subj'];
$sel_page = "";
} elseif(isset($_GET['page'])) {
$sel_subj = "";
$sel_page = $_GET['page'];
} else {
$sel_subj = "";
$sel_page = "";
}
$sel_subject = get_subject_by_id($sel_subj);
?>
<?php include("includes/connection.php"); ?>

Shouldn't the connection be before the get_subject_by_id()? That would explain the lack of mysql_error() messages. If this is the case, I'm very surprised PHP didn't moan a lot about using mysql_* functions without a valid mysql connection identifier.
Woo hoo, my 1000th post! :P

LOL W00T! Thanks my friend, I searched a whole day after this problem! :D

BluePanther
01-10-2012, 07:44 PM
no problem :)

One more thing - your site is quite open to SQL injection. I would advise using mysql_real_escape_string() around any user input (GET or POST values).

RDLyTN
01-10-2012, 07:51 PM
no problem :)

One more thing - your site is quite open to SQL injection. I would advise using mysql_real_escape_string() around any user input (GET or POST values).

Oke, I'll do that. Thanks for your help :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum