Forum Moderators: coopster
I am really new to the forum - in fact first post - so apologies for dropping straight in with a question, but I am trying to get into some php and am hitting a brick wall!
Basically I am wanting to extract data from a mysql db and insert data into a page dependent upon a parameter in a url:
so for example
url.com/datapage.php?id=1
the code I have in this page is:
<?
$usr = "username";
$pwd = "pass";
$db = "db";
$host = "localhost";
# connect to database
$cid = mysql_connect($host,$usr,$pwd);
if (!$cid) { echo("ERROR: " . mysql_error() . "\n");}
?>
<?
$id = $_GET['id'];
if (!preg_match('/^[0-9]+$/', $id)) die('die you vile scriptkiddie.');
$handle=mysql_query("select * from database where id='{$_GET['id']}'");
while ($row = mysql_fetch_assoc($handle));
echo <<<HTML
This is some test data<br>
You are looking for {$row['name']} described as {$row['description']} and the price is {$row['price']}
HTML;
?>
Now when I run this all it does is show the wording - i.e. this is some test data, and I have been scratching my head trying work out where things are going wrong!
Would it be possible for some pointers? - this is just a test page at the moment and then I will be building this into a slightly more advanced page!
Thank you in advance for any help / advice you can give
<?
$id = $_GET['id'];if (!preg_match('/^[0-9]+$/', $id)) die('die you vile scriptkiddie.');
$handle=mysql_query("select * from database where id='{$_GET['id']}'");
while ($row = mysql_fetch_assoc($handle));
echo <<<HTML
This is some test data<br>
You are looking for {$row['name']} described as {$row['description']} and the price is {$row['price']}
HTML;?>
Try it this way.
<?php
while ($row = mysql_fetch_assoc($handle))
{
echo "This is some test data<br>";
echo "You are looking for ".$row['name']." described as ".$row['description']." and the price is ".$row['price']."";
}
?>
while ($row = mysql_fetch_assoc($handle))
{
$name = $row{'name'};
$description = $row{'description'};
$price = $row{'price'};
echo "This is some test data<br>";
echo "You are looking for $name described as $description and the price is $price";
}
<?
$usr = "username";
$pwd = "pass";
$db = "db";
$host = "localhost";
# connect to database
$cid = mysql_connect($host,$usr,$pwd);
if (!$cid) { echo("ERROR: " . mysql_error() . "\n");}
$id = $_GET['id'];
if (!preg_match('/^[0-9]+$/', $id)) die('die you vile scriptkiddie.');
$handle=mysql_query("select * from database where id='$id'");
while ($row = mysql_fetch_assoc($handle))
{
$name = $row{'name'};
$description = $row{'description'};
$price = $row{'price'};
echo "This is some test data<br>";
echo "You are looking for $name described as $description and the price is $price";
}
?>
[edited by: dreamcatcher at 6:28 am (utc) on Aug. 16, 2008]
[edit reason] use example.com. Thanks. [/edit]
forgot to select a DB for use.
Make this:
$cid = mysql_connect($host,$usr,$pwd);
if (!$cid) { echo("ERROR: " . mysql_error() . "\n");}
Look like this:
$cid = mysql_connect($host,$usr,$pwd);
if (!$cid) { echo("ERROR: " . mysql_error() . "\n");}
mysql_select_db("$db", $cid); // SELECT DATABASE
[edited by: tatorface at 9:14 pm (utc) on Aug. 15, 2008]