Page 1 of 1

Not able to fetch data from database

Posted: Mon May 12, 2003 4:28 pm
by vipul73
I have created a database "orders" in Mysql and have a table named "spaces" in that.

#
# Table structure for table `spaces`
#

CREATE TABLE spaces (
id tinyint(4) NOT NULL auto_increment,
package varchar(50) default NULL,
amount varchar(10) default NULL,
currency varchar(4) default NULL,
duration varchar(7) default NULL,
PRIMARY KEY (id),
UNIQUE KEY id (id)
) TYPE=MyISAM;

When I am trying to fetch data from the database I am successfully able to do so but when I am trying to pass a value of field 'id' by using

<a href="test.php?id=1>Check</a>

It straight jumps to the else function. The code I am giving is as below:

<?php

$db = mysql_connect("localhost", "root");

mysql_select_db("orders",$db);

// display individual record

if ($space) {

$result = mysql_query("SELECT * FROM spaces WHERE space=$space",$db);

$myrow = mysql_fetch_array($result);

printf("Package Name: %s\n<br>", $myrow["package"]);

printf("Amount: %s\n<br>", $myrow["amount"]);

printf("Currency: %s\n<br>", $myrow["currency"]);

printf("Duration: %s\n<br>", $myrow["duration"]);

} else {

// no records to display

echo "Sorry, no records were found!";

}
?>


Can anyone help as I am a novice to Programming and Mysql both.

Posted: Mon May 12, 2003 4:31 pm
by twigletmac
Where does $space come from? Have you read:
viewtopic.php?t=511

Mac

Not able to fetch data from database

Posted: Tue May 13, 2003 4:26 am
by vipul73
Sorry for being such a blockhead.

But can you help me and let the code I should put

Posted: Tue May 13, 2003 5:06 am
by twigletmac
Where does $space come from - you have id in the query string yet you try and use $space in the code.

You may also find that you probably have to use $_GET['id'] instead of just $id. So instead of:

Code: Select all

if ($space) { 

$result = mysql_query("SELECT * FROM spaces WHERE space=$space",$db); 

$myrow = mysql_fetch_array($result); 

printf("Package Name: %s\n<br>", $myrow["package"]); 

printf("Amount: %s\n<br>", $myrow["amount"]); 

printf("Currency: %s\n<br>", $myrow["currency"]); 

printf("Duration: %s\n<br>", $myrow["duration"]); 

} else { 

// no records to display 

echo "Sorry, no records were found!"; 

}
you may need something like:

Code: Select all

if (!empty($_GET['id']) && is_numeric($_GET['id'])) { 
    $sql = "SELECT package, amount, currency, duration FROM spaces WHERE space=".$_GET['id'];
    $result = mysql_query($sql, $db) or die(mysql_error().'<p>'.$sql.'</p>');

    if (mysql_num_rows($result) == 1) {
        $myrow = mysql_fetch_assoc($result); 
        echo 'Package Name: '.$myrow['package'].'<br />';
        echo 'Amount: '.$myrow['amount'].'<br />'; 
        echo 'Currency: '.$myrow['currency'].'<br />';
        echo 'Duration: '.$myrow['duration'].'<br />'; 
    } else {
        // no records found
        echo 'There is no record with an ID of '.$_GET['id'];
    }
} else { 
    // no records to display 
    echo 'Sorry, no records were found!';
}
For more info have a read of:
http://www.php.net/empty
http://www.php.net/is_numeric
http://www.php.net/mysql_num_rows
http://www.php.net/mysql_fetch_assoc

Mac

Thanks a Lot

Posted: Tue May 13, 2003 4:24 pm
by vipul73
:D Thanx a lot it is working perfectly now :D