Take the 2-minute tour ×

This question already has an answer here:

Im trying to convert a project of mine from mysql to mysqli but it seems to give me a error

Warning: mysqli_query() expects at least 2 parameters, 1

this is my database connection

$mysqli = new mysqli();
$mysqli->connect('localhost', 'root', '', 'myscript');

this is the query

$sql = mysqli_query("SELECT * FROM settings WHERE id='1'") or die (mysqli_error());
$results = mysqli_fetch_array($sql);

if anyone can tell me how to fix this error i will be grateful. thanks in advance.

share|improve this question

marked as duplicate by mario, Bill Karwin, Soner Gönül, Leigh, andrewsi May 6 '13 at 3:59


The parameters to mysqli_query() are explained in the manual: php.net/mysqli_query (note: procedural style!) –  Pekka 웃 Feb 25 '13 at 18:53
Perhaps the manual could help: mysqli_query. The object-structured interface is easier to use, only the procedural call needs the resource/link param. –  mario Feb 25 '13 at 18:54
You established a connection using OO style. Why not continue that way? $sql = $mysqli->query(...); –  Michael Berkowski Feb 25 '13 at 18:54

2 Answers 2

You can try performing your query using Object oriented PHP way instead of mixing and matching Object oriented PHP and regular PHP:

$mysqli = new mysqli();
if($result = $mysqli->query("SELECT * FROM settings WHERE id='1'")){

    //DO STUFF

share|improve this answer
Thank you works like a charm –  nick cruse Feb 25 '13 at 18:58

Please, do not convert a project of yours from mysql to mysqli.
Convert it to PDO.
Mysqli is good only for the silly examples from beginner's manual - it will make your life harder as you progress.

So, instead of mysqli just use PDO.
It's almost the same:

$pdo = new PDO('mysql:dbname=myscript;host=localhost','root','', $opt);

$stm = $pdo->prepare("SELECT * FROM settings WHERE id=?");
$data = $stm->fetch();

Note parameretized query support - the main reason for such a move between drivers - which already used in this code.
Even in such small examples PDO is better. Way better. And with more complex ones mysqli will become totally unusable while PDO would be the same - quite ugly but at least feasible.

share|improve this answer
