Hi everybody, i have problem in my php code (it just showing nothing....means no error no results).......can anybody tell me what is wrong with this code.....i'm new to PHP........here is my code

<?php 

try
{


$config=array(
'DB_USERNAME'=>'root',
'DB_PASSWORD'=>''
);
$conn=new PDO('mysql:host=localhost;dbname=scc',$config['DB_USERNAME'],$config['DB_PASSWORD']); 
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo rand_id(); 
}
catch(Exception $e)
{
echo 'error: '.$e->getMessage();
}

function rand_id()
{
$id=rand(1200,9600);
$results=$conn->query("select * from student_personal_info where id='".$id."'");
if($results->rowCount()>0)
{   rand_id();  }
else
{
    return $id; }
}


?>

plz help me waiting for reply..................

Hi,

You cannot use the random integers from 1200 to 9600 and expect a result from it. The probability on getting a match is (Xb - Xa) to the power of (xb-xa) times the number of digits combination.

Of course, script is not going to return something that it is not there. Unless, you want to manually testing it by allowing the script to run and choose a random integer and then create a new query ONLY based on the random integer choosen.

I strongly suggest to use the random integer only for the purpose of generating a unique id, instead of using it to find a match on what is already on the database. The problem is that you are pushing your database server too hard just to find one entry from multitude of probable integer combination.

This is acceptable of course ( code wise ), if you are building some kind of md5 decoder that will penetrate a high-end website, but for the purpose of student enrollment system, I believe you need to re-write your codes to something simple e.g. 10 random student id out of 100 random integer. The approximate probabilty of getting the exact sequence is about 1 for almost every 3.5 million refresh of the script.

Member Avatar for diafol

"select * from student_personal_info ORDER BY RAND() LIMIT 1"

You could try the above to get a random record. OR if you have a big table, run two queries, the first to get the count of records, get a random number from this, then do a custom LIMIT clause:

"select count(*) from student_personal_info"

Get the number as $cnt, then

$rec_no = mt_rand(0, $cnt-1);

then run the query:

"select * from student_personal_info LIMIT $rec_no, 1"

here is a simple function that will generate a random integers based on the number of digits you defined. This will be a lot easier on your mysql server.

    function generate_id($range_a, $range_b, $digits)
            {
            return rand($range_a, pow($range_b, $digits));
            }

Usage sample, say we want a 8 digits random number between 1 and 100, we can call the function above like this

        echo generate_id( 1 , 100 , 8 );

The function above demonstrate the flexibility and control of the output. You can define any combination of min. and max (range_a and range_b), and the number of digits

php reference pow function.

UPDATE! stackoverflow has a better solution in controlling the number of digits. My function does not work all the time. I missed it by few more codes.. :)

thanks veedeoo and diafol for reply.........i'm agree with u veedeeo but what i want to ask is if i simply returns something it runns fine but when i'm checking the generated id against the database it just showing nothing ....no exception,no error, no ouput.......... but if i comment out the sql part it ruuning as i want.....what i'm doing wrong here ?
......i'm doing this to get a unique id that is not already in the database. i got the point of not quering dtabase for only one unique id but i think it is better to provide students a unique id after checking in database rather then showing error on next page which is not a fault of user because i'm generating the unique id.
i'm using this value in html textfield where it is auto filled with php generated random unique id.

ok.. hold on let me run your codes in my debugger........

i tihnk there is some problem in my function calling as i change my code and remove the rand_id() function now the query statement working fine and i can check whether the generated id is unique or not but i can't generate random unique id after checking my database again and again .......help plz
here is my code

<?php 

try
{
$config=array(
'DB_USERNAME'=>'root',
'DB_PASSWORD'=>''
);


$conn=new PDO('mysql:host=localhost;dbname=scc',$config['DB_USERNAME'],$config['DB_PASSWORD']); 
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$id=rand(100,102);          // i have  a record having id='100'

$results=$conn->query("select id from student_personal_info where id='".$id."'");

if($results->rowCount()>0)
{   
    echo "generate again";
    }
else
{
    echo "this is unique id";
    }

}
catch(Exception $e)
{
echo 'error: '.$e->getMessage();
}


?>

hey i got the answer i was not using global connection object here in my case $conn after passing the $conn object in function now it working fine

thanks everyone.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.