Skip to main content

Posts

Showing posts with the label Mysql

Read an image from database using PHP

PHP program to Read image from Database.  <?php header("content-type:image/jpeg");     mysql_connect("servername","username","password"); mysql_select_db("databasename");  $sql = "select * from tablename"; $data = mysql_query($sql);     while($rec=mysql_fetch_row($data))    {      echo "$rec[1]<br>";    }  ?>  ------------------------------------------------------------- Note: When we using XAMPP or WAMP, servername = localhost, username = root, password is empty.

Save image to database using PhP

 <?php   if(isset($_POST['sub']))   {   $cont =   file_get_contents($_FILES['f1']['tmp_name']);  $type = $_FILES['f1']['type'];         $cont=addslashes($cont);      mysql_connect("servername","username","password"); mysql_select_db("dbname");   $sql="insert into tablename values ('','$cont','$type')";  if(mysql_query($sql))  echo "Inserted";  else  echo mysql_error();  } ?>  <form method="post" action="" enctype="multipart/form-data">  <input type="file" name="f1"> Image : <input type="submit" value="Upload" name="sub"> </form> -------------------------------------------------------------------- Note: When we using XAMPP or WAMP, servername = localhost, username = root, password is empty.

Fetch data from database using PHP

PHP program to fetch records from the table in Database. <?php $conn = mysqli_connect('servername','username','password','databasename'); if(!$conn) {  die('Connection failed!'.mysqli_error($conn)); } $sql = "SELECT * FROM tablename";   $data = mysqli_query($conn,$sql);   while($rec = mysqli_fetch_row($data)) { echo "$rec[0]<br>"; echo "$rec[1]<br>"; echo "$rec[2]<br>"; } ?>  ------------------------------------------------------------- Note: When we are using XAMPP or WAMP, servername = localhost, username = root, password is empty.

Insert records to database using PHP

Today i am going to write a program to Insert records into the table in Database. <?php  $conn =mysqli_connect('servername','username','password','databasename');  if(!$conn) {  die('Connection failed!'.mysqli_error($conn));  } $sql = "INSERT INTO tablename('sno','name','pwd') VALUES('101','Richy','Richy123')";    if(mysqli_query($conn,$sql)) {  echo "Record Inserted";  } else {  echo mysql_error(); } ?> ------------------------------------------------------------- Note: When we using XAMPP or WAMP, servername = localhost, username = root, password is empty.

Connect to server and select database using php

<?php if(!mysql_connect('servername','username','password'))  {      die('Connection failed!'.mysql_error());  }       if(!mysql_select_db("dbname"))  {  die('Database unavailable'.mysql_error());  }  ?> ------------------------------------------------------------- Note: When we are using XAMPP or WAMP, servername = localhost, username = root, password is empty

Create a database with PHP and Mysql

<?php  if(! mysql_connect('servername','username','password'))  {     die('Connection failed!'.mysql_error());  } $sql = "create database newdb"; if(mysql_query($sql)) {  echo "Database created"; } else {   echo mysql_error(); } ?> ------------------------------------------------------------- Note: When we are using XAMPP or WAMP, servername = localhost, username = root, password is empty.