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!

 
 

Creating Databases

<?php $conn = @mysql_connect( "localhost", "userName", "password" ) or die( "Sorry – could not connect to MySQL" );$rs1 = @mysql_create_db( $_REQUEST[’db’] );$rs2= @mysql_list_dbs($conn);$list = "";for( $row=0; $row < mysql_num_rows( $rs2 ); $row++ ) { $list .= mysql_tablename( $rs2, $row ) . " | "; } ?><html> <head> <title>Creating databases</title> </head> <body> <form action="<?php echo( $_SERVER[’PHP_SELF’] […]
Continue reading…

Enjoyed this post? Share it!

 
 

Mysql_List_Processes.Php

<?php mysql_connect("localhost","root",""); $processes = mysql_list_processes(); echo "<table>"; echo "<tr><th>ID</th><th>User</th><th>Host</th><th>DB</th> <th>Command</th><th>Time</th><th>State</th><th>Info</th> </tr>"; while ($row = mysql_fetch_row($processes)) { list($id,$user,$host,$db,$command,$time,$state,$info) = $row; echo "<tr><td>$id</td><td>$user</td> <td>$host</td><td>$db</td><td>$command</td> <td>$time</td><td>$state</td><td>$info</td> </tr>"; } echo "</table>"; ?>
Continue reading…

Enjoyed this post? Share it!

 
 

Using Mysqli_Error() And Mysqli_Errno()

<?php $link = mysqli_connect("hostname", "username", "password", "mydatabase"); if(!$link) { $mysql_error = "Connection error: ".mysqli_connect_error(); die($mysql_error); } $result = mysqli_query($link, "SELECT * FROM foo"); if(!$result) { $errno = mysqli_errno($link); $error = mysqli_error($link); die("Query Error: $error (code: $errno)"); }?>
Continue reading…

Enjoyed this post? Share it!

 
 

Function Mysql_Close() Closes The Connection.

If the link_id is not specified, mysql_close() will close the most recently opened link. The syntax is: int mysql_close ([int link_id])<? @mysql_connect("localhost", "web", "asdf") or die("Could not connect to MySQL server!"); @mysql_select_db("company") or die("Could not select company database!"); print "You’re connected to a MySQL database!"; mysql_close(); ?>
Continue reading…

Enjoyed this post? Share it!

 
 

Multiple Queries Using Mysqli

<?php $mysqli = new mysqli("localhost", "username", "password","mydatabase", 3306); $queries = "SELECT * FROM mytable; SELECT * FROM anothertable"; if(mysqli_multi_query($mysqli, $queries)) { do { if($result = mysqli_store_result($mysqli)) { while($row = mysqli_fetch_row($result)) { foreach($row as $key => $value) { echo "$key => $value<br/>\n"; } } mysqli_free_result($result); } if(mysqli_more_results($mysqli)) { echo "<br/>\nNext result set<br/>\n"; } } while(mysqli_next_result($mysqli)); } […]
Continue reading…

Enjoyed this post? Share it!

 
 

Using Transactions In Mysqli

<?php $mysqli = new mysqli("localhost", "username", "password", "mydatabase", 3306); $query = "CREATE TEMPORARY TABLE friends (name VARCHAR(50), age INT) TYPE=InnoDb"; mysqli_query($mysqli, $query); mysqli_autocommit($mysqli, false); $friends = array( array("name" => "Max", "age" => 22), array("name" => "Cliff", "age" => 45), array("name" => "Hollie", "age" => 18)); foreach($friends as $friend) { $query = "INSERT INTO friends VALUES(‘{$friend[‘name’]}’,{$friend[‘age’]})"; […]
Continue reading…

Enjoyed this post? Share it!

 
 

Connecting User

<?php$domain = "localhost"; $user = "userName"; $password = "password";$conn = mysql_connect( $domain, $user, $password );if($conn) { $msg = "Congratulations $user, You connected to MySQL"; } ?><html> <head> <title>Connecting user</title> </head> <body> <h3> <?php echo( $msg ); ?> </h3> </body></html>
Continue reading…

Enjoyed this post? Share it!

 
 

Get Column Name, Type And Max Length

  <?php mysql_connect("mysql153.secureserver.net","w3mentor","password"); mysql_select_db("w3mentor"); $query = "SELECT * FROM Employee LIMIT 1"; $result = mysql_query($query); $fields = mysql_num_fields($result); for($count=0;$count<$fields;$count++) { $field = mysql_fetch_field($result,$count); echo "<p>$field->name $field->type ($field->max_length)</p>"; } ?>
Continue reading…

Enjoyed this post? Share it!