Programming Blog

This blog is about technical and programming questions and there solutions. I also cover programs that were asked in various interviews, it will help you to crack the coding round of various interviews

Saturday 6 January 2018

PHP database functions

PHP provides access to MySQL databases via a number of functions
We'll find six of them particularly useful. They are listed below in the order they will typically be used.
$db = mysql_connect($dbms_location)
Connects to the DBMS whose address is identified by the parameter, which should be a string. The exact string will depend on where exactly the DBMS is located; an example string islocalhost:/export/mysql/mysql.sock. The function returns a value that can later be used to refer to the DBMS connection.
mysql_select_db($db_name, $db)
Selects which database managed by the DBMS that this PHP connection will reference. The DBMS will have a name for each database, and the name should be passed as a string for the first parameter. The second parameter should be a reference to the DBMS connection as returned by mysql_connect.
$result = mysql_query($sql, $db)
Sends a query to the DBMS. The SQL code should be located in the string passed as the first parameter; the second parameter should be a reference to the DBMS connection as returned bymysql_connect. The function mysql_query returns a value that allows access to whatever the results were of the query; if the query failed — as for example would happen if there were an error in the given SQL — this return value would be FALSE.
mysql_error()
If the last executed query failed for some reason, mysql_error returns a string that describes this error. I recommend that you always make sure your PHP code somehow echoes the result of mysql_error when a query fails, so that you have a way to debug your script.
mysql_num_rows($result)
Returns the number of rows found by the SQL query whose results are encapsulated in the parameter. Of course, this could be 0 if there were no rows that resulted from the query.
mysql_result($result, $row, $col)
Returns a string holding the information found in row $row and column $col in the results that are encapsulated by the $resultparameter. Both rows and  columns are numbered starting from 0: That is, the first row is numbered 0, the second row 1, and so on, and similarly the first column is numbered 0.

No comments:

Post a Comment