Posted By

lorenz82 on 11/15/10


Tagged

mysql php


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

lorenz82


MySQL Important query


 / Published in: PHP
 

  1. //Connection
  2. $con = mysql_connect("localhost","root","password");
  3. mysql_select_db("my_db", $con);
  4.  
  5. //Creat DB
  6. mysql_query("CREATE DATABASE my_db",$con)
  7.  
  8. //Create table
  9. $sql = "CREATE TABLE Persons
  10. (
  11. id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
  12. FirstName varchar(15) NOT NULL,
  13. LastName varchar(15) NOT NULL,
  14. Age int,
  15. PRIMARY KEY (id)
  16. )";
  17. mysql_query($sql,$con);
  18.  
  19. //Insert row in table
  20. mysql_query("INSERT INTO Persons (FirstName, LastName, Age) VALUES ('Peter', 'Griffin', '35')");
  21. $insertedId=mysql_insert_id();
  22.  
  23. //Update row
  24. mysql_query("UPDATE Persons SET Age = '36' WHERE FirstName = 'Peter' AND LastName = 'Griffin'");
  25.  
  26. //Delete row
  27. mysql_query("DELETE FROM Persons WHERE LastName='Griffin'");
  28.  
  29. //Select one row
  30. $result = mysql_query("SELECT * FROM Persons WHERE FirstName='Peter'") or die(mysql_error());
  31. $row = mysql_fetch_array($result);
  32. echo $row['LastName'];
  33.  
  34. //Select more rows
  35. $result = mysql_query("SELECT * FROM Persons WHERE FirstName='Peter'") or die(mysql_error());
  36. while( $row = mysql_fetch_array($result) ){
  37. echo $row['LastName'];
  38. }
  39.  
  40. //Select more rows order by Age discending from the rows from 0 to 10
  41. $result = mysql_query("SELECT * FROM Persons WHERE FirstName='Peter' ORDER BY Age DESC LIMIT 0,10") or die(mysql_error());
  42. while( $row = mysql_fetch_array($result) ){
  43. echo $row['LastName'];
  44. }

Report this snippet  

You need to login to post a comment.