Linking to a file or displaying an image from remote server is very common practice these days. But problem occurs when the file is not there on the remote server and our page starts showing a broken link. For this problem I have few solutions that will help you in avoiding this issue.
Solution # 1: Using file_exists()
Syntax
file_exists(string $filename)
Whenever we want to access the data in the mysql database using php, we have to create a connection to that particular database. Once the connection is establish we can insert and retrieve any data from the mysql database.
Creating a Mysql connection is quite simple in php, a simple built-in php function "mysql_connect()" is used for this purpose.
Syntax:
mysql_connect(servername,username,password);Where servername is the name of the server to connect, username and password are the credentials used to connect to the database.
Selecting a Database:
Once a connection is established to the database, we have to select the particular database that we want to use for inserting and fetching the data. This can be done by using another built-in php function "mysql_select_db()".
Syntax:
mysql_select_db(database,connection)Where database is the name of the database and connection is the connection created in the earlier part of this article.
Working Example:
Since connecting and selecting the database is required each time we want to use a database. So we can save the code for creating the connection and selecting the database in a file and include it wherever we want to use the database. You can name that file as "connection.php" or whatever other name you want. Contents of this file are as follows:
<?php
$db_host="localhost";
$db_user="root";
$db_pass="";
$db_name="any_db";
$con=mysql_connect($db_host,$db_user,$db_pass) or die ('Can not connect to database');
mysql_select_db($db_name,$con) or die ('Can not Select Database');
?>If you are using this code on the local server then the above mentioned configuration will work fine, as the default host/server name is always "localhost", user name is always "root" and password is always "" for the local server, but if you are using it on some web server then you have to change these settings to your web server's settings. The db_name is the name of the database that you want to select.Well I guess this is all for this lesson, for any queries, should you have, feel free to comment and ask.
Databases are very important in the field of web development. A database is a collection of data in a very organized fashion. By organized fashion it is meant that the data will be stored in different tables. So that it will be easier to retrieve the date when needed. The most common database used with php is Mysql. Mysql is a relational database. A relational database is one in which tables are linked together.
What is a Table?
A table is an structure in which the datat is stored, It can contain rows and columns. Columns are used to categorize the data and rows contain the actual data. For example if a company contains its user's data in a table named 'Users' then the table structure may look like this:
| User Id | User Name | Password | |
| 1 | John | john@example.com | 123 |
| 2 | Smith | smith@example.com | 456 |
In the above example, A company's users table is shown, it contains 4 columns and 2 rows. 4 columns are the categories and 2 rows are actual data. It is not mandatory that each row contains all the columns, some times rows can contain empty columns.
What is a Query?
A query is an statement used to manipulate the data in the database. You can write queries to Insert, retrieve, delete, and update the data and also to perform any action on the data present in the database.A sample query may look like this:
SELECT email FROM usersThe above statement is used to retrieve all the emails from the users table. So the output of the above query will be like this:
| john@example.com |
| smith@example.com |
Well I guess that's all for the introduction, further details will be discussed in the later lessons. For any queries or questions should you have, feel free to comment.
Sometimes we need to check whether a file or directory exists on server or not, well this can be done by a built-in php function "file_exixts". This function takes the path of the file as argument and check on the server for file, if the file exists on server it returns true and if it is not then it returns false. The syntax and example usage of this function is stated below:
Syntax
bool file_exists ( string $filename )Example usage
<?php $file1="example.php"; if (file_exists($file1)) echo $file1." exists on the server"; else echo $file1." does not exist on the server"; ?>You can test this example by placing a file named "example.php" in the same directory as the file containing the example code.
Reference
http://php.net/manual/en/function.file-exists.php
For any queries and issues should you have, please feel free to comment and ask.
Popular Posts
- Step by Step setting Transactional Replication in SQL Server 2008 R2
- How to check if a file exists on remote/local server using php
- Php Array Pagination Script
- How to encrypt decrypt url or code in php
- How to configure Reporting Services 2008 in SharePoint 2010
- Enable double escaped sequence in IIS 7
- Storage Area Networks PART 1
- How to create dynamic rss feed using php mysql
- Mysql Php Pagination Script
- How to check a file exists on server using php
Categories
- Databases (3)
- htaccess (3)
- IIS (1)
- javascript (2)
- mysql (3)
- php for beginners (14)
- php scripts (10)
- robots.txt (1)
- rss (2)
- SharePoint (3)
- SQL Scripts (5)
- Storage (2)
- VbScripts (1)
- Windows Administration (1)











