Jump to content

Trying to do my first $_get query to generate dynamic pages


Skor

Recommended Posts

???
Yes, I'm a newbie. Trying to teach myself PHP.  Ran into a roadblock yesterday.

I've been pouring over various books and web docs to to use the _get along with a database query. The best I can get is the column names to appear. I have a very similar query on another page that works just fine, the addition of the $_get is throwing it off. My goal is to create dynamic pages based on the prodID. Looking to see where I veered off.

<?php

$host="localhost";
$user="user";
$password="pw";
$db_name ="products";
$db_table="table";

//check connection to db
mysql_connect($host, $user, $password)
or die ("Unable to connect to server");
mysql_select_db($db_name)
or die ("Couldn't retrieve database information");


$Prodid = $_GET['Prodid'];
print($Prodid); //to make sure you're getting what you wanted

$query = "SELECT Prodid, Name, Category, Price FROM testdb WHERE Prodid = '".$_GET['Prodid']."' AND Status = 'A'";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());


?>
<table border ="2">

<?php


echo '<tr><td>Prodid</td><td>Name</td><td>Category</td><td>Price</td></tr>';

$cols = mysql_num_fields( $result );

while ($row = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td>".$row['Item_no']."</td>";
echo "<td>".$row['Name']."</td>";
echo "<td>".$row['Category']."</td>";
echo "<td>".$row['Price']."</td>";
echo "</tr>";
}

?>
</table>

This results in the following being displayed:

netted in a table with only labels being displayed.

<table border ="2">

<tr><td>Prodid</td><td>Name</td><td>Category</td><td>Price</td></tr></table>

Thanks in advance, I'm baffled.
Link to comment
Share on other sites

Give this  a try:

[code]
<?php
$host="localhost";
$user="user";
$password="pw";
$db_name ="products";
$db_table="table";

//check connection to db
mysql_connect($host, $user, $password)
or die ("Unable to connect to server");
mysql_select_db($db_name)
or die ("Couldn't retrieve database information");

$Prodid = $_GET['Prodid'];
print($Prodid); //to make sure you're getting what you wanted
$query = "SELECT Prodid, Name, Category, Price FROM testdb WHERE Prodid = '$Prodid' AND Status = 'A'";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
?>

<table border ="2">
  <tr>
    <td>Prodid</td>
    <td>Name</td>
    <td>Category</td>
    <td>Price</td>
  </tr>

<?php
$cols = mysql_num_fields( $result );
while ($row = mysql_fetch_array($result)) {
  $Item_no = $row['Item_no'];
  $Name = $row['name'];
  $Category = $row['Category'];
  $Price = $row['Price'];

  echo "<tr>";
  echo "<td>$Item_no</td>";
  echo "<td>$Name</td>";
  echo "<td>$Category</td>";
  echo "<td>$price</td>";
  echo "</tr>";
}
?>

</table>
[/code]
Link to comment
Share on other sites

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.