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.
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.
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 }