-1

So here is my code:

$sql = "SELECT * FROM `items`";
$result = mysqli_query($conn, $sql);

while($prommes = mysql_fetch_array($result)){
}

When I'm running it. I'm getting this error:

mysql_fetch_array() expects parameter 1 to be resource.

Is it something wrong with my database?

And here is my connection which Irequire in the html document. Can it be something here which makes the query not working?

$servername = "localhost";
$username = "root";
$password = "";

// Create connection
$conn = mysqli_connect($servername, $username, $password);


// Check connection
  if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} 
// echo "Connected successfully";
mymiracl
  • 583
  • 1
  • 16
  • 24
Transk
  • 7
  • 1

2 Answers2

0

You never select a database, as far as I can tell, so $result is not a resultset (because the query never executes properly). It is probably false instead. You can var_dump it to verify that. Try adding the database name in the query. If your database is named db, for example:

SELECT * FROM `db`.`items`;

If you don't want to explicitly specify the database name every time, you'll just want to add the database name to the MySQLi connection string, as shown: http://php.net/manual/en/mysqli.query.php

Redbeard011010
  • 954
  • 6
  • 20
0

In the while loop, mysql_fetch_array is incorrect. It should be

while($prommes = mysqli_fetch_array($result))

Additonally your connection needs to be

$conn = mysqli_connect($servername, $username, $password, $database);

where $database is the name of your database (not the hostname).

As an aside, this can be better avoided using an object orientated approach. So for example, your connection could be;

$conn = new mysqli($servername, $username, $password, $database);

and your query and loop could be;

$result = $conn->query($sql);

while($prommes = $result->fetch_array())

Hope this helps.

worldofjr
  • 3,868
  • 8
  • 37
  • 49
  • For a more detailed explaination of using OO mysqli, see [this answer](http://stackoverflow.com/a/26476208/3899908) – worldofjr Dec 27 '15 at 02:38
  • And between mysqli_query() and while(mysqli_fetch_... [there must be error handling](http://stackoverflow.com/questions/2973202/mysql-fetch-array-expects-parameter-1-to-be-resource-or-mysqli-result-boole). Every query can fail at any time. – VolkerK Dec 27 '15 at 02:59
  • @worldofjr where can I find the name of my database? Im using XAMP to setup it through localhost. It is not the name of the table that I want to use as "$database", right? – Transk Dec 27 '15 at 12:59
  • @Transk You're probably best installing phpMyAdmin so you can see your database structure. – worldofjr Dec 27 '15 at 13:05