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!

 
 

Create Table And Install Data

  <? define("DBName","test"); define("HostName","localhost"); define("UserName","root"); define("Password","");   if(!mysql_connect(HostName,UserName,Password)){ echo "Cannot get connected to the database ".DBName."!<br />"; echo mysql_error(); exit; } mysql_select_db(DBName); // Creating the t table. If it already exists, // "@" overrides the error message @mysql_query("create table t(id int, a double)"); // Inserting 10 records into the table for($i=0; $i<10; $i++){ $id=time(); mysql_query("insert […]
Continue reading…

Enjoyed this post? Share it!

 
 

Mysqli_Multi_Query.Php

<?php $mysqli = new mysqli("127.0.0.1", "root","", "mydatabase"); $userid = $_SESSION[’userid’]; $query = "SELECT lastname, firstname FROM user WHERE userID=’$userid’;"; $query .= "SELECT product_count, CONCAT(‘$’,total_cost) FROM sales WHERE userID=’$userid’"; if($mysqli->multi_query($query)) { do { $result = $mysqli->store_result(); while ($row = $result->fetch_row()) echo "$row[0], $row[1] <br />"; if ($mysqli->more_results()) echo "<br />"; } while ($mysqli->next_result()); } ?>
Continue reading…

Enjoyed this post? Share it!

 
 

Selecting A Database

  <?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_select_db ("w3mentor");   if (! $result) { die ("database could not be selected"); }   echo "Database w3mentor is ready for use."; ?>
Continue reading…

Enjoyed this post? Share it!

 
 

Use For Loop To Read All Query Result

  <?php mysql_connect("mysql153.secureserver.net","w3mentor","password"); mysql_select_db("w3mentor"); $query = "SELECT id, firstname FROM Employee"; $result = mysql_query($query); for ($count=0; $count <= mysql_numrows($result); $count++) { $id = mysql_result($result, $count, "id"); $name = mysql_result($result, $count, "firstname"); echo "$firstname ($id) <br />"; }?>
Continue reading…

Enjoyed this post? Share it!

 
 

Discovering Which Extension Is Being Used

<?php function mysqlinstalled (){ if (function_exists ("mysql_connect")){ return true; } else { return false; } } function mysqliinstalled (){ if (function_exists ("mysqli_connect")){ return true; } else { return false; } }   if (mysqlinstalled()){ echo "<p>The mysql extension is installed.</p>"; } else { echo "<p>The mysql extension is not installed..</p>"; } if (mysqliinstalled()){ echo "<p>The […]
Continue reading…

Enjoyed this post? Share it!

 
 

Listing Databases

<?php$conn = @mysql_connect( "localhost", "userName", "password" ) or die( "Sorry – could not connect to MySQL" ); $rs= @mysql_list_dbs( $conn ) or die( "Sorry – could not list databases" );for( $row=0; $row < mysql_num_rows($rs); $row++ ){ $db_list .= mysql_tablename( $rs, $row ) . "<br />"; } ?><html> <head> <title>Listing databases</title> </head> <body> <h3> <?php echo( […]
Continue reading…

Enjoyed this post? Share it!

 
 

Sqlite_Field_Name.Php

<?php $sqldb = sqlite_open("mydatabase.db"); $results = sqlite_query($sqldb,"SELECT * FROM employee"); echo "Field name found at offset #0: ".sqlite_field_name($results,0)."<br />"; echo "Field name found at offset #1: ".sqlite_field_name($results,1)."<br />"; echo "Field name found at offset #2: ".sqlite_field_name($results,2)."<br />"; sqlite_close($sqldb); ?>
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!