I make a website on video tutorials, I added the code there, it turned out on the video, I gave the same error (((

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in D:\appserver\www\shop\db_fns.php on line 26

Here is the code:

 <? function db_connect() { $host = 'localhost'; $user = 'shop_user'; $pswd = 'admin123'; $db = 'shop'; $connection = mysql_connect($host, $user, $pswd); mysql_query("SET NAMES utf-8"); if (!$connection || !mysql_select_db($db, $connection)) { return false; } return $connection; } function db_result_to_array($result) { $res_array = array(); $count = 0; while ($row = mysql_fetch_array($result)) { $res_array[$count] = $row; $count++; } return $res_array; } function get_products() { db_connect(); $query = "SELECT * FROM products ORDER BY id DESC"; $result = mysql_query($query); $result = db_result_to_array($result); return $result; } function get_cat_products($cat) { db_connect(); $query = "SELECT * FROM products WHERE cat='$cat' ORDER BY id DESC"; $result = mysql_query($query); $result = db_result_to_array($result); return $result; } function get_cat() { db_connect(); $query = "SELECT * FROM categories ORDER BY id DESC"; $result = mysql_query($query); $result = db_result_to_array($result); return $result; } function get_product($id) { db_connect(); $query = ("SELECT * FROM products WHERE id='$id' "); $result = mysql_query($query); $row = mysql_fetch_array($result); return $row; } ?> 

    1 answer 1

    This means that mysql_fetch_array($result) does not work.

    Those. $result possible itself is not passed. show the output of where you call this function.

     function db_result_to_array($result) { $res_array = array(); $count = 0; while ($row = mysql_fetch_array($result)) { $res_array[$count] = $row; $count++; } return $res_array; } 
    • I'm sorry, but I don't understand ((( - Alina
    • wrote the same "Show the output from where you call this function." - Artem
    • <? include ('db_fns.php'); include ('cart_fns.php'); session_start (); if (! isset ($ _ SESSION ['cart'])) {$ _SESSION ['cart'] = array (); $ _SESSION ['total_items'] = 0; $ _SESSION ['total_price'] = '0.00'; } $ view = empty ($ _ GET ['view'])? 'index': $ _GET ['view']; include ($ _ SERVER ['DOCUMENT_ROOT']. '/ shop / views / layouts / shop.php'); ?> - Alina
    • This is clearly the wrong code. I wrote, I need a code in which the call goes to the d-and db_result_to_array and not to connect them - Artem
    • switch ($ view) {case ('index'): $ products = get_products (); break; case ('cat'): $ cat = $ _GET ['id']; $ products = get_cat_products ($ cat); break; case ('product'): $ id = $ _GET ['id']; $ product = get_product ($ id); break; } - Alina