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.

MySQL Advise

Featured Replies

Hi there, I'm starting to make my own CMS, and run into a couple of problems. To start with I have made a navigation bar using data from a MySQL database, as below;

 

<?php  
 $subject_set = get_all_subjects();
 while ($subject = mysql_fetch_array($subject_set)) {
  echo "<li><a href-\"content1.php?subj=" . urlencode($subject["id"]) . "\">{$subject["menu_name"]}</a></li>";
$page_set = get_pages_for_subject($subject["id"]);
echo "<ul>";
while ($page = mysql_fetch_array($page_set)) {
	echo "<li><a href=\"content1.php?page=" . urlencode($page["id"]) . "\" >{$page["menu_name"]}</a></li>";
}
echo "</ul>";
}
 ?>

 

I then draw back relevent data from the DB to populate the content on the page like so;

 

<?php if (isset($_GET['subj'])) {
	$sel_subj = $_GET['subj'];
	$sel_page = "";
} elseif (isset($_GET['page'])) {
	$sel_page = $_GET['page'];
	$sel_subj = "";
} else {
	$sel_subj = "";
	$sel_page = "";
}
?>
<head></head>
<body>
<div id="right">
<h1><?php echo ($subject["menu_name"]); ?></h1>

<p><?php echo $sel_page; ?></p>
</div>
</body>

 

The $sel_page works fine, but what I am wanting to do is make the h1 tag populate with the menu name from subjects table.

 

What I currently have in the h1 tags does not seem to work, can anyone advise why?

 

Cheers

Jamie

Have you tried

<h1><?php echo $subject["menu_name"]; ?></h1>

without the parentheses?

  • Author

Didnt work. I have added the below to my functions 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 no rows are returned fetch_arrey will return false
	if ($subject = mysql_fetch_array($result_set)) {
		return $subject;
	} else {
		return NULL;
	}
}

 

And this to my actual page;

 

<?php $sel_subject = get_subject_by_id($sel_subj); ?>

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

 

However when I come to open that page I get this error;

 

Database query failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LIMIT 1' at line 1

 

Am I missing something really daft here, or is there an issue with the LIMIT function with version 5?

Try changing this line:

$query .= "WHERE id=" . $subject_id;

to this:

$query .= "WHERE id= $subject_id";

As you're using double quotes, the variable should be read.

  • Author
Try changing this line:
$query .= "WHERE id=" . $subject_id;

to this:

$query .= "WHERE id= $subject_id";

As you're using double quotes, the variable should be read.

 

Nah still no joy. This one is really frying my brain lol! I think I'm in a little deep over my head now ^_^

Have you tried putting the query into one line:

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

  • Author

Yeah tried both ways, its wierd 'cos if I try to access the page using "content.php?subj=5" the page displays correctly. However if I try using a page= variable or no variables at all that error appears - its baffled me (but thats not hard!!) :rolleyes:

  • Author

Anyone got any ideas? I'm quite puzzled by this lol...

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.