Getting the category id from php in a mysql query

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
tybinc
Forum Newbie
Posts: 3
Joined: Wed Aug 20, 2008 8:52 pm

Getting the category id from php in a mysql query

Post by tybinc »

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.
User avatar
lukewilkins
Forum Commoner
Posts: 55
Joined: Tue Aug 12, 2008 2:42 pm

Re: Getting the category id from php in a mysql query

Post by lukewilkins »

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:

Code: Select all

function CategoryID() {
     //some code
     //some more code
     return $some_var_from_above;
}
You would simply have you query as so:

Code: Select all

$data = mysql_query("SELECT * FROM categories_descr WHERE category_id='".CategoryID()."'")
or die(mysql_error());
Hope that helps. If you still have problems, post your code ... especially the CategoryID() function.
tybinc
Forum Newbie
Posts: 3
Joined: Wed Aug 20, 2008 8:52 pm

Re: Getting the category id from php in a mysql query

Post by tybinc »

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...
User avatar
lukewilkins
Forum Commoner
Posts: 55
Joined: Tue Aug 12, 2008 2:42 pm

Re: Getting the category id from php in a mysql query

Post by lukewilkins »

Hey Brina,

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'];
}
?>
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:

Code: Select all

mysql_fetch_array($data, MYSQL_ASSOC)
Let me know if that works.

Luke
tybinc
Forum Newbie
Posts: 3
Joined: Wed Aug 20, 2008 8:52 pm

Re: Getting the category id from php in a mysql query

Post by tybinc »

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'];
}
?>
User avatar
lukewilkins
Forum Commoner
Posts: 55
Joined: Tue Aug 12, 2008 2:42 pm

Re: Getting the category id from php in a mysql query

Post by lukewilkins »

It goes inside the while() just like the mysql_fetch_array.

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'];
}
?>
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
Post Reply