Jump to content

Recommended Posts

Hi:

 

I want to place data in mysql DB in two columns. I want to place sandwich in one column, and then the price in another column. It is a restaurant menu. I am using the following code: Thanks for your help

 

<?php

$username = "user";

$password = "password";

$hostname = "localhost";

 

//connection to the database

$dbhandle = mysql_connect($hostname, $username, $password)

or die("Unable to connect to MySQL");

echo "Fresh Sandwich Menu<br><br>";

 

//select a database to work with

$selected = mysql_select_db("menus",$dbhandle)

  or die("Could not select menus");

 

//execute the SQL query and return records

$result = mysql_query("SELECT id, sandwich, price FROM sandwiches");

 

//fetch tha data from the database

while ($row = mysql_fetch_array($result)) {

  echo " ".$row{'sandwich'}." ". //display the results

  $row{'price'}."<br>";

}

//close the connection

mysql_close($dbhandle);

?>

Where is this data coming from? 

 

All you do in your script is display all the sandwiches and id's from the sandwiches table. 

 

You need to use INSERT.  But again, where is this data you would like to 'place in your mysql DB'?

If you want a table you need to use HTML table tags to format it. 

 

Did you get an errors?  Try this:

 

//execute the SQL query and return records
$result = mysql_query("SELECT id, sandwich, price FROM sandwiches") or die(mysql_error());

//fetch tha data from the database
while ($row = mysql_fetch_array($result)) {
   echo $row['sandwich'] . " " . $row['price'] . "
"; //display the results
}

That made the screen blank. It is not printing anything. Any way I can tab between the sandwich and the price?

 

I spend all day googling, reading php books, and I can't figure out how to get the two into a neat column.

 

Thanks again for the help you are suggesting.

Yeah that mean you have an error somewhere.  Add this to the top of your script:

 

ini_set ("display_errors", "1");
error_reporting(E_ALL);

 

I spend all day googling, reading php books, and I can't figure out how to get the two into a neat column.

 

Like I said, you need to use HTML table tags to format neatly.

I tried what you told me and I didn't get any errors, but it still did not do anything. I'm trying to use the HMTL TABLE TAGS, but I'm stuck here:

 

echo "<table width=298 border=0 cellspacing=0 cellpadding=0>";

echo "<tr>";

  echo $row['sandwich'] </td>. "  " . $row['price'] . "<br />"; //display the results

}

 

Not sure where I begin and end my TD because of this ." ".

 

Thanks

Try

 

<?php
$username = "user";
$password = "password";
$hostname = "localhost";

//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL");
echo "Fresh Sandwich Menu<br><br>";

//select a database to work with
$selected = mysql_select_db("menus",$dbhandle) or die("Could not select menus");

//execute the SQL query and return records
$result = mysql_query("SELECT `id`,`sandwich`, `price` FROM `sandwiche`s");

// Print the titles
echo '<table width="28%" border="0" cellspacing="2" cellpadding="0">
  <tr>
    <td width="50%" height="3" align="center"><strong>Sandwich</strong></td>
    <td width="50%" height="3" align="center"><strong>Price</strong></td>
  </tr>
</table>';

echo '<table width="28%" border="0" cellspacing="2" cellpadding="0">
<tr>';
//fetch tha data from the database
while ($row = mysql_fetch_array($result)) {
echo '<td width="50%" height="3" align="center">'.$row['sandwich'].'</td>
      <td width="50%" height="3" align="center">'.$row['price'].'</td>';
}
echo '</tr>';
echo '</table>';
//close the connection
mysql_close($dbhandle);
?> 

There

 

<?php
$username = "user";
$password = "password";
$hostname = "localhost";

//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL");
echo "Fresh Sandwich Menu<br><br>";

//select a database to work with
$selected = mysql_select_db("menus",$dbhandle) or die("Could not select menus");

//execute the SQL query and return records
$result = mysql_query("SELECT `id`,`sandwich`, `price` FROM `sandwiche`s");

echo '<table width="28%" border="0" cellspacing="2" cellpadding="0">
<tr>
<td width="50%" height="3" align="center"><strong>Sandwich</strong></td>
<td width="50%" height="3" align="center"><strong>Price</strong></td>';
//fetch tha data from the database
while ($row = mysql_fetch_array($result)) {
echo '<td width="50%" height="3" align="center">'.$row['sandwich'].'</td>
      <td width="50%" height="3" align="center">'.$row['price'].'</td>';
}
echo '</tr>';
echo '</table>';
//close the connection
mysql_close($dbhandle);
?> 

Nope. check the results now...http://freshfreshseafood.com/test/table.php

 

here is the code so far:

 

//execute the SQL query and return records

$result = mysql_query("SELECT id, sandwich, price FROM sandwiches");

echo mysql_num_rows($result) . '<br>';

if($result){ echo '1'; } else { echo '2'; }

 

// Print the titles

echo '<table width="28%" border="0" cellspacing="2" cellpadding="0">

  <tr>

    <td width="50%" height="3" align="center"><strong>Sandwich</strong></td>

    <td width="50%" height="3" align="center"><strong>Price</strong></td>

  </tr>

</table>';

 

echo '<table width="28%" border="0" cellspacing="2" cellpadding="0">

<tr>';

//fetch tha data from the database

while ($row = mysql_fetch_array($result)) {

echo '<td width="50%" height="3" align="center">'.$row['sandwich'].'</td>

      <td width="50%" height="3" align="center">'.$row['price'].'</td>';

}

echo '</tr>';

echo '</table>';

//close the connection

mysql_close($dbhandle);

?>

 

Okay I am guessing the script spits out results now. How does your data look in the db, it seems you put it all in one column.

 

Lets raise the tables width to 100%

 

change

 

'<table width="28%" border="0" cellspacing="2" cellpadding="0">

 

to

 

'<table width="100%" border="0" cellspacing="2" cellpadding="0">

First, I appreciate your help. Thank you again. Second almost if you look at the link now, shrimp sandwich, and the price is lined up.

 

I took this out because it was displaying an 8 1

 

echo mysql_num_rows($result) . '<br>';

if($result){ echo '1'; } else { echo '2'; }

try

<?php
$result = mysql_query("SELECT id, sandwich, price FROM sandwiches");
//echo mysql_num_rows($result) . '<br>';
//Sif($result){ echo '1'; } else { echo '2'; }

// Print the titles
echo '<table width="28%" border="0" cellspacing="2" cellpadding="0">
  <tr>
    <td width="50%" height="3" align="center"><strong>Sandwich</strong></td>
    <td width="50%" height="3" align="center"><strong>Price</strong></td>
  </tr>',"\n";
//fetch tha data from the database
while ($row = mysql_fetch_array($result)) {
echo '<tr><td width="50%" height="3" align="center">'.$row['sandwich'].'</td>
      <td width="50%" height="3" align="center">'.$row['price'].'</td></tr>',"\n";
}
echo '</table>';
//close the connection
mysql_close($dbhandle);
?> 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.