Please help me. I'm really dumb wehn it comes to PHP.
I want to be able to display text on my dynamic category page by category id. I have created the field for this text in the database.
Here is the query:
<?php
// Connects to your Database
mysql_connect("my.host.com", "myuser", "mypass") or die(mysql_error());
mysql_select_db("my_database") or die(mysql_error());
$data = mysql_query("SELECT * FROM categories_descr WHERE category_id=RIGHT HERE")
or die(mysql_error());
while($info = mysql_fetch_array( $data ))
{
Print $info['field_in_database'];
}
?>
The category for the page is stored in the code somewhere. The code I am to retrieve the category_id with is:
<?php CategoryID() ?>
But I can't use that in the database query. How do I use the category_id that the page stores dynamically?
Hope that makes a little sense.
Getting the category id from php in a mysql query
Moderator: General Moderators
- lukewilkins
- Forum Commoner
- Posts: 55
- Joined: Tue Aug 12, 2008 2:42 pm
Re: Getting the category id from php in a mysql query
So you have a function called CategoryID(), correct? Give us that code and we can probably help you more (or give us the full "relevant" code).
If you have that CategoryID() like this:
You would simply have you query as so:
Hope that helps. If you still have problems, post your code ... especially the CategoryID() function.
If you have that CategoryID() like this:
Code: Select all
function CategoryID() {
//some code
//some more code
return $some_var_from_above;
}Code: Select all
$data = mysql_query("SELECT * FROM categories_descr WHERE category_id='".CategoryID()."'")
or die(mysql_error());Re: Getting the category id from php in a mysql query
Ok, well that must be the function but I must have the syntax wrong because whe I run:
<?php
// Connects to your Database
mysql_connect("my.host.com", "myuser", "mypass") or die(mysql_error());
mysql_select_db("my_database") or die(mysql_error());
$data = mysql_query ("SELECT * FROM categories_descr WHERE category_id='".CategoryID()."'")
or die(mysql_error());
while($info = mysql_fetch_array( $data ))
{
Print $info['page_header'];
}
?>
The page prints just the category ID. It's the correct id but that's all that displays on the page. Not the field in the table I am asking for...
<?php
// Connects to your Database
mysql_connect("my.host.com", "myuser", "mypass") or die(mysql_error());
mysql_select_db("my_database") or die(mysql_error());
$data = mysql_query ("SELECT * FROM categories_descr WHERE category_id='".CategoryID()."'")
or die(mysql_error());
while($info = mysql_fetch_array( $data ))
{
Print $info['page_header'];
}
?>
The page prints just the category ID. It's the correct id but that's all that displays on the page. Not the field in the table I am asking for...
- lukewilkins
- Forum Commoner
- Posts: 55
- Joined: Tue Aug 12, 2008 2:42 pm
Re: Getting the category id from php in a mysql query
Hey Brina,
Try this:
and see if that solves your problem.
It is strange though because mysql_fetch_array() should return both the numeric and associative array if result_type parameter is not set. You could also try using:
Let me know if that works.
Luke
Try this:
Code: Select all
<?php
// Connects to your Database
mysql_connect("my.host.com", "myuser", "mypass") or die(mysql_error());
mysql_select_db("my_database") or die(mysql_error());
$data = mysql_query ("SELECT * FROM categories_descr WHERE category_id='".CategoryID()."'")
or die(mysql_error());
while($info = mysql_fetch_assoc($data)) {
echo $info['page_header'];
}
?>It is strange though because mysql_fetch_array() should return both the numeric and associative array if result_type parameter is not set. You could also try using:
Code: Select all
mysql_fetch_array($data, MYSQL_ASSOC)Luke
Re: Getting the category id from php in a mysql query
The first example you gave me produces the same result - it prints the category id.
Where does the second line of code you provided go? Does it replace:
while($info = mysql_fetch_assoc($data)) {
If so, then it looks like this (and breaks the page):
<?php
// Connects to your Database
mysql_connect("host", "user", "pass") or die(mysql_error());
mysql_select_db("db") or die(mysql_error());
$data = mysql_query ("SELECT * FROM categories_descr WHERE category_id='".CategoryID()."'")
or die(mysql_error());
mysql_fetch_array($data, MYSQL_ASSOC)
echo $info['page_header'];
}
?>
Where does the second line of code you provided go? Does it replace:
while($info = mysql_fetch_assoc($data)) {
If so, then it looks like this (and breaks the page):
<?php
// Connects to your Database
mysql_connect("host", "user", "pass") or die(mysql_error());
mysql_select_db("db") or die(mysql_error());
$data = mysql_query ("SELECT * FROM categories_descr WHERE category_id='".CategoryID()."'")
or die(mysql_error());
mysql_fetch_array($data, MYSQL_ASSOC)
echo $info['page_header'];
}
?>
- lukewilkins
- Forum Commoner
- Posts: 55
- Joined: Tue Aug 12, 2008 2:42 pm
Re: Getting the category id from php in a mysql query
It goes inside the while() just like the mysql_fetch_array.
So:
If this doesn't solve it then something else is the issue because this should be working either way. Post your entire CategoryID() function so I can see that as that is probably where the problem lies.
Luke
So:
Code: Select all
<?php
// Connects to your Database
mysql_connect("my.host.com", "myuser", "mypass") or die(mysql_error());
mysql_select_db("my_database") or die(mysql_error());
$data = mysql_query ("SELECT * FROM categories_descr WHERE category_id='".CategoryID()."'")
or die(mysql_error());
while($info = mysql_fetch_array($data, MYSQL_ASSOC)) {
echo $info['page_header'];
}
?>Luke