Database Functions
Uit Covide
The file “common/functions_pear.php” has functions for database connection. Listed below are the ways how the query are executed and listed using the functions.
We will use the following query for illustrations::
$sql = “select username, password from users where xs_usermanage = 1”;
Inhoud |
Execute a query
sql_query() function takes string query as input and returns the result for select statement, or affected rows for other statements and prints sql error if the query is not correct.
$res = sql_query($sql);
The $res variable will be a resource table which selects username and password fields from users table having user manager authorities.
Fetch Data
sql_fetch_row(), sql_fetch_array(), sql_fetch_assoc() are the functions for fetching the result that take the result variable as input and returns the array of data according to which functions we used. This functions prints error, if there are any.
while($row = sql_fetch_assoc($res))
{
echo $row[‘username’].”</ br>”;//The tasks related to fetched data can be done here
}
Count the number of rows
sql_num_rows() functions takes result variable as input and returns the number of records in the result.
$res = sql_num_rows($res);
Get sql insert id
The sql_insert_id() functions takes the table name as input and returns the latest auto-increment index of the table.
$id = sql_insert_id($table);
