Not able to fetch data from database

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
vipul73
Forum Commoner
Posts: 27
Joined: Mon May 12, 2003 5:32 am

Not able to fetch data from database

Post 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.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Where does $space come from? Have you read:
viewtopic.php?t=511

Mac
vipul73
Forum Commoner
Posts: 27
Joined: Mon May 12, 2003 5:32 am

Not able to fetch data from database

Post by vipul73 »

Sorry for being such a blockhead.

But can you help me and let the code I should put
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
vipul73
Forum Commoner
Posts: 27
Joined: Mon May 12, 2003 5:32 am

Thanks a Lot

Post by vipul73 »

:D Thanx a lot it is working perfectly now :D
Post Reply