-4

I'm not a professional coder. I'm just trying to make my own website. I did the following steps below, but it didn't work.

1- I created categories for the blog at localhost database.

http://i.hizliresim.com/3jqE3M.jpg

2- I created database.php as below.

<?php 

mysql_connect("localhost","root",""); 

mysql_select_db("mycms"); 

?>

3- I added the following codes to home.html

<?php 
include("includes/database.php"); 

$get_cats = "select * from `categories`"; 

$run_categories = mysql_query($get_cats); 

while ($cats_row=mysql_fetch_array($run_cats)) { 

$cat_id=$cats_row['cat_id']; 
$cat_title=$cats_row['cat_title']; 

echo "<li><a href='home.html?cat=$cat_id'>$cat_title</a></li>"; 

} 

?> 

BUT I can't see categories from http://localhost/MyCMS/home.html

It didn't work. What is the problem ? How can I solve this problem. Please help me :(

BRoebie
  • 374
  • 3
  • 13
mrcoder
  • 3
  • 2

2 Answers2

1

Change home.html into home.php as html page with .html extension can't execute PHP code. One more thing, mysql_* in future will deprecated, use mysqli_* or pdo instead. In while loop, must be sure to pass $run_categories instead of $get_cats like following code :

// this variable only store string, not the query itself
$get_cats = "select * from `categories`";
$run_categories = mysql_query($get_cats); 

while ($cats_row=mysql_fetch_array($run_categories)) {...}

Recommended use either one of below version :

1) Mysqli Version

<?php 
// database connection
$con = mysqli_connect("localhost","root","");
mysqli_select_db($con, "mycms");

$get_cats = "select * from `categories`";
$run_categories = mysqli_query($con, $get_cats); 

while ($cats_row=mysqli_fetch_array($run_categories )) { 

 $cat_id=$cats_row['cat_id']; 
 $cat_title=$cats_row['cat_title'];
 echo "<li><a href='home.html?cat=$cat_id'>$cat_title</a></li>";

} 
?>

2) PDO version

<?php 
// database connection
$con = new PDO('mysql:host=localhost;dbname=mycms;charset=utf8', 'root', '');
$get_cats = "select * from `categories`";
$run_categories = $con->query($get_cats); 

while ($cats_row = $run_categories->fetch(PDO::FETCH_ASSOC)) { 

 $cat_id=$cats_row['cat_id']; 
 $cat_title=$cats_row['cat_title'];
 echo "<li><a href='home.html?cat=$cat_id'>$cat_title</a></li>";

} 
?>
Norlihazmey Ghazali
  • 9,000
  • 1
  • 23
  • 40
1

First PHP code is only executed if the extension of the filename is .php so change home.html to home.php.

Second you have a small error in your code as well.

<?php 
include("includes/database.php"); 

$get_cats = "select * from `categories`"; 

$run_categories = mysql_query($get_cats); 

//while ($cats_row=mysql_fetch_array($run_cats)) { 

// The parameter to mysql_fetch_array should be 
// the result of a call to mysql_query()
while ($cats_row=mysql_fetch_array($run_categories )) { 

    $cat_id=$cats_row['cat_id']; 
    $cat_title=$cats_row['cat_title']; 

    echo "<li><a href='home.html?cat=$cat_id'>$cat_title</a></li>"; 
} 
?> 

Also

Please dont use the mysql_ database extensions, it is deprecated (gone for ever in PHP7) Especially if you are just learning PHP, spend your energies learning the PDO or mysqli_ database extensions, and here is some help to decide which to use

Community
  • 1
  • 1
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149