I've written this code to

<?php $getnamequery = "SELECT cat_title FROM store_categories WHERE id = '$catid'";?>
<?php $cat_name = mysql_query($getnamequery) or die(mysql_error());?>

What would happen is the variable $catid would be filled in with a number, for instance 1.
The table store_categories looks like this

id   cat_title
1    Humor
2    Biographies


If $catid is equal to 1 I want to get the string humor.
I've tried this php code
<?php echo $cat_name['cat_title'];?>

But it returns nothing

Recommended Answers

All 5 Replies

The mysql_query function returns a resource not a row. The resource is a special php type that can be used to retrieve rows. You can use the mysql_fetch_row() function to fetch rows

while ($row = mysql_fetch_assoc($cat_name)) {
    echo $row['cat_title'];

Please note that $cat_name is a misleading name for a variable holding the resource result. $result would be more appropriate.

And another note which is being repeated quite often these days: try to switch to newer and safer mysqli extension and drop the old mysql which is deprecated as of PHP 5.5.0.

Still coming up as blank. Here's how i've used the code

<?php do { ?>
      <?php $catid = $row_Items['id'];?>
      <!--SELECT cat_title FROM store_categories WHERE id = '1'-->
      <?php $getnamequery = "SELECT cat_title FROM store_categories WHERE id = '$catid'";?>
      <?php $cat_result = mysql_query($getnamequery) or die(mysql_error());?>

          <td><?php echo $row_Items['id']; ?></td>
          <td><?php  while ($row = mysql_fetch_assoc($cat_result)) {
    echo $row['cat_title'];
          <td><?php echo $row_Items['item_title']; ?></td>
          <td><?php echo $row_Items['item_price']; ?></td>
          <td><?php echo substr($row_Items['item_desc'], 0, 31);?>...</td>
          <td><?php echo $row_Items['item_image']; ?></td>
          <td><a href="edit.php?id=<?php echo $row_Items['id']; ?>">Edit</a>
        <?php } while ($row_Items = mysql_fetch_assoc($Items)); ?>

Oops, sorry. My bad, Another bit of code was wrong which was causing it to break.
Thanks for your help!

Ok, so is the problem solved now?

Another recommendation: try to mix php and html as little as possible. The code will be far more manageable. Happy coding.

Everything is working now and thanks for the tips.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.