Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9145621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:33:35+00:00 2026-06-17T10:33:35+00:00

I have a problem in calling my rand_id() function. here is code as <?php

  • 0

I have a problem in calling my rand_id() function. here is code as

<?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(100,103);          //i have record with id ='100'
$results=$conn->query("select id from student_personal_info where id='".$id."'");

if($results->rowCount()>0)
{   
rand_id();
}
else
{
return "this is unique id";
}
}
?>        

but if I remove the function and check this code working fine but now i can’t generate unique id message after checking with database…plz help……

Here is another code after removing function

<?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,103);          //i have record with 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();
}

?>        
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T10:33:37+00:00Added an answer on June 17, 2026 at 10:33 am

    Your database object $conn is out of scope in the rand_id() function. You could pass it as an argument:

    function rand_id($conn)
    {
        // ...
    }
    

    Or use the global method:

    function rand_id()
    {
        global $conn;
        // $conn is now accessible
    }
    

    More info about Variable Scope on the Manual.


    For future debugging, remember to check your error log. In this case you should have something like:

    Fatal Error: Call to a member function query() on a non-object in …

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem with calling the Match function from a VBA code: it
I have a serious problem with calling references in my php code. I wrapped
I have got a problem with calling a global function, which takes a pointer
Here's my problem: I have an unmanaged dll that I made.I'm calling one of
I have a problem with calling get_PrimaryKeys() function from msi.dll in c#. I want
I have problem calling a JavaScript function in an iframe from the parent page.
I have a problem when calling this function: function dobleevent(obj){ if (document.total.motivo.value){ document.total.resumen.click(); document.total[modificar][0].click();
i have a problem with calling an onclick function multiple times. The problem is
I have a problem calling an anonymous function with parameters passed as variables. If
I have a problem in calling a function from a function. These are the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.