Jump to content

new to mysqli and putting it into a Function


EricOnAdventure
Go to solution Solved by ginerjm,

Recommended Posts

I've been working on moving everything my mysql to mysqli, and for the most part it has been going smoothly, one exception is an error I am having now while putting it into a function. It worked just fine outside of the function, but inside I am getting an error. Here let me show you.

<?php


function findit($column,$table)
{
require_once '../dbstart.php';
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

$query = "SELECT $column FROM $table";
$result = mysqli_query($conn, $query);

$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
return $row[$column];


/* close connection */
mysqli_close($conn);
}

echo findit('familyname','test');


?> 

I get

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\efun\unisearch\SQLi.php on line 15

 

keep in mind before making it into a function I got exactly what I wanted. Any idea what I am doing wrong?

Thanks.

Link to comment
Share on other sites

That means that your query failed. Add error checking/handling to your code.

 

A few other things to note:

- You cannot have any code after your return statement. Return causes the function to end right there, everything after it would be ignored.

- You shouldn't be opening and closing a connection for each query. Open the connection once at the start of the script. Let PHP close it automatically at the end of the script.

  • Like 1
Link to comment
Share on other sites

Like kicken already said, PHP automatically closes the connection. Closing it manually a few microseconds earlier serves no purpose.

 

While we're at it: Remove the last “?>” tag as well. It's not only useless but harmful. The closing tag makes PHP switch back into output mode, so if there's just one space after the tag, PHP will in fact send it to the client. And as you probably know, once there's output, it's no longer possible to start a session, issue a cookie or set an HTTP header. This is a huge problem when an included script accidentally starts output (like the already mentioned space) and blocks the entire main script.

 

So just get rid of the tag. It's only needed when you actually want to write HTML markup after the PHP code.

  • Like 1
Link to comment
Share on other sites

  • Solution

Does $column contain any spaces?

 

One should really wrap associative indices in quotes to ensure there is no problem in determining the correct element of the array it is used on.

 

$row["$column"] would be correct.

 

BTW - you are doing a query that selects all rows but only returning the first row's value. Is that what you want?

Link to comment
Share on other sites

You shouldn't really have a require_once inside a function - the point of functions is to be re-usable and if you try to re-use a require_once call it's just going to throw warnings up at you.  It would be better to require_once your script at the top of the page and then pass the connection object into the function as part of the call.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.