Query a database and return results ::function::

Information for PHP. Hypertext Preprocessor
Post Reply
dzcadii
Posts: 94
Joined: Sun Mar 01, 2015 4:36 am

Query a database and return results ::function::

Post by dzcadii » Sun Mar 01, 2015 6:32 pm

Simple function that queries a database and returns results

Code: Select all

<?
function get_my_info($passed_var){

//link to your database right here
$link=mysql_connect("databasehost","databaseuser","databasepassword");
$sel_db=mysql_select_db("databasetoworkwith");

$query_string = "SELECT * FROM database_table WHERE field='$passed_var'";
$query = mysql_query($query_string);
$num_rows = mysql_num_rows($query);
if($num_rows == "0"){
echo "Query error";
exit;
}
else{
$my_info = mysql_fetch_assoc($query);
//You can do below or
print_r($my_info);

while($my_info = mysql_fetch_assoc($query)){
extract($my_info);
echo $file_name;
}
}
}
}
?>
Call the function like this

Code: Select all

<?
function get_my_info($passed_var="username");
?>

Post Reply