Basic mysql queries :: PHP ::

Information for PHP. Hypertext Preprocessor
Post Reply
dzcadii
Posts: 94
Joined: Sun Mar 01, 2015 4:36 am

Basic mysql queries :: PHP ::

Post by dzcadii » Sun Mar 01, 2015 6:28 pm

Basic mysql queries:

My basic table will be:
  • id
    first_name
    last_name
    email_address
Insert data into a mysql database with PHP

Code: Select all

$insert = mysql_query("INSERT INTO table_name (id, first_name, last_name, email_address) VALUES('0','Dz','Sound','support@dzsoundnirvana.com')");
Update data in a mysql database with PHP

Code: Select all

$update = mysql_query("UPDATE table_name SET first_name='Dz', last_name='SoundNirvana', email_address='you@you.com' WHERE id='0' && last_name='Sound'");
Delete one specific record from a mysql database with PHP

Code: Select all

$delete = mysql_query("DELETE FROM table_name WHERE id='0' && email_address='you@you.com'");
Delete more than one record from a mysql database with PHP

Code: Select all

$delete = mysql_query("DELETE FROM table_name WHERE id > 1");
Return the email address (or what ever you want) from a single record

Code: Select all

$query = mysql_query("SELECT email_address FROM table_name WHERE first_name='Dz'");

$number_of_rows = mysql_num_rows($query);

if($number_of_rows != "0"){

$result = mysql_result($query,0);

}
else{

echo "There are no records in the database with that first name!";

}
Show all fields in a record set

Code: Select all

$query = mysql_query("SELECT * FROM table_name WHERE first_name='Dz'");

$number_of_rows = mysql_num_rows($query);

if($number_of_rows != "0"){

//Here you can do a number of things with the array. I will show a few ways of outputting that information
while($user_info = mysql_fetch_assoc($query)){

//This will take the key name (the field name in the table) and set it a as $variable
extract($user_info);

echo $id . "<br>" . $first_name . "<br>" . $last_name . "<br>" . $email_address;
}

//Or you can simply print_r($user_info)

//Or run it through a foreach loop if you expect multiple record sets
foreach($user_info as $key => $value){
echo $user_info['id'] . "<br>" . $user_info['first_name'] . "<br>" . $user_info['last_name'] . "<br>" . $user_info['email_address'];
}

}

else{

echo "There are no records in the database with that first name!";

}

Post Reply