0

I have a database from which I would like to display two columns in a table.

I have the following code however, this does not work and I get

"; } mysqli_close($con); as my answer.[solved after removing echo "<br />"]

Code follows:

<html>
<style>
div.transbox
  {
  width:850px;
  height:500px;
  margin:30px 50px;
  background-color:#ffffff;
  border:1px solid black;
  opacity:0.9;
  padding: 20px;
  filter:alpha(opacity=60); /* For IE8 and earlier */
  }



  ul#navigation
  {
  link-style: none;
   padding: 5px 20px 5px 20px
  margin: 0;
  }
  ul#navigation li
  {
  display: inline;
  }
  h1#heading
  {
    color:#ffffff;
        background-color:#000000;
        text-decoration:none;
font-family:georgia, times, serif;
font-size:2em;
padding:10px;
border-bottom:3px solid #ff6600;
  }
  ul#navigation a {
        color:#ffffff;
        background-color:#000000;
        text-decoration:none;
font-family:georgia, times, serif;
font-size:1.126em;
padding:10px;
border-bottom:3px solid #ff6600;}

  div.line
  {
  display: inline;
  }

 ul#navigation a:hover {
        color:#000000;
        background-color:#ff6600;
        padding:10px;
        border-bottom:3px solid #000000;}
  </style>

<head><h1 id="heading">Food Review </h1>
<ul id="navigation">
<li><a href="form4.html">Review</a></li>
<li><a href="Ratings.html">Ratings</a><li>
</ul>
</head>

<body background = img.jpg>
<div class="transbox">
<div class="work">
<?php
define('DB_NAME','form');
define('DB_USER','root');
define('DB_PASSWORD','toor');
define('DB_HOST','localhost');
$link = mysql_connect(DB_HOST,DB_USER,DB_PASSWORD);
if(!$link)
{
    die('could not connect : ' . mysql_error());
}
$db_selected = mysql_select_db(DB_NAME,$link);
if(!$db_selected)
{
    die('Can\'t use ' .DB_NAME . ': ' .mysql_error());
}
$result = mysqli_query($link,"SELECT fname,Ratings from demo1");

while($row = mysqli_fetch_array($result))
{
echo $row['fname']." ".$row['Ratings'];
}
mysqli_close($link);
?>
</div>
</div>
</body>
</html>
}

I did the following changes but still no results are displayed.

5
  • The html you have within the <head> tag is invalid. That code belongs in the body. Commented Oct 8, 2013 at 22:54
  • Wow that is a seriously malformed HTML document. Why do you have actual display content within the <head> element? Commented Oct 8, 2013 at 22:57
  • Also mysqli_close($con); seems faulty, i don't see $con being defined anywhere, shouldn't you use $link ? Commented Oct 8, 2013 at 22:59
  • I would recommend you to use a framework to better organize your work and your learning guide them. I use CodeIgniter for example. Commented Oct 8, 2013 at 23:04
  • not sure this help you but you selected database via mysql_select but other database commands are mysqli Commented Oct 8, 2013 at 23:15

4 Answers 4

2
$link = mysql_connect(DB_HOST,DB_USER,DB_PASSWORD);

Since you initialized the connection with $link you should close $link

 mysql_close($link);

and change the following line

echo "<br />"; 

with

echo "<br>"; 

and you are mixing mysql with mysqli please check and use only one which suits you

Sign up to request clarification or add additional context in comments.

6 Comments

Ok i did this and still i get no result
try to remove the line echo "<br />"; and check
i removed it now nothing is displayed
$num_rows = mysqli_num_rows($result); echo "$num_rows Rows\n"; //try to check if records exists
i do not get a reply but i stored the values in the table
|
1

You are mixing mysql functions and mysqli. Have a look here: http://php.net/manual/en/mysqli.query.php how to use mysqli properly

<?php

define('DB_NAME','form');
define('DB_USER','root');
define('DB_PASSWORD','toor');
define('DB_HOST','localhost');

$mysqli = new mysqli(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);

/* check connection */
if ($mysqli->connect_errno) {
    printf("Connect failed: %s\n", $mysqli->connect_error);
    exit();
}

$result = $mysqli->query("SELECT fname,Ratings from demo1");

while($row = $result->fetch_array())
{
$rows[] = $row;
echo $row['fname']." ".$row['Ratings']."<br />";
}

$mysqli->close();
?>

There is also a procedural style but I highly recommend the object oriented ;)

EDIT: Also a good idea is to enable error reporting for development. Add this to the beginning of your php file:

<?php
ini_set('display_errors',1); 
error_reporting(E_ALL);
?>

Comments

1

I'm pretty sure some data from the demo1 table contains characters that mess with html and breaks the tag nesting.

Try replacing this:

echo $row['fname']." ".$row['Ratings'];

with

echo htmlentities($row['fname']." ".$row['Ratings']);

EDIT: Also, replace mysql_connect with mysqli_connect and mysql_select_db with mysqli_select_db

2 Comments

i still didnt get anything
It did work but thanks a lot for the effort sir i really appreciate it
0

Your PHP install is broken and not functioning, so your PHP code is being sent to the browser, which interprets the <?php tag as a broken/unknown HTML tag, and the entire contents of the page are ignored, until you reach

echo "<br />";

where the /> serves as the closing bracket for this "malformed" <?php tag.

That's why you only see the mysqli_close business in your browser, because everything else is hidden as a broken tag.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.