90 likes | 238 Views
PHP SQL. PHP SQL. Connection code:- mysql_connect (" server", "username", "password "); Connect to the Database Server with the authorised user and password. Eg $connect = mysql_connect (" localhost","root ","") or die("Cannot Connect"); l ocalhost server name r oot username
E N D
PHP SQL • Connection code:- • mysql_connect("server", "username", "password"); • Connect to the Database Server with the authorised user and password. • Eg • $connect = mysql_connect("localhost","root","") or die("Cannot Connect"); • localhost server name • root username • “ ” password • die display error message when cannot connect
PHP SQL • mysql_select_db("database_name") • Selecting a database within a connected database server. • Eg • mysql_select_db(“phpdb") or die("Database not found"); • phpdb database name • die display error message when data base not exisit
PHP SQL • mysql_query('TYPE_HERE_YOUR_MYSQL_QUERY') • This function is used to run specific queries on our database • Eg • $query= mysql_query("select * From login_table WHERE username='$username'"); • Login_table table name
PHP SQL • $numrows = mysql_num_rows($query); • Count no of row that satisfies the query
PHP SQL • Login page • <html> • <form action='login.php' method='POST'> • User name <input type='text' name='username'><br> • Password <input type='password' name='password'><br> • <input type='submit' name='Log in'> • </form> • </html>
PHP SQL • 'login.php‘ page • <?php • $username = $_POST['username']; • $password = $_POST['password']; • if($username && $password)//check username and password null • { • $connect = mysql_connect("localhost","root","") or die("Cannot Connect"); • mysql_select_db(“phpdb") or die("Database not found"); • $query= mysql_query("select * From login_table WHERE username='$username'"); • $numrows = mysql_num_rows($query); • echo $numrows; • } • else • die("Please enter username and Password!"); • ?>
PHP SQL • <?php • $username = $_POST['username']; • $password = $_POST['password']; • if($username && $password) • { • $connect = mysql_connect("localhost","root","") or die("Couldn't connect!"); • mysql_select_db("phpdb") or die("Couldn't find db"); • $query= mysql_query("SELECT * From login_table WHERE username='$username'"); • $numrows = mysql_num_rows($query); • if ($numrows!=0) • { • while($row = mysql_fetch_assoc($query)) • { • $dbusername = $row['username']; • $dbpassword = $row['password']; • } • if($username==$dbusername&&$password==$dbpassword) • { • //code for in • echo "You're in!"; • } • else • echo "Incorrect password!"; • } • else • die("That user does't exist!"); • } • else • die("Please enter username and Password!"); • ?>