0

This is my code using OO style:

$query = "SELECT * FROM articles LIMIT 1";
$result=mysqli_query($db, $query) or die("Could Not execute query.");
$row = $result->fetch_assoc()
echo $row;

Code using Procedural:

$query = "SELECT * FROM articles LIMIT 1";
$result=mysqli_query($db, $query) or die("Couldn't execute query.");
$row = mysqli_fetch_row($result);
echo $row;

I have tried various different ways of getting this to work and the query never seems to execute.

I am new to mysqli from mysql and am struggling with some of the differences. Apologies if there is an obvious answer to this question.

Any Help much appreciated.

mychalvlcek
  • 3,956
  • 1
  • 19
  • 34

2 Answers2

0

Try:

$query  = "SELECT * FROM articles LIMIT 1";
$mysqli = mysqli_connect("example.com", "user", "password", "database");
$res = mysqli_query($mysqli, $query);
$row = mysqli_fetch_assoc($res);

or:

$mysqli = new mysqli("example.com", "user", "password", "database");
$res = $mysqli->query($query);
$row = $res->fetch_assoc();
John Conde
  • 217,595
  • 99
  • 455
  • 496
0

Assuming your

 $db = mysqli_connect("localhost", "user", "password", "database");

then you can have

$query  = "SELECT * FROM articles LIMIT 1";
$res = $db->query($query);
$row = $res->fetch_assoc();
Niranjan N Raju
  • 12,047
  • 4
  • 22
  • 41
Tim Chosen
  • 41
  • 8