Saturday, 6 June 2009

Working between SQL and PHP : Displaying data by SELECT syntax

Firstly, I create a page called test.php which has some code following:

// This is to connect to localhost server, it can be domain name hoặc IP address
$server = " localhost";

//account user name which logins MySQL server, is root
$userName = " root";

//account password to login mysql server
$password ="";

//Name of chosen database to be interacted with, is test
$dbName = "test";

//Inputting server, username and password into mysql_connect function
$conn
= mysql_connect($server, $username, $password);

with $conn is a connection which is done by mysql_connect statement.
I use IF statement to report error if it occur in the connection
if ( !$conn ) {
//Cant connect to mysql server, exit and report error
die("Cant connect to Mysql server");
} //end if

//choose a database for interaction by mysql_select_db statement
mysql_select_db($dbName, $conn);
or die("Can not choose a CSDL Database: ".mysql_error($conn));

Finally, close the connection with mysql_close statement.

//Close the connection
mysql_close($conn);

There are 2 ways to display data from MySQL database by mysql_fetch_row() statement or mysql_fetch_assoc() statement

In first practice, I use mysql_fetch_row()

so I have full code on database connection and retrieve data from MySQL database

$SERVER = "localhost";
$USERNAME = "root";
$PASSWORD = "";
$DBNAME = "test";

$conn = mysql_connect($SERVER, $USERNAME, $PASSWORD);
if ( !$conn ) {
//Cant not connect, exit and report error
die("Cant connect to MySQL server: ".mysql_error($conn));
} //end if

//choose database to work with
mysql_select_db($DBNAME, $conn)
or die("Cant select CSDL database: ".mysql_error($conn));

$sql = "SELECT * FROM groupCategory";
$result = mysql_query($sql, $conn);
if ( !$result )
die("Cant do your query: ".mysql_error($conn));

echo "Quantity of rows: ".mysql_num_rows($result)."
\n"
;

while ( $row = mysql_fetch_row()($result) ) {

echo "gID = ".$row[0]."
\n"
;
echo "gName = ".$row[1]."
\n"
;
echo "gDescription = ".$row[2]."
\n"
;} //end while

//We should free the memory after getting rows from SELECT statement
mysql_free_result($result);

//đóng kết nối
mysql_close($conn);


A full code in my PHP editor looks like:



In my second practice, I use mysql_fetch_assoc() statement
$SERVER = "localhost";
$USERNAME = "root";
$PASSWORD = "";
$DBNAME = "test";

$conn = mysql_connect($SERVER, $USERNAME, $PASSWORD);
if ( !$conn ) {
//Cant not connect, exit and report error
die("Cant connect to MySQL server: ".mysql_error($conn));
} //end if

//choose database to work with
mysql_select_db($DBNAME, $conn)
or die("Cant select CSDL database: ".mysql_error($conn));

$sql = "SELECT * FROM groupCategory";
$result = mysql_query($sql, $conn);
if ( !$result )
die("Cant do your query: ".mysql_error($conn));

echo "Quantity of rows: ".mysql_num_rows($result)."\n";

while ( $row = mysql_fetch_assoc($result) ) {
echo "gID = ".$row['gID']."
\n"
;
echo "gName = ".$row['gName']."
\n"
;
echo "gDescription = ".$row['gDescription']."
\n"
;
} //end while
//We should free the memory after getting rows from SELECT statement
mysql_free_result($result);

//đóng kết nối
mysql_close($conn);

Screen of full code:



Notice:

mysql_fetch_row() fetches one row of data from the result associated with the specified result identifier. The row is returned as an array. Each result column is stored in an array offset, starting at offset 0. (Hàm mysql_fetch_row() sẽ trả về 1 array mà phần tử thứ [0] sẽ tương ứng với cột đầu tiên của table, phần tử thứ [1] sẽ tương ứng với cột thứ hai của table)

mysql_fetch_assoc() fetches a result row as an associative array.
If two or more columns of the result have the same field names, the last column will take precedence. To access the other column(s) of the same name, we either need to access the result with numeric indices by using mysql_fetch_row() or add alias names
Note: Performance
An important thing to note is that using mysql_fetch_assoc() is not significantly slower than using mysql_fetch_row(), while it provides a significant added value

Both mysql_fetch_row() statement and mysql_fetch_assoc() statement give the same result:



or another way to view data by the code below




The result will be viewed:




However, the table will not be in order, to fix this problem, I replace :




The result now looks much nicer

No comments:

Post a Comment