Jump to content

Help cannot read query with PHP!


phpnewbe

Recommended Posts

I cannot figure out how to properly read from query into the browser. I want to read from table "subjects" in mySQLdb and have browser output "menu_name" field from there. This is the code of the function that does query:

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

 

It is called from here:

<?php require_once("includes/connection.php"); ?>
<?php require_once("includes/functions.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);
       
?>

The output goes from here:

<table id="structure">
            <tr>
                <td id="navigation">
                    <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>";
//content.php?subj= - sends coorect id to the browser,but I get only first "menu_name" to show//

 

Then $sel_subject supposed to echo every "subject name" here:

 

<h2><?php echo $sel_subject['menu_name']; ?></h2>

 

Instead I get only the first "menu_name" item put out. But I want them all to be read out of the table. And as I click on other "subjects" there is no output.(

 

I'll greatly appreciate some help here. What am I doing wrong?

Link to comment
https://forums.phpfreaks.com/topic/129279-help-cannot-read-query-with-php/
Share on other sites

Sorry this is the whole thing:

 

 

 

<?php require_once("includes/connection.php"); ?>

<?php require_once("includes/test.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/header.php"); ?>

 

<table id="structure">

            <tr>

                <td id="navigation">

                    <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/>

                            <br/>

                            <?php echo $sel_page; ?><br/>

       

                           

                       

                </td>

            </tr></table>

 

<?php require("includes/footer.php"); ?>

 

 

Problem is that $sel_subject gives only the fist item from "menu_name", but I need them all.

 

And this is the function:

function get_subject_by_id($subject_id) {
global $connection;
$query = "SELECT * FROM subjects";
//$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;}
}

function get_subject_by_id2($subject_id) {
    echo $subject_id;
}

Then if I uncomment those line in the function it works ( I get all the "menu_name" but stop getting output for page_name.

 

That is another function :

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;      

 

I get SQL read error from here but the syntax is correct here, I think.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.