1337475
1337475

Reputation: 171

sql code not displaying table data

Hello I am trying to understand why this isn't working on my page. I am using the php block below to use the variable I created at the beginning of my page $sel_subj (I used $_GET to get the id of what I clicked on on the previous page. I want the new page to reflect the data of the link I clicked on) I got the url to work to show the correct number from the database but I cannot get my page to display the name of what link was pressed; aka the data in the column labeled 'subject_name' from the 'subjects' table.

<?php
$query = "SELECT * FROM subjects WHERE id = \"$sel_subj\"";
$result_set = mysql_query($query, $connection);
if(!$result_set) {
    die("Database query failed: " . mysql_error());
$subject = mysql_fetch_array($result_set);
return $subject;
?>

into this h2 tag right here.

<h2><?php echo $subject['subject_name']; ?>Hello</h2>

I can post the whole page if it will help. I appreciate everyone's input.
Thank you.

edit:new problems

Here is the bottom half of my code. I have a navigation div above this code which separates the links to the pages that relate to the database tables from the content that I'd like to pull from the db and display in the div for the page table.

However with the code I've provided nothing is showing up on the page when I open it in firefox. In my html when I "view source" while previewing on the testing server there is nothing underneath ...

<td id="page">

<?php
$query = "SELECT * FROM subjects WHERE id ='$sel_subj'";
$result_set = mysql_query($query, $connection);
if(!$result_set) 
    die("Database query failed: " . mysql_error());
$subject = mysql_fetch_assoc($result_set);
return $subject;

?>
            <h2><?php echo $subject['subject_name']; ?>Hello</h2>
            <br />
            <?php echo $sel_page; ?><br />
            <?php echo $sel_subj; ?><br />
            <?php echo $subject; ?><br />
            <?php echo $subject['id']; ?>
        </td>

Upvotes: 1

Views: 2130

Answers (3)

Radu Murzea
Radu Murzea

Reputation: 10920

Change this:

$query = "SELECT * FROM subjects WHERE id = \"$sel_subj\"";

to this:

$query = "SELECT * FROM subjects WHERE id = '$sel_subj'";

and this:

$subject = mysql_fetch_array($result_set);

to this:

$subject = mysql_fetch_assoc($result_set);

PS: Try not to use the mysql class of functions anymore, they're not too good. Instead, use mysqli or PDO.

EDIT

If the column id is of numeric type, remove the apostrophes from the query. Like this:

$query = "SELECT * FROM subjects WHERE id = $sel_subj";

Upvotes: 3

Moyed Ansari
Moyed Ansari

Reputation: 8461

Try this

$query = "SELECT * FROM subjects WHERE id = {$sel_subj} ";

Upvotes: 1

vvr
vvr

Reputation: 466

Try This

$query = "SELECT * FROM subjects WHERE id = '$sel_subj' ";

Upvotes: 1

Related Questions