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.
Not able to fetch data from database
Moderator: General Moderators
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
Not able to fetch data from database
Sorry for being such a blockhead.
But can you help me and let the code I should put
But can you help me and let the code I should put
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
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:
you may need something like:
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
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!";
}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!';
}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