What does it mean?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Bekzod
Forum Newbie
Posts: 3
Joined: Fri Aug 05, 2005 6:14 am
Location: Uzbekistan

What does it mean?

Post by Bekzod »

8O .
//for checking email in database
$check="select id from subscribe where email='$email');
$check_result=mysql_query($check, $conn);
*/when i asked my friend for help how to delete entries he gave the next:
$Id= mysql_result($check_result, 0, "id");
/*
what does the above script mean exactly?
then i logically put the next it works but the meaning of the mentioned line.
$sql="delete from subscribe where id='$id');
$result=mysql_query($sql, $conn);
Bekzod
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: What does it mean?

Post by s.dot »

Code: Select all

// This is your SQL line, you're telling the database what you're going to do
// It had a syntax error, I fixed it.
$check="select id from subscribe where email='$email'";

// This line is performing the query, and stores the result of it in $check_result
$check_result=mysql_query($check, $conn);

// Attempting to extract the ID from the returned result row, although you should use mysql_fetch_assoc.. or leave the "id" part out
$Id= mysql_result($check_result, 0, "id");

// This line is telling the database to delete the record $Id
// Again, a syntax error, I replaced ')' with a double quote -- "
$sql="delete from subscribe where id='$id'";

// This line is performing the delete query
$result=mysql_query($sql, $conn);
I added comments above to help you out a little bit
Last edited by s.dot on Sat Aug 06, 2005 5:44 am, edited 1 time in total.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Code: Select all

$check="select id from subscribe where email='$email'";
That's just assigning a written MySQL SELECT query to a PHP variable called check. $email puts the email address into the query.

Code: Select all

$check_result=mysql_query($check, $conn);
That line simply runs the query on the database using the PHP function mysql_query() and assigns the result to the PHP variable check_result.

Now to retreive any data from the output of mysql_query() you need to use one of a handful of special PHP functions (mysql_result() or mysql_fetch_array() or mysql_fetch_assoc()). Here you've used mysql_result.

Code: Select all

$Id= mysql_result($check_result, 0, "id");
That tells mysql_result to get some data stored in the database that you retreived using mysql_query(). It gets id from row 0 of the results returned by the query and assigns it to the PHP variable Id.

Now your final line just uses that data to run a DELETE query on the MySQL database just like you ran the SELECT query at the start ;)

Code: Select all

$sql="delete from subscribe where id='$id'";
$result=mysql_query($sql, $conn);
EDIT | Damn I'm too slow :P
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

d11wtq wrote:EDIT | Damn I'm too slow :P
Slower, but definately more detailed :P
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Post Reply