mamleader Posted December 28, 2010 Share Posted December 28, 2010 hey I am still little new in php can you help me in this one func.php function confirm_query($result_set){ if(!$result_set){ die(mysql_error()); } function get_subjects_all() { global $connection ; $sub_query = "SELECT * FROM subjects ORDER BY position ASC"; $subject_query = mysql_query($sub_query,$connection); confirm_query($subject_query) ; return $subject_query; } function get_pages_all($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; } test.php require_once("config.php"); require_once("func.php"); $subject_query = get_subjects_all(); while($subject = mysql_fetch_array($subject_query)) { echo "<a href= \"test.php?subj=" . urlencode($subject["id"]) . "\">{$subject['menu_name']} <br />"; } $an = $subject["id"] ; $page_set = get_pages_all($an); while($page = mysql_fetch_array($page_set)) { echo "<a href = \"test.php?page=" . urldecode($page["id"]) . "\">{$page['menu_name']} </a><br />" ; // echo "<b> <a href= '1.php?page={$page['id']}' >{$page['menu_name']} </a></b> <br />"; } in this line WHERE subject_id = '$subject_id' the query don't work and gives me sql error i tried with this one ' ".$subject_id." ' but not worked but if i changed $subject_id to a number it work and read the data from the table thanks in advanced . Quote Link to comment https://forums.phpfreaks.com/topic/222822-mysql_query-error/ Share on other sites More sharing options...
BlueSkyIS Posted December 28, 2010 Share Posted December 28, 2010 what is the error? and what is the SQL that is causing it? echo the SQL to see what is wrong in the SQL. $result = mysql_query($sql) or die(mysql_error(). " IN $sql"); that will tell you a lot more than functions confirm_query() Quote Link to comment https://forums.phpfreaks.com/topic/222822-mysql_query-error/#findComment-1152149 Share on other sites More sharing options...
desjardins2010 Posted December 28, 2010 Share Posted December 28, 2010 first off try echo subject_id see what the value is... I could be missing something but I don't see where your declaring subject_id Quote Link to comment https://forums.phpfreaks.com/topic/222822-mysql_query-error/#findComment-1152154 Share on other sites More sharing options...
mamleader Posted December 28, 2010 Author Share Posted December 28, 2010 thanks I think no wrong with query it comes when I am doing the fetch command btw I put all in a simple file <?php //header //mysql database connection $connection = mysql_connect("localhost","root","ikillu"); if(!$connection){die(mysql_error()); } //mysql database select $db_select = mysql_select_db("mam_cop",$connection); if(!$db_select){die(mysql_error());} function get_subjects_all() { global $connection ; $sub_query = "SELECT * FROM subjects ORDER BY position ASC"; $subject_query = mysql_query($sub_query,$connection)or die(mysql_error() . " IN $query"); //confirm_query($subject_query) ; return $subject_query; } function get_pages_all($subject_id) { global $connection; $query = "SELECT * FROM pages WHERE subject_id = '$subject_id' ORDER BY position ASC"; $page_set = mysql_query($query, $connection) or die(mysql_error() . " IN $query"); //confirm_query($page_set); return $page_set; } $subject_query = get_subjects_all(); while($subject = mysql_fetch_array($subject_query)) { echo $subject["menu_name"] . "<br />" ; } $all_pages = get_pages_all($subject["id"]); while($page = mysql_fetch_array($all_pages)) { echo $page["menu_name"] . "<br />" ; } ?> so you can figure easlly btw $subject_id called with $subject["id"] which is table -> row named id it continus id : 1 , 2 , .. etc Quote Link to comment https://forums.phpfreaks.com/topic/222822-mysql_query-error/#findComment-1152175 Share on other sites More sharing options...
Maq Posted December 28, 2010 Share Posted December 28, 2010 As desjardins mentioned echo out the subject to see if there is a value. Are you also aware, your call is not in the while loop? Quote Link to comment https://forums.phpfreaks.com/topic/222822-mysql_query-error/#findComment-1152181 Share on other sites More sharing options...
mamleader Posted December 28, 2010 Author Share Posted December 28, 2010 thanks guys you helped me I am stupid stupid I just forget that I am calling a variable outside the loop i just move } and it works :=wooo thanks Quote Link to comment https://forums.phpfreaks.com/topic/222822-mysql_query-error/#findComment-1152195 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.