Forum Moderators: coopster

Message Too Old, No Replies

Hello all need some advice

I am trying to get started with php and am hitting a wall!

         

paulmerseyside

8:01 pm on Aug 15, 2008 (gmt 0)

10+ Year Member



Hi all

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

StoutFiles

8:08 pm on Aug 15, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<?
$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']."";
}
?>

paulmerseyside

8:30 pm on Aug 15, 2008 (gmt 0)

10+ Year Member



thanks for your reply

have tried that and for some reason nothing seems to be displayed now

tatorface

8:32 pm on Aug 15, 2008 (gmt 0)

10+ Year Member



StoutFiles is right, but sometimes to beginners the ". ." surrounding db values can be intimidating or just plain confusing. To combat this, you can set the db values to variables that are usable without having special quoting needs. It makes the code a little more readable too, though it makes it work a little harder.

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";
}

paulmerseyside

8:37 pm on Aug 15, 2008 (gmt 0)

10+ Year Member



thanks for that - I guess I still need to include the:

$id = $_GET['id'];

in order to obtain the id from the url? - would that sit above that?

tatorface

8:49 pm on Aug 15, 2008 (gmt 0)

10+ Year Member



Yea, try this. Make sure your URL is like:
http://example.com/script.php?id=34
where 34 is the id you are trying to pull...

<?
$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]

paulmerseyside

9:05 pm on Aug 15, 2008 (gmt 0)

10+ Year Member



thanks for your replies - have tried that and it just seems to display a blank page no matter what.

The db has data in there because I have entered a number of rows with completed fields to test out the concept!

tatorface

9:11 pm on Aug 15, 2008 (gmt 0)

10+ Year Member



oops ;)

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]

StoutFiles

9:12 pm on Aug 15, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



select * from database

Is your table named database?

paulmerseyside

9:18 pm on Aug 15, 2008 (gmt 0)

10+ Year Member



thank you so much - that is working a treat now

Really appreciate your help

cheers

paulmerseyside

9:23 pm on Aug 15, 2008 (gmt 0)

10+ Year Member



no just changed the name for the post