NOKIA

pages of blog

Thursday 10 July 2014

use RANDOM function in php , with a database (by class)

hello ,
the RANDOM function is the best and easy . read here
but what is the interest ? 

we use this edea by writting a script PHP .
if the result of random function exist at our database that we will create then do no thing 
else add this result value to this database
we create a database . called RND . If you do not know how to creat a database read here
and , we create a table called  rnd_table , Contains Only one field 
called  val : int(20) 
ok .

Such as habit we create a scripte PHP for Contacting to our database RND
but in   this cour we use the  class . 
if you do not know what is a class read PHP class 
we create a file (Ourclass.php) .
<?php
 class contact_db{
public $localhost="localhost";
    public $username="root";
    public $password="root";
    public $db_name='rnd';
    public $tbl_name="rnd_table";
  public $r=array();
 function __construct(){ // this function is Present in the time of calling to class
 mysql_connect($this->localhost,$this->username,$this->password) or die ('can not connect to data base ');
mysql_select_db($this->db_name) or die('can not select database ');
echo $db_name;
}
 function get_elements_db(){ // this function get all elements of database and puts them in array
$sql="SELECT * FROM $this->tbl_name";
$result=mysql_query($sql);
$num=mysql_num_rows($result);
if ($num==0) echo "sory , no elements !!";
else {
$j=0;
while ($rr=mysql_fetch_array($result)){
$r[$j]=$rr['val'];
echo $r[$j]."<br>";
 $j++;
}}}
}
?>
 
ok , the scripte ourclass.php give all values from table of database that we create , 
if no data , then it show (sory , no elements !!) 

we creat a new file called (index.php) and we use the last scripte befor give a random value for Every time .
but , here we add a test , in our script if the value exist at our database show true else show false

to be continued ... :) 


 

No comments:

Post a Comment