How to check if mysqli connection works?

I am really new to php and I am trying to create my own php shop basket. After some research, I myself am stuck in the "functional products" below, because it seems to me that it does not work correctly. I expect to see the names of my products in my mysql database, but it shows nothing. My username is noivaemd_etalhes, I use my correct password, and my database name is noivaemd_cart, and I created a table in this database called Products with my list of available products. Can someone help me figure out what I'm doing wrong in the php instructions below? I appreciate any help.

<?php 

session_start();
$page = 'index.php';

function products()  {
        $con = mysqli_connect("localhost", "noivaemd_etalhes", "mypassword", "noivaemd_cart") or die (mysqli_error());
        $res = mysqli_query($con, "SELECT id, name, description, price FROM Products WHERE quantity > 0 ORDER BY id DESC");
        if (mysqli_num_rows($res)==0) {
        echo "<font family=verdana><font size=6px><font color= #90882C><font style=normal><font variant= normal><br>No products available<br></font>";
            }
        else{
        while($get_row = mysqli_fetch_assoc($res)) {
                echo '<p>'.$res['name'].'</p>';
        }
    }
}

?>
+4
source share
2 answers

This code:

while ($get_row = mysqli_fetch_assoc($res)) {
    echo '<p>'.$res['name'].'</p>';
}

Must be:

while ($get_row = mysqli_fetch_assoc($res)) {
    echo '<p>'.$get_row ['name'].'</p>';
}

, , , mysqli, :

$con = mysqli_connect("localhost","my_user","my_password","my_db");

// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

: http://www.php.net/manual/en/function.mysqli-connect.php

+5

. "dbconnect.php" , . .

DB :

<?php
$mysql_host = "localhost"; 
$mysql_user = "username"; 
$mysql_pass = ""; 
$mysql_dbname = "Products"; 
$conn_error = "Sorry, Could Not Connect"; 

if(!@mysql_connect($mysql_host,$mysql_user,$mysql_pass)||!@mysql_select_db($mysql_dbname)){
    die($conn_error); 
}
?>

index.php php- 'require' dbconnect.php "; '.

:

$InfoQuery = " SELECT id, product, name FROM table_name WHERE quantity>0 ORDER BY id DESC"; 
    $Info = mysql_query($InfoQiery); 
    while($InfoRow=mysql_fetch_assoc($Info)){echo "<p>".$InfoRow['id']."<br>". $InfoRow['product']"."<br>". $InfoRow['name']."</p>";}

: , , while, $res, $get_row

0

All Articles