0

now i have a function in PHP to retrieve data from my database

function get_category_posts($cat_name) {
    global $conn;
    $sql = "SELECT * from posts where cat_name = '".$cat_name."' order by date desc";
    $result = mysqli_query($conn, $sql);
    $row = array();
    for($i = 0; $i < mysqli_num_rows($result); $i++) {
        $row[] = mysqli_fetch_array($result,MYSQL_ASSOC);
    }
    return $row;
 }

and I have a .php file where i'm calling this function

$result = get_category_posts("Актуальные новости");
foreach ($result as $row) {
    // do something
}

my db connection

$conn=new mysqli("localhost", "ss", "ss", "s");
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}   

db include

include_once("functions.php");  

But the problem is, nothing displayed on my file. Sorry for my bad english ^_^

DerVO
  • 3,679
  • 1
  • 23
  • 27
ATIKON
  • 427
  • 2
  • 13
  • 1
    Is it your exact code _blah blah blah_ ? Also did you include file before calling get_category_posts function ? – Niklesh Raut Mar 10 '16 at 09:20
  • 1
    Check if Your SQL calls return some error - see http://php.net/manual/en/mysqli.error.php – Roman Hocke Mar 10 '16 at 09:23
  • question edited, error_log empty – ATIKON Mar 10 '16 at 09:27
  • 2
    can you add a `var_dump($result)` after your function call to see, what is returned? – DerVO Mar 10 '16 at 09:31
  • Result of var_dump -> `array(0) { }` – ATIKON Mar 10 '16 at 09:35
  • You might also want to look at [`mysqli_fetch_all()`](http://php.net/manual/en/mysqli-result.fetch-all.php) rather than building your own array in the for loop. – Dezza Mar 10 '16 at 09:35
  • 1
    Are you using buffered results sets? `mysqli_num_rows` will fail if thats the case. Retrieve your result using `while(mysqli_fetch_row...` – Nadir Mar 10 '16 at 09:36
  • 1
    For some reason, everybody checks for connection errors and afterwards assume that running queries can never fail. Also, your code is apparently wide open to SQL injection. – Álvaro González Mar 10 '16 at 09:39

1 Answers1

1

Add this before your query

mysqli_query($conn, 'SET NAMES utf8');

Your text which you pass as a condition is not English and should be treated special so your database encoding should be utf8 and add this line in main file to call before running queries:

mysqli_query($conn, 'SET NAMES utf8');
$result = mysqli_query($conn, $sql);
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Gouda Elalfy
  • 6,888
  • 1
  • 26
  • 38