0

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.

Leigh
  • 12,859
  • 3
  • 39
  • 60
nick cruse
  • 141
  • 2
  • 5
  • 15
  • The parameters to mysqli_query() are explained in the manual: http://php.net/mysqli_query (note: procedural style!) – Pekka Feb 25 '13 at 18:53
  • 2
    Perhaps the manual could help: [`mysqli_query`](http://php.net/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
  • 1
    You established a connection using OO style. Why not continue that way? `$sql = $mysqli->query(...);` – Michael Berkowski Feb 25 '13 at 18:54

2 Answers2

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();
$mysqli->connect('localhost', 'root', '', 'myscript');
if($result = $mysqli->query("SELECT * FROM settings WHERE id='1'")){

    //DO STUFF

    $result->close();
}else{
     printf("Error: %s\n", $mysqli->error);
}
Tucker
  • 7,017
  • 9
  • 37
  • 55
1

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:

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

$stm = $pdo->prepare("SELECT * FROM settings WHERE id=?");
$stm->execute(array(1));
$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.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345