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!

 
 

Delete Data From Database By Id

  <html> <body> <?php $cnx = mysql_connect(’mysql153.secureserver.net’,’w3mentor’,’password’); mysql_select_db(’java2s’);$id = ‘1’; $ok1 = @mysql_query("DELETE FROM Employee WHERE ID=’$id’"); if ($ok1) { echo('<p>Author deleted successfully!</p>’); } else { echo('<p>Error deleting author from database!<br />’. ‘Error: ‘ . mysql_error() . ‘</p>’); }?> </body> </html>
Continue reading…

Enjoyed this post? Share it!

 
 

Int Mysql_Num_Rows ( Resource Result )

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, […]
Continue reading…

Enjoyed this post? Share it!

 
 

Sqlite Error Constants

SQLITE_OK No error occurred.   SQLITE_ERROR SQLite error (or database not found).   SQLITE_INTERNAL An internal SQLite error.   SQLITE_PERM Access permission denied.   SQLITE_ABORT Callback routine aborted.   SQLITE_BUSY The database file is currently locked.   SQLITE_LOCKED A table within the database is locked.   SQLITE_NOMEM SQLite memory allocation error.   SQLITE_READONLY An attempt […]
Continue reading…

Enjoyed this post? Share it!

 
 

Using Php’s Odbc Functions To Interface With Ms Access

<? $connect = odbc_connect("ContactDB","","") or die("Couldn’t connect to datasource."); $query = "SELECT First_Name, Last_Name, Cell_Phone, Email FROM Contacts";$result = odbc_prepare($connect,$query);odbc_execute($result); odbc_result_all($result,"border=1"); odbc_free_result($result);odbc_close($connect); ?>
Continue reading…

Enjoyed this post? Share it!

 
 

Adding A Row To A Table

<html> <head> <title>Adding a Row to a Database</title> </head> <body> <div> <?php $user = "root"; $pass = ""; $db = "mydatabase"; $link = @mysql_connect( "localhost", $user, $pass ); if ( ! $link ) { die( "Couldn’t connect to MySQL: ".mysql_error() ); } print "<h2>Successfully connected to server</h2>\n\n"; @mysql_select_db( $db ) or die ( "Couldn’t open […]
Continue reading…

Enjoyed this post? Share it!

 
 

Function Mysql_Fetch_Array() Accomplishes The Same Result As Mysql_Fetch_Row(), By Default It Assigns A Returned Row To An Associative Array.

array mysql_fetch_array (int result [, result_type])<? @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);while ($row = mysql_fetch_array($result)) : print $row["id"]; endwhile; mysql_close(); ?>
Continue reading…

Enjoyed this post? Share it!

 
 

Odbc_Free_Result(): Restore Any Resources.

Its syntax is: int odbc_free_result (int result_id)<? $connect = @odbc_connect("myAccessDB", "user", "secret") or die("Could not connect to ODBC database!"); $query = "SELECT * FROM customers"; $result = odbc_exec($connect, $query) or die("Couldn’t execute query!"); odbc_result_all($result, "BGCOLOR=’#c0c0c0′ border=’1’") odbc_free_result($result); odbc_close($connect); ?>
Continue reading…

Enjoyed this post? Share it!

 
 

While Loop And Mysql_Fetch_Array() Function

<html> <head> <title>Get data</title> </head> <body> <?php $conn = @mysql_connect( "localhost", "userName", "password" ) or die( "Err:Conn" ); $rs = @mysql_select_db( "my_database", $conn ) or die( "Err:Db" ); $sql = "select id,first_name from my_table order by first_name"; $rs = mysql_query( $sql, $conn ); while( $row = mysql_fetch_array( $rs ) ) { echo( "ID: " . […]
Continue reading…

Enjoyed this post? Share it!

 
 

Connect To Mysql Database

  <?php // $hostname="mysql153.secureserver.net"; $username="w3mentor"; $password="password"; $dbname="w3mentor"; $usertable="Employee"; $yourfield = "FirstName";mysql_connect($hostname,$username, $password) OR DIE ("Unable to connect to database! Please try again later."); mysql_select_db($dbname);$query = "SELECT * FROM $usertable"; $result = mysql_query($query); if($result) { while($row = mysql_fetch_array($result)){ $name = $row["$yourfield"]; echo "Name: ".$name; } } ?>
Continue reading…

Enjoyed this post? Share it!

 
 

Get Data From Database Query

  <html> <body> <ul> <?php $cnx = mysql_connect(’mysql153.secureserver.net’,’w3mentor’,’password’); mysql_select_db(’java2s’);$employees = @mysql_query(’SELECT ID, FirstName FROM Employee’); if (!$employees) { die('<p>Error retrieving employees from database!<br />’. ‘Error: ‘ . mysql_error() . ‘</p>’); }while ($employee = mysql_fetch_array($employees)) { $id = $employee[’ID’]; $name = htmlspecialchars($author[’Name’]); echo("$name <br />"); echo("$id <br />"); }?> </ul></p> </body> </html>
Continue reading…

Enjoyed this post? Share it!

 
 

Reference Table Columns By Index

  <html> <body> <?php $host="mysql153.secureserver.net"; $uname="w3mentor"; $pass="password"; $database="w3mentor"; $tablename="Employee";   $connection= mysql_connect ($host, $uname, $pass) or die ("<br />Database connection failed! <br />"); $result=mysql_select_db ($database) or die ("<br />Database could not be selected<br />"); $query = "SELECT * from Employee"; $result = mysql_query ($query); echo "<table>"; echo "<tr"; echo " <td> <center><b>ID </b></center></td>\n"; echo " […]
Continue reading…

Enjoyed this post? Share it!

 
 

Delete Data From Database Table

  <?php mysql_connect("mysql153.secureserver.net","w3mentor","password"); mysql_select_db("w3mentor"); $rowID = "001"; $query = "DELETE FROM Employee WHERE ID=’$rowID’"; $result = mysql_query($query); if ((mysql_affected_rows() == 0) || mysql_affected_rows() == -1) { echo "<p>There was a problem deleting some of the selected items.</p>"; exit; } ?>
Continue reading…

Enjoyed this post? Share it!