Selecting a row and putting it into a variable

Discussion in 'PHP' started by gahoo, Oct 24, 2007.

  1. #1
    SELECT cat_ID from wp_categories WHERE cat_name = '$name'

    How do I put the cat_ID into a variable after that?

    Thanks!

    I've tried
     $sql = "SELECT cat_ID from wp_categories WHERE cat_name = '$name'";
    	//Then we assign '$query' to the query, this runs query based on ^ sql statement
     $query = mysql_query($sql);
     //Then, if the query has worked (Return bool(True, false))
     //Then continue (Denoted by the curly braces)
     if($query){
     	if(mysql_num_rows($query)){
    	while($row = mysql_fetch_array($query)){
    		//Now, as you can see above. We have assigned '$row' to the array
    		//Then we assign another variable called $cat_id. Each variable will be different because 
    		//we are using the data from a mysql table that is being looped.
    		$cat_id = $row['cat_ID'];
    		//Now we use ^ value to echo a link to the category
    		echo '<a href="category?id='.$cat_id.'">Cat id </a>';
    		//This last curly brace will finish the loop which means anything outside this brace, wont loop
    	}
    	//Now we use the 'else' statement to make sure that if there is an error, use the commands in the curly braces
    }else{
    	echo 'No rows';
    }
    }else{
    	//We use the die() function now, anything AFTER this wont work.
    	//The we use 
    	die('Mysql Error - > '.mysql_error());
    }
    Code (markup):
    And it did not work. $cat_id stayed undefined. The row is not empty though.
     
    gahoo, Oct 24, 2007 IP
  2. webrepair

    webrepair Peon

    Messages:
    41
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Use mysql_fetch_row() and you can assign variable that way.
     
    webrepair, Oct 25, 2007 IP
  3. tonybogs

    tonybogs Peon

    Messages:
    462
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #3
    This code looks ok to me.

    Try doing a print_r($row) for every loop to see what value is actually being assigned to $row.
     
    tonybogs, Oct 25, 2007 IP
  4. dadougalee

    dadougalee Peon

    Messages:
    589
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Ok, do this:

    while($row = mysql_fetch_array($query, MYSQL_ASSOC)){ // i think once you add this it should work.
    //Now, as you can see above. We have assigned '$row' to the array
    //Then we assign another variable called $cat_id. Each variable will be different because
    //we are using the data from a mysql table that is being looped.
    $cat_id = $row['cat_ID'];
    //Now we use ^ value to echo a link to the category
    echo '<a href="category?id='.$cat_id.'">Cat id </a>';
    //This last curly brace will finish the loop which means anything outside this brace, wont loop
    }
     
    dadougalee, Oct 25, 2007 IP
  5. jakomo

    jakomo Well-Known Member

    Messages:
    4,262
    Likes Received:
    82
    Best Answers:
    0
    Trophy Points:
    138
    #5
    try to change mysql_fetch_array to mysql_fetch_assoc

    Best,
    Jakomo
     
    jakomo, Oct 25, 2007 IP