Connect to MySql and select database using PHP

The following code is an example of how to connect to a mysql server and select a database to query. We should always free up resources you don’t need anymore i.e make sure that mysql_close() executes at the end. This makes your code more efficient and reduces the possibility of errors. <?php $DB_HOST = "localhost"; […]
Continue reading…

Enjoyed this post? Share it!

 
 

PHP Script to backup tables from mysql database

This script allows you to backup Mysql database and its tables. It is configured by default to Easyphp locally. <?php $Database_name = ‘somedb’; $table = ‘sometable’;   Define (’H’, ‘localhost’); define (’N’, ‘root’) define (’P’,’password’); define (’T’, $database_name); Mysql_connect (H, N, P) or die (mysql_error ()); mysql_select_db (T) or die (mysql_error ());   function Escape($v){return […]
Continue reading…

Enjoyed this post? Share it!

 
 

PHP function that will help you fill a list from any table in your database

function listmytablerows($table, $name, $field, $where, $textID) { / / Connect to the database and query execution connect (); $Sql = "select * from". $table. "". $where. "ORDER BY". $field; $Req = mysql_query($sql); $Res = mysql_num_rows($req); ?> <Select name = "<?php echo $name; ?>" id="<?php echo $textID; ?>"> <option value=""> ____</ option> <? Php / / […]
Continue reading…

Enjoyed this post? Share it!

 
 

Simple tutorial on php mysql

Hello, here is a little tutorial on managing databases using PHP To connect to your database we can use the function mysql_connect () that accepts three arguments: * The host name * User Name * Password This function returns the MySQL connection id on success or FALSE on failure <?php $connection = mysql_connect ("localhost", "root", […]
Continue reading…

Enjoyed this post? Share it!

 
 

Retrieve and display data using MySQLi Object Interface with PHP

The following is an example on how to retrieve and display data using MySQLi Object Interface with PHP. $mydb = new mysqli(’localhost’, ‘username’, ‘password’, ‘databasename’); $sql = "SELECT * FROM users ORDER BY lname, fname"; $result = $mydb->query($sql); while( $row = $result->fetch_assoc() ){ echo $row[’fname’] . " " . $row[’lname’] ; echo " made these […]
Continue reading…

Enjoyed this post? Share it!

 
 

Using MySQLi Object Interface with PHP to insert data

The following is an example of using MySQLi Object Interface with PHP to insert data into a database table. $mydb = new mysqli(’localhost’, ‘username’, ‘password’, ‘databasename’); $sql = "INSERT INTO users (fname, lname, comments) VALUES (‘$_POST[fname]’, ‘$_POST[lname]’, ‘$_POST[comments]’)"; if ($mydb->query($sql) == TRUE) { echo "user entry saved successfully."; } else { echo "INSERT attempt failed" […]
Continue reading…

Enjoyed this post? Share it!

 
 

Building Queries On The Fly

<?php function opendatabase ($host,$user,$pass) { try { if ($db = mysql_connect ($host,$user,$pass)){ return $db; } else { throw new exception ("Sorry, could not connect to mysql."); } } catch (exception $e) { echo $e->getmessage (); } }   function selectdb ($whichdb, $db){ try { if (!mysql_select_db ($whichdb,$db)){ throw new exception ("Sorry, database could not be […]
Continue reading…

Enjoyed this post? Share it!

 
 

Mysql_Fetch_Row() Fetches Rows

Its syntax is: array mysql_fetch_row() (int result)<? @mysql_connect("localhost", "root","") or die("Could not connect to MySQL server!"); @mysql_select_db("mydatabase") or die("Could not select database!"); $query = "SELECT * FROM mytable"; $result = mysql_query($query); print "<table>\n"; print "<tr>\n<th>ID</th><th>Title</th><th>MyValue</th>\n</tr>\n"; while (list($id, $title, $myvalue) = mysql_fetch_row($result)) : print "<tr>\n"; print "<td>$id</td>\n<td>$title</td>\n<td>$myvalue</td>\n"; print "</tr>\n"; endwhile; print "</table>"; mysql_close(); ?>
Continue reading…

Enjoyed this post? Share it!

 
 

Parameter Binding In Mysqli

<?php $mysqli = mysqli_connect("hostname", "user", "pass", "database"); if(mysqli_connect_errno()) { die("Could not connect: ".mysqli_connect_error()); } /* CREATE TABLE books(name VARCHAR(255), isbn VARCHAR(10), price FLOAT) */ $bookname = "PHP"; $bookisbn = "11111111111"; $bookprice = 49.95; $stmt = mysqli_prepare($mysqli, "INSERT INTO books VALUES(?, ?, ?)"); mysqli_bind_param($stmt, "ssd", $bookname, $bookisbn, $bookprice); mysqli_execute($stmt); mysqli_stmt_close($stmt); mysqli_close($mysqli);?>
Continue reading…

Enjoyed this post? Share it!

 
 

Storing Information In A Database

A sample user authentication table (user_authenticate)USER ID USERNAME PASSWORDur1234 brian 111ur1145 tom 222<?if (!isset($PHP_AUTH_USER)): header(’WWW-Authenticate: Basic realm="Secret"’); header(’HTTP/1.0 401 Unauthorized’); exit; else : mysql_connect ("localhost", "root", "") or die ("Can’t connect to database!"); mysql_select_db ("user_info") or die ("Can’t select database!"); $query = "select userid from user_authenticate where username = ‘$PHP_AUTH_USER’ and password = ‘$PHP_AUTH_PW’"; $result […]
Continue reading…

Enjoyed this post? Share it!

 
 

Creating A Database In Mysql

  <?php $host="mysql153.secureserver.net"; $uname="w3mentor"; $pass="password";   $connection= mysql_connect ($host, $uname, $pass); if (!$connection) { die ("A connection to the server could not be established!"); } $result=mysql_create_db ("mydatabase"); if (!$result) { die ("database could not be created!"); } echo "Database megabase was created successfully."; ?>
Continue reading…

Enjoyed this post? Share it!

 
 

Sending Sql Queries To A Mysql Database Using The Mysql_Query () Function

  <?php $host="mysql153.secureserver.net"; $uname="w3mentor"; $pass="password"; $database="w3mentor";   $connection= mysql_connect ($host, $uname, $pass) or die ("Database connection failed!"); $result=mysql_select_db ($database) or die ("Database could not be selected"); $query = "drop table if exists mytable"; $result = mysql_query ($query) or die ("Query failed."); ?>
Continue reading…

Enjoyed this post? Share it!

 
 

Use Form To Add Data To Database

  <html> <body> <?phpif (isset($_POST[’submit’])): $dbcnx = mysql_connect(’mysql153.secureserver.net’, ‘java2s’, ‘password’); mysql_select_db(’java2s’); $name = $_POST[’name’]; $email = $_POST[’email’]; $sql = "INSERT INTO Employee SET FirstName=’$name’, EMail=’$email’ , ID = ‘3’";   if (@mysql_query($sql)) { echo('<p>New employee added</p>’); } else { echo('<p>Error adding new employee: ‘ . mysql_error() . ‘</p>’); } ?><p><a href="<?=$_SERVER[’PHP_SELF’]?>">Add another Author</a></p><?php else: // […]
Continue reading…

Enjoyed this post? Share it!

 
 

Execute Query With Odbc_Execute().

Its syntax is: int odbc_execute (int result_ID [, array parameters])<? $connect = @odbc_connect("myAccessDB", "user", "secret") or die("Could not connect to ODBC database"); $query = "UPDATE customers set cust_id = \"Inc.\" WHERE cust_id \"2\""; $result = odbc_prepare($connect, $query) or die("Couldn’t prepare query!"); $result = odbc_execute($result) or die("Couldn’t execute query!"); odbc_close($connect); ?>
Continue reading…

Enjoyed this post? Share it!

 
 

Listing Tables

<?php $conn = @mysql_connect( "localhost", "userName", "password" ) or die( "Sorry – could not connect to MySQL" );$rs1= mysql_list_dbs( $conn );for( $row=0; $row < mysql_num_rows( $rs1 ); $row++ ) { $this_db = mysql_tablename( $rs1, $row ); $list .= "<b>".$this_db."</b><br />"; if( $this_db != "mysql" ) { $rs2 = mysql_list_tables( $this_db );   for( $num=0; $num […]
Continue reading…

Enjoyed this post? Share it!