|
|
|
Home Credits Affliates Link Exchange HTMLshaman PHPshaman LINUXshaman Helpful Links Web Design Images Backgrounds Icon Sets GIF Images |
Simple Scripts : MySQL Query Normally we do not work with HTML and PHP alone, some times we have to work with the SQL database and belwo we will project to you how we can connect to the MySQL server, look for a specific table, find a specific number and its corresponding vale of interest and then display it on the web browser. Below is an example of a query script and if you are going to use this please replace the connection details with that of your actual login details. <?php // Connection details $sql_host = "NAME_OF_HOST"; $sql_user = "NAME_OF_SQL_USER"; $sql_pass = "PASSWORD_TO_SQL"; $sql_db = "NAME_OF_SQL_DB"; // Data to use when connection of a SQL Database $db_connect = mysql_connect("$sql_host", "$sql_user", "$sql_pass"); // Asking the script to connect to SQL mysql_select_db("$sql_db", $db_connect); // Data to use when trying to get a value from a specific location $rowp_access = "SELECT wanted_column FROM occuring_table WHERE id_number = specific_number"; // The SQL Query process $row_printout = mysql_query($row_access, $db_connect); // Processing the data to be printed $needed_value = mysql_fetch_assoc($row_printout); // Displaying it on the web browser echo needed_value['wanted_column']; ?> The above should be self explaining with the comment tags that we have placed, however if you still don't get what it does, below is a more detailed explaination. // Connection details $sql_host = "NAME_OF_HOST"; $sql_user = "NAME_OF_SQL_USER"; $sql_pass = "PASSWORD_TO_SQL"; $sql_db = "NAME_OF_SQL_DB"; The above serve as to faciliate the connection process and could be reused if need be throughout the script. Where you need to replace the connection details with that of your own. NAME_OF_HOST is usually "localhost" but can be anything from your IP address to your URL depending on how your server is set up. NAME_OF_SQL_USER will be the SQL user name that has access to the Database that you want to connect to. PASSWORD_TO_SQL is the password for that particular SQL user that was stated earlier. NAME_OF_SQL_DB is the name of the database you want to connect to. // Data to use when connection of a SQL Database $db_connect = mysql_connect("$sql_host", "$sql_user", "$sql_pass"); mysql_connect tells PHP to connect to the SQL server with the data that is set to it, where in this case, when $db_connect is called for, it will do a connection to the SQL server. // Asking the script to connect to SQL mysql_select_db("$sql_db", $db_connect); This part is where we tell the script to connect to the SQL server using the connection details and also telling it which Database we are requesting information to. // Data to use when trying to get a value from a specific location $rowp_access = "SELECT wanted_column FROM occuring_table WHERE id_number = specific_number"; The above is used to specifiy what we want from the Database after we have connected to it. wanted_column is the name of the column in the database that the information is in but we don't know the values. occuring_table is the name if the table that this information is on. id_number is another column on the table that we have information on and also know the value. specific_number is the value that is known to us. NOTE : the value that we are interested in is on the same row for wanted_column and id_number // The SQL Query process $row_printout = mysql_query($row_access, $db_connect); Now that we know where is the specific table, row and the colum to take this data, we have to query the database to tell it that we are going to select the information. // Processing the data to be printed $needed_value = mysql_fetch_assoc($row_printout); Either mysql_fetch_assoc or mysql_fetch_array can be used and is used to process the data for printing or display on the web browser. // Displaying it on the web browser echo $needed_value['wanted_column']; Like the print command, echo will also display the command on the web browser. Note that we have to once again specify which column that this data is on.
|
| Our Recommended Links |
|
|
Contact us at webmaster@galacnet.com for advertising opportunities on GalacNet. |