Results 1 to 4 of 4
Threaded View
-
8th May 2009, 06:02 PM #1OPMemberWebsite's:
litewarez.net litewarez.com triniwarez.comFetching Data from mysql database (Well Commented)!
ok so ill be showing you how to do the following:
- MySql Connection
- Get Results as a Resource
- Flip The Results to array
- Close Connection and clear memory.
Ok so lets see how its done
PHP Code:
<?php
//Connection
$Host = 'localhost'; // This is where the MySql Engine is hosted
$User = 'litewarez'; // This is the username that is used to login to MySql
$Pass = 'YouWishMate'; // This is the password that is used to authenticate with MySql
$DB = 'SampleDB'; //This is the database where all the information is stored!
//ok so we have all the information needed to connect to the database now we can connect
$ConnectionHandle = @mysql_connect($Host,$User,$Pass);
/*
ok so we have just run the mysql_connect() function and passed in the information
that is needed such as ($Host $User $Pass ).
if you notine the @ sybol before the mysql_connect(), what this does is stops
any errors showing on the screen if it fails..
this is great for security but not for debugging
*/
//Now we need to do some logic and see if the connection was successful
if($ConnectionHandle !== false){
$BaseHandle = @mysql_select_db($DB);
}
/*
Ok so now whats goin on here is at first we connected to the server
but we return the output of mysql_connect() into a variable so that
later on we can use the variable to check whether were connected!
we have the used the expression
<!--
if($ConnectionHandle !== false){
...
}
-->
if we are unable to connect to the mysql server
the value of $ConnectionHandle will be false
*/
//Ok so now we do some error checking to see if its safe to proceed and show the website
if($ConnectionHandle === false){
die("Please be patient, we are doing some work on our servers: Code:0");
}elseif($BaseHandle === false){
die("Please be patient, we are doing some work on our servers: Code:1");
}
/*
Ok so above what we are doing is firstly checking if we are not
connected to the server.
and then IF we are connected we check to see if we selected
the database without any issue
if everything goes smooth it will continue to execute the rest of our code :)
*/
//Here were going to create a string thats going to be
//used to select data from the database
$SelectString = "SELECT id,username,password FROM users WHERE id = %s";
$User_Id = "12";
/*
Ok so the first variable contains a MySql string that would usually
be used to select a particular user from the table "users"
the reason for the %s is that were going to be using a finction called
"sprinf()" witch will make the security easier and cleaner.
The $User_Id would usually be fetch ising GET or POST or SESSION but
for this example we just using a static number witch is 12
*/
//Lets run the command and fetch the results
$Data = @mysql_query( sprintf($SelectString , mysql_real_escape_string($User_Id) ) );
//Learn More about sprinf() at http://uk.php.net/manual/en/function.sprintf.php#function.sprintf
/*
Ok so now we should have the results as a resource and they would be held within $Data.
We cant just echo the results because echo is used to print strings and $Data is a
resources so we cant use that so were going to use a function that will get the results into an array
*/
$UserData = mysql_fetch_array($Data);
//were using Mysql_fetch_array because there's only one result help in there
//otherwise we would use mysql_fetch_ossoc to loop the results.
/*
Ok so now we should have an array containing id username and password witch is what we queried for erlier.
we going to print these out now.
*/
$BaseHTML = "
UserData:<br /><br />
User ID: %d <br />
User Name: %s <br />
User Pass: %s <br />
";
//This is just a basic html block that is just used with sprintf to
//show what data we have on the browser.
$HTML = sprintf($BaseHTML , $UserData['id'] , $UserData['username'] , $UserData['password']);
//Now we will echo the HTML block with the users information replacing the %s and %d
echo $HTML;
//No this is important when coding to high standers as you want to keep the website running at maximum performance.
@mysql_free_result($Data); // this will free the memory thats holding that result.
@mysql_close($ConnectionHandle); //this will close the connection to mysql and free the resources it was using.
/*
Well i hope you enjoyed this tutorial and please note that im not claiming
to be a perfect programmer and by using my techniques i cannot guarantee
the security to be perfect but to the best of my knowledge its pretty
secure and also this code is not tested as iv'e just typed it up right now.
*/
?>
Regards, Cornetofreaklitewarez Reviewed by litewarez on . Fetching Data from mysql database (Well Commented)! ok so ill be showing you how to do the following: MySql Connection Get Results as a Resource Flip The Results to array Close Connection and clear memory. Ok so lets see how its done Rating: 5Join Litewarez.net today and become apart of the community.
Unique | Clean | Advanced (All with you in mind)
Downloads | Webmasters
Notifications,Forum,Chat,Community all at Litewarez Webmasters
Sponsored Links
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Similar Threads
-
Problem my display data php mysql
By softpk in forum Web Development AreaReplies: 1Last Post: 22nd Apr 2012, 11:42 AM -
ODBC connection error for fetching value from MSsql to MYsql
By dhruvpandit in forum Technical Help Desk SupportReplies: 0Last Post: 9th Feb 2012, 10:23 AM -
Mysql fetch specific data from row (id)
By roadrunner in forum Web Development AreaReplies: 9Last Post: 23rd Aug 2011, 03:45 PM -
replacing mysql $link supplied data, a question?
By XSLTel in forum Web Development AreaReplies: 10Last Post: 23rd Jul 2011, 03:14 PM -
Get data from a corrupt database
By neilbla in forum Technical Help Desk SupportReplies: 3Last Post: 3rd Dec 2010, 12:36 AM
themaLeecher - leech and manage...
Version 5.03 released. Open older version (or...