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!

 
 

A Function To Open A Connection To Mysql

<?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 closedatabase ($db){ mysql_close ($db); }   $db = opendatabase ("localhost","root","");   try { if (!mysql_select_db ("mydatabase",$db)){ […]
Continue reading…

Enjoyed this post? Share it!

 
 

Function Mysql_Result() Produces A Data Set.

Its syntax is: int mysql_result (int result_id, int row [, mixed field])<? @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); $x = 0; print "<table>"; print "<tr><th>ID</th><th>Title</th><th>MyValue</th></tr>"; while ($x < mysql_numrows($result)) : $id = mysql_result($result, $x, ‘id’); $name = […]
Continue reading…

Enjoyed this post? Share it!

 
 

Opening A Connection And Selecting A Database

<html> <head> <title>Opening a Connection 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!

 
 

Store Query Result In An Associate Array

  <?php mysql_connect("mysql153.secureserver.net","w3mentor","password"); mysql_select_db("w3mentor"); $query = "SELECT id, firstname FROM Employee"; $result = mysql_query($query); while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) { $name = $row[’name’]; $developerid = $row[’id’]; echo "Product: $name ($id) <br />"; }?>
Continue reading…

Enjoyed this post? Share it!

 
 

Create New Table In Mysql

  <?php $linkID = mysql_connect(’mysql153.secureserver.net’, ‘java2s’, ‘password’); mysql_create_db(’new_db’, $linkID); mysql_select_db(’new_db’);$query = "CREATE TABLE new_table (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, new_col VARCHAR(25))"; $result = mysql_query($query); $axe = mysql_drop_db(’new_db’); ?>
Continue reading…

Enjoyed this post? Share it!

 
 

Listing All Rows And Fields In A Table

<html> <head> <title>Selecting Data</title> </head> <body> <?php $user = "root"; $pass = ""; $db = "mydatabase"; $link = mysql_connect( "localhost", $user, $pass ); if ( ! $link ) { die( "Couldn’t connect to MySQL: ".mysql_error() ); }mysql_select_db( $db, $link ) or die ( "Couldn’t open $db: ".mysql_error() );$result = mysql_query( "SELECT * FROM domains where […]
Continue reading…

Enjoyed this post? Share it!

 
 

Using The Mysqli Object-Oriented Api

<?php $mysqli = new mysqli ("localhost","root","","mydatabase"); try { if (mysqli_connect_errno()){ throw new exception ("Error: " . mysqli_connect_errno() . " – ". mysqli_connect_error());   } else { if ($cdquery = $mysqli->query ("SELECT * FROM mytable ORDER BY id ASC")){   while ($cddata = $cdquery->fetch_array ()){ echo "ID: " . $cddata[’id’] . "<br />"; echo "Title: " […]
Continue reading…

Enjoyed this post? Share it!

 
 

Function Odbc_Exec() Accomplishes The Roles Of Both Odbc_Prepare() And Odbc_Execute().

Its syntax is: int odbc_exec (int connection_ID, string query)<? $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_close($connect); ?>
Continue reading…

Enjoyed this post? Share it!

 
 

Mysql_Fetch_Field.Php

<?php mysql_connect("localhost","root",""); mysql_select_db("mydatabase"); $query = "SELECT * FROM mytable 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!