/    Sign up×
Community /Pin to ProfileBookmark

MySQL Relational Database

Hi,

Using PHP & MySQL, how does one create a relational database?

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@DysanauthorMar 16.2008 — How do I convert the following database into a relational database? The database contains peoples favorite food and drinks.

[CODE]if (mysql_query("CREATE DATABASE db",$con))
{
echo "Database Created!";
}
else
{
echo mysql_error();
}

mysql_select_db("db", $con);
$sql = "CREATE TABLE persons
(
name text,
)";

mysql_query($sql,$con);

$sql = "CREATE TABLE items
(
favouritedrink text,
favouritefood text,

)";

mysql_query($sql,$con);
mysql_close($con);
[/CODE]
Copy linkTweet thisAlerts:
@scragarMar 16.2008 — you need a field joining the two, so either a unique ID or some such:

[code=php]mysql_query(
"CREATE TABLE persons(
ID INT NOT NULL AUTO_INCREMENT,
name VARCHAR(64),
PRIMARY KEY (ID)
)"
);
mysql_query(
"CREATE TABLE items(
person INT,
favouritedrink text,
favouritefood text,
FOREIGN KEY (person) REFERENCES persons(ID) on delete cascade
)"
);[/code]
Copy linkTweet thisAlerts:
@DysanauthorMar 16.2008 — How do I retrieve each user from the database, along with their favorite food and drink?
Copy linkTweet thisAlerts:
@scragarMar 16.2008 — [code=php]$rs = mysql_query("SELECT * FROM persons
LEFT JOIN(items)
ON (persons.ID=items.person)");
while($row = mysql_fetch_assoc($rs)){
echo '<p>';
var_dump($row);
echo '</p>';
};[/code]
×

Success!

Help @Dysan spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...