I am try calling a function in php but it's not calling and it show an error " Uncaught Error: Call to undefined function alldata() "

<?php $servername="localhost"; $username="root"; $password=""; try{ $conn = new PDO("mysql:host=$servername;dbname=practice_join",$username,$password); $conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION); echo "connection establish"; alldata($commection); function alldata($sd) { echo "hello"; } } catch(PDOException $e) { echo "conmection failed".$e->getMessage(); } ?>
  1. Your function has no business being in the try/catch block
  2. You need to pass the connection to the function.
1 Like

can you tell me How I should do it?

Just give it the parameter

alldata($commection);

after given parameter it again shown an error

Easy: Just solve it!

Do you expect us to guess which one?

No, I add a parameter as you tell me but nothing is happening and it is showing
Fatal error : Uncaught Error: Call to undefined function alldata() in C:\xampp\htdocs\connection.php: Stack trace: #0 {main} thrown in C:\xampp\htdocs\connection.php

And what’s your current code? Looks like you removed the function somehow.

I think maybe look at clarity, accuracy and syntax.
1 - displaying your code as a code block would be clearer.
2 - I may be wrong but you seem to have an extra } in your code
3 - You refer to your connection as connection, conmection and $commection all in the same code block, obviously not a real problem when displaying messages (but untidy)
4 - $commection is a different matter but I can’t see where the value is assigned so cant tell what it should be

But I am worried about accuracy in your syntax

Php allows function definitions to be placed after a call to the function (a bad programming practice), if the definition is not conditionally defined, otherwise the function definition must (properly) come before the function call. The code inside a try {…} and a catch () {…} block is however conditionally executed and any function definition inside those would need to be placed before the function call.

One of the points of user written functions is to simplify/de-clutter the main code. If you are putting the definition in-line in the main code, near where it is being called, you have not achieved this goal. Follow good programming practices and put any function definitions near the top of your code, preferably in an external .php file, then ‘require’ this file into your code.

Next, there’s no good reason for your code to catch and handle a database exception unless it’s for something that the visitor can do something about or should even know about. The only common things that fall into this category are when inserting/updating duplicate or out of range user submitted data. In all other cases, letting a visitor/hacker know that something they did caused a database error, and then displaying the internal error message on the site (a connection error contains the connection username and server path information), is just giving hackers useful information when they intentionally trigger errors.

Sponsor our Newsletter | Privacy Policy | Terms of Service