mySQL/PHP - store cell value in variable

zachsformacs95

New Member
Ok, I'm from webdevforums which I hear got hacked... Anyway, I'm pretty new to SQL. I have a database, and each record in the table has a unique id. If I want to delete a row, I pass on the table name and that unique id like so:

Code:
<input type="button" onclick="show_confirm('tablename', '<?php echo $id; ?>')" value="Delete" />

It shows a confirmation box which, on OK, proceeds to delete the row -

Code:
window.location = "edit.php?p=delete&table=" + table + "&id=" + id;

All of this works fine. I delete the row like so:

Code:
$con = mysql_connect("localhost", "username", "password");
if (!$con) {
	die( 'Could not connect to database: ' . mysql_error() );
}
@mysql_select_db("database") or die( "Unable to select database");
mysql_query("DELETE FROM " . $_GET['table'] . " WHERE id = '" . $_GET['id'] . "'");
mysql_close($con);

Once again, all of this works fine. However, I would like to notify the user which row has been deleted. I want to get the "teacher" field of the row in this table that has the id of $id and store that in a variable. How would I do this?

I've tried:

Code:
$teacher = mysql_query("SELECT teacher FROM " . $table . " WHERE id = '" . $id . "'");

But that returns "Resource id #2".

Thanks in advance. Note: I've simplified my code, if for whatever reason you want to see more of the original code, let me know.
 
Last edited:

DHDdirect

New Member
Lets see all your code. You can either post it or upload a file.

It may sound stupid but just make sure you are storing the information in the $teacher variable before the row is deleted :)
 

MarkR

New Member
When it returns a resource ID it's because you still need to apply another function to get the information you need from the resource.

Add this:

PHP:
$teacher_rows = mysql_fetch_row($teacher); 
$teacher_rows = $teacher_rows[0];

If you are expecting more than one row in the result you can use this to parse all the data:

PHP:
while($teacher_rows = mysql_fetch_row($teacher)){ 

   print_r($teacher_rows); //this is the current row
}
 

zachsformacs95

New Member
When it returns a resource ID it's because you still need to apply another function to get the information you need from the resource.

Add this:

PHP:
$teacher_rows = mysql_fetch_row($teacher); 
$teacher_rows = $teacher_rows[0];

If you are expecting more than one row in the result you can use this to parse all the data:

PHP:
while($teacher_rows = mysql_fetch_row($teacher)){ 

   print_r($teacher_rows); //this is the current row
}

Thank you - that works.
 
Top