N
N
nugmani2017-01-08 09:01:36
PHP
nugmani, 2017-01-08 09:01:36

How to connect to db in php file using OpenServer?

Throws an error Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in D:\OpenServer\domains\PHPNews\data.php on line 25
Indicates a while loop. But the reason is not in him, because. $result contains no data from the database. I assume that connection to a DB does not work. What could be the reason?

<?php

require_once 'classes.php';
$publications = array();

$con = mysqli_connect("localhost","root","","testsite2");

if (mysqli_connect_errno())
{
    echo "Faild to connect to MySQL:" .mysqli_connect_error();
}

$result = mysqli_query($con, "SELECT * FROM publications");

while ($row = mysqli_fetch_array($result))
{
       $publications[] = new $row['type']($row);
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
DevMan, 2017-01-08
@nugmani

if the connection did not work, then "Faild to connect to MySQL:" would fall out.
error due to mysqli_query returning false :

Returns FALSE on failure. If the SELECT, SHOW, DESCRIBE, or EXPLAIN queries succeed, mysqli_query() will return a mysqli_result object. For other successful queries, mysqli_query() will return TRUE.
php.net/manual/ru/mysqli.query.php#refsect1-mysqli...
use php.net/manual/ru/mysqli.error.php to debug.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question