1

I have a small website that I wanted to switch over to the mysqli_* functions, and after reading up a lot on it I decided to do the switch by doing a replace-all mysql_ with mysqli_ -- I then went through and verified that everything changed correctly...

Now, the mysqli_connect() works - (I get a valid resource connection back from it) but further down the PHP script I have a mysqli_query function that is returning NULL no matter what I put in as the SQL.

Any thoughts what could be happening?

The relevant code:

function connecti_database() {
  mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_DATABASE);
}

connecti_database();
$sql = 'SELECT * FROM table where id = 5';
$result = mysqli_query($sql);
var_dump($result); // this returns NULL every time
ccagle8
  • 308
  • 1
  • 2
  • 12
  • 1
    +1 for [converting your `mysql_*` functions to `mysqli`](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php#answer-12860140). – Joseph Silber Jan 27 '13 at 18:44

1 Answers1

4

mysqli functions require two parameters, not one
...and a developer required to read documentation first.

function connecti_database() {
  global $mysqli;
  $mysqli = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_DATABASE);
  mysqli_select_db($mysqli,DB_DATABASE);
}

connecti_database();
$sql = 'SELECT * FROM table where id = 5';
$result = mysqli_query($mysqli, $sql);

PHP Manual: http://php.net/manual/en/mysqli.query.php

mixed mysqli_query ( mysqli $link , string $query [, int $resultmode = MYSQLI_STORE_RESULT ] )
Peter
  • 16,453
  • 8
  • 51
  • 77
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • hmm... still getting NULL in the var_dump. (To be honest, I did try this exact thing before posting, but stupidly forgot 'global'.) – ccagle8 Jan 27 '13 at 18:53
  • boneheaded moment for me - i needed yet another global here. Thanks, it is working now. – ccagle8 Jan 27 '13 at 18:58