1

I have used a lot of function in my website projects. I had used mysql. now I want to convert it into mysqli but the problem I am facing is with mysql_result my fuction should return 1 valve only

// $mysqli is my database connection


function test($id){

$query = "SELECT name FROM members where id= '$id'"; 
 $result = mysqli_query($mysqli, $query);

$row = mysqli_fetch_array($result,  MYSQLI_ASSOC);


return $row;
}

$name = test(4);

echo $name;

when I do not use functions, just echo $row it gives correct result when I put $id =4;

3
  • Try return $row['name']; Commented Mar 15, 2015 at 5:33
  • thanx bheem Raj sab u and peter solve my problem Commented Mar 15, 2015 at 6:24
  • Please mark the answer as accepted if your question was addressed. Thanks. Commented Mar 15, 2015 at 7:49

1 Answer 1

1

If your function definition of test() is complete above then you do not have the value of $mysqli. You get that value when you connect to the database and you either need to make it global or pass it in:

function test ($id) {
    global $mysqli;

or

function test ($mysqli, $id) {
Sign up to request clarification or add additional context in comments.

4 Comments

Thankx Sir Peter u solved my problem but i cant vote due to reputation less
sorry 1 more problem #peter that i use 100 functions 3 or 4 time each there in project so i have to pass $mysqli every time is there any other way efficient and easy fast
I'm not aware of another way to handle this more efficiently or easily. You have to somehow get access to the variable within the function and the 2 ways to do that are by passing in a parameter or by using a global...
oh did now ok peter sir i thankx i will use any of 2 ways thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.