Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to retrieve data from the database (list of products) but I have a blank page when I execute the code.

<?php


$DB_HOST = "@@@";
$DB_NAME = "@@@";
$DB_PASS = "@@@";
$DB_USER = "@@@";


$db_obj = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
if($db_obj->connect_errno > 0) {
  die('Connection failed [' . $db_obj->connect_error . ']');
}

if($db_obj->connect_errno > 0){
    die('Unable to connect [' . $db_obj->connect_errno . ']');
}


?>



<html>
<head>
<title>List of products</title>
</head>
<body>
<?php


$query="SELECT * FROM product"; 
$result_obj = $db_obj->query($query);
$row=mysql_fetch_row($result);

echo $row;




?>

</body>
</html>

I am doing it right? where is the problem exactly? is the problem can be from the server side?

new try:

<?php
// session_start();
// require("mysqli.php"); 

$DB_HOST = "webdev.cs.kent.edu";
$DB_NAME = "ralsuhai";
$DB_PASS = "810646396";
$DB_USER = "ralsuhai";


$db_obj = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
if($db_obj->connect_errno > 0) {
  die('Connection failed [' . $db_obj->connect_error . ']');
}

if($db_obj->connect_errno > 0){
    die('Unable to connect [' . $db_obj->connect_errno . ']');
}


?>



<html>
<head>
<title>List of products</title>
</head>
<body>
<?php


$query="SELECT * FROM product"; 
$result_obj = $db_obj->query($query);
//first try
//while($row=$result_obj->fetch_row() )
//{
  //   var_dump($row);
 //}

 //second try
//$res = array();
//while($row = $result->fetch_array(MYSQLI_ASSOC))
//{
  //$res[] = $row;
//}
//print_r($res);




?>
<table><tr><th>Product Id</th><th>Name</th><th>Price</th></tr>
<?php
$result_obj = $db_obj->query($query);
while($row=$result_obj->fetch_row() )
{ ?>
    <tr>
     <td><?=implode('</td><td>',$row)?></td>
    </tr>
<?php
 }
?>
</table>
<?php










?>

</body>
</html>
share|improve this question
5  
You are mixing up mysql_* and mysqli_* functions. –  Shankar Damodaran Apr 3 '14 at 4:17
    
what is the different between both of them? –  user3483167 Apr 3 '14 at 4:19
2  
Difference is that they are both different –  Shankar Damodaran Apr 3 '14 at 4:20
    
@user3483167 first off, they can't cooperate. And mysqli_ supports prepared statements and object-oriented approach, while mysql_ cannot. –  Jan Dvorak Apr 3 '14 at 4:20
    
What Shankar meant by that is, those two functions do not mix. @user3483167 –  Fred -ii- Apr 3 '14 at 4:38

2 Answers 2

up vote 0 down vote accepted

You've got a couple problems here. First is, the variable $db_obj is a mysqli object and your trying to use the mysql_fetch row function on it. That won't work, next even if it did work, $row would be an array.try this...

?>
<table><tr><th>Product Id</th><th>Name</th><th>Price</th></tr>
<?php
$result_obj = $db_obj->query($query);
while($row=$result_obj->fetch_row() )
{ ?>
    <tr>
     <td><?php echo implode('</td><td>',$row); ?></td>
    </tr>
<?php
 }
?>
</table>
<?php

this should dump each row returned by the query and you can see what's going on. Note that the result from $db_obj->query($query) is an object. This should print a table with your data.

share|improve this answer
    
when I applied this code I got the following: "array(3) { [0]=> string(10) "prod_99004" [1]=> string(14) "Tennis Racquet" [2]=> string(5) "95.85" } array(3) { [0]=> string(10) "prod_99007" [1]=> string(12) "Tennis Shoes" [2]=> string(5) "75.95" } array(3) { [0]=> string(10) "prod_99008" [1]=> string(12) "Tennis Balls" [2]=> string(4) "3.85" } array(3) { [0]=> string(10) "prod_99009" [1]=> string(14) "Tennis T-shirt" [2]=> string(5) "15.85" }" –  user3483167 Apr 3 '14 at 4:33
    
perfect. That's what should be expected, now do as you wish with the data... What are you trying to do anyways? –  Bryan Apr 3 '14 at 4:36
    
I want to show them as a list. –  user3483167 Apr 3 '14 at 4:37
    
all of the information or just the name or name and price? –  Bryan Apr 3 '14 at 4:38
    
all the information in the product table is the data base. –  user3483167 Apr 3 '14 at 4:41
<?php
   $DB_HOST = "@@@";
   $DB_NAME = "@@@";
   $DB_PASS = "@@@";
   $DB_USER = "@@@";

   $db_obj = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
   if($db_obj->connect_errno) {
   die('Connection failed [' . $db_obj->connect_error . ']');
   }
?>
<html>
  <head>
    <title>List of products</title>
      </head>
      <body>
      <?php
         $query="SELECT * FROM product";
         $result_obj = $db_obj->query($query);
         $row=$result_obj->fetch_assoc();
         print_r($row);
      ?>
  </body>
 </html>    
share|improve this answer
    
showed a blank page too >< –  user3483167 Apr 3 '14 at 5:22
    
if I want to update a table in the database, I can use the following: <?php $query="UPDATE admin SET password=npassword"; { ?> <?php echo "your password has been updated!" ?> <?php } ?> –  user3483167 Apr 5 '14 at 2:45
    
<?php $query="UPDATE admin SET password=npassword WHERE user_id=2"; $query = mysql_query($query) or die (mysql_error()); ?> –  Maulik patel Apr 5 '14 at 10:39
    
Thanks you, I will try it. –  user3483167 Apr 8 '14 at 4:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.