View Single Post
Old 06-03-2005, 02:27 PM   #1 (permalink)
jaswinder_rana
The Acquainted
 
Join Date: May 2005
Posts: 106
Thanks: 0
jaswinder_rana is on a distinguished road
Default PHP/MySQL tips and tricks

Hi,
This thread is dedicated to all the tips and trick to ease the usage of PHP/MySQl.

Feel free to list or ask about any Trick you use or want to know.

print_r()
This function great to debug large arrays. It can be used for string or other variables too, but its real handy with arrays. in every project i always make a function then include that file(which includes that function) in every file. it makes it real easy to debug problems.
The normal output from this function is really cluttery and is hard to figure things out. But, when used with <pre></pre> tags, its realy useful.
function printR($var)
{
echo '<pre>';
print_r($var);
echo '</pre>';
}
Will list more functions as i remember them.

Also point out any errors that i migh thave made.

Reading from a file

Ther are many methods of reading from a file. it depends what you want to do.
1) file('filename'). it reads the whole file and return an array with each line as new index in array

2) file_get_contents('filename'). it returns the whole file.

the above 2 are useful if you just wana read the file. it saves you from the hassle of opening, reading and closing files.

also, its always a good idea to check if the file exists, before doing anything with it. it just saves you from making errors. so you do something like
if(file_exists('filename'))
echo file_get_contents('filename');

Writing to a file

file-put_contents('filename','data') will write the 'data' to the file. This again saves fromthe hassle of opening, writing and closing the file.

mysql_errno()

You can use this feature to tackle with the MySQL errors.
for eg.
PHP Code:
function dbSelect($dbName)
{
   if(!@
mysql_select_db($dbName))
    return 
FALSE;
  else
   return 
TRUE;
}
function 
dbErrorNo()
{
  return 
mysql_errno();
}
if(!
dbSelect('wrong_db_name'))
{
  if(
dbErrorNo() == '1049')
  {
    echo 
'Database selection problem.';
  }

Why this is handy?
Suppose, you have to insert a new user in database and before inserting you have to check, if that username already exists or not. To check this, first you have to run a a select statement to see if MySQL returns a record with that username. If records is returned it means user already exists. If not then we can safely insert the record in database.

Now it needs to access database twice, which takes up resources (especially on large scale website).

So, now we'll use our new solution. For this new solution to work, the username field should be set to unique, it means, no other user can have the same username.

Then we run our insert statement. If the username already exists, then it won't let the query run and will generate an error, which will say "Duplicate entry 'username' for key 1". and the error number will be 1062;
now we can say
PHP Code:
if(dbError() == '1062')
{
  echo 
'Sorry, username already exists.';

This will save some processing time.
__________________
---------------------------
Errors = Improved Programming.
Portfolio

Last edited by jaswinder_rana : 06-04-2005 at 02:42 PM.
Send a message via MSN to jaswinder_rana
jaswinder_rana is offline  
Reply With Quote