0

Here is my PHP code:

include ('dbconnect.php');
   $sql = "SELECT * FROM products";
   $result = $conn->query($sql);

   if($result -> num_rows >0){
     while($row = $result -> fetch_assoc()){
echo "$row[productID]"."$row[productName]";

}else {
echo "0 results";
}
    $conn -> close();

Here is my dbconn.php code. I don't know where my error is, if it is in the dbconn.php or on the other php page.

<?php

$conn = mysql_connect("localhost","root","")
or die (mysql_error());

$conn = mysql_select_db("shoppingcart",$conn) or die (mysql_error());

?>
Chonchol Mahmud
  • 2,717
  • 7
  • 39
  • 72
  • You should avoid using `mysql_* ` functions altogether. They're error-prone and unsafe, and they are removed from PHP. – Chonchol Mahmud May 09 '16 at 10:16
  • should I replace it wtih mysqli ? Im really bothered by this – user3762945 May 09 '16 at 10:17
  • Yes, You should. May be even better using http://php.net/manual/en/book.pdo.php – xAqweRx May 09 '16 at 10:26
  • dbconnect.php is what you are including and your file name is dbconn.php. Rename file name and do require_once('dbconnect.php') or require_once('dbconn.php') whichever is your file name for precaution instead of using include. – Mukesh Ram May 09 '16 at 10:30
  • @xAqweRx I tried PDO method sir but I have an error of, INVALID CATALOG NAME no database selected. – user3762945 May 09 '16 at 10:54

1 Answers1

0

try this, avoid use of mysql instead of mysqli

include ('dbconnect.php');

   $sql = "SELECT * FROM your_table";

   $result = mysqli_query($conn, $sql);

   $count = mysqli_num_rows($result);
   if($count > 0) {
        while($row = $result->fetch_assoc()){
            echo $row['column1'] . $row['column2'] . "<br/>";
        }
   }

$conn -> close();

and this is connection code

$conn = mysqli_connect("localhost","root", "" ,"your_db");
if(!$conn) {
    die() . mysqli_error();
} else {
    //echo "connection successfull";
}
Hassan Farooq
  • 71
  • 2
  • 12