Skip to content

One to many relationship of database in PHP MySQLi

To establish a One to many relationships of databases in PHP MySQLi, need to perform the following steps;

  1. Set up your database and tables
  2. Established the relationship between tables. We can do it by setting a primary key in one table and a foreign key in another table that references the primary key.
1 to many relationship with sql query in php myadmin
1 to many relationship with sql query in php myadmin
  1. I am sharing with you an example of how you can create tables and SQL Query and you might set up a one-to-many relationship in a MySQL database;

 

CREATE TABLE customers (

customer_id INT NOT NULL AUTO_INCREMENT,

name VARCHAR(255) NOT NULL,

PRIMARY KEY (customer_id)

);

 

CREATE TABLE orders (

order_id INT NOT NULL AUTO_INCREMENT,

customer_id INT NOT NULL,

order_total DECIMAL(10,2) NOT NULL,

PRIMARY KEY (order_id),

FOREIGN KEY (customer_id) REFERENCES customers(customer_id)

);

 

  1. Now, in our PHP code, we will use mysqli functions to connect to the database and query the appropriate tables.
  2. Use a JOIN statement in your SELECT query to combine the data from the two tables based on the foreign key.
  3. Use a loop (e.g. while or for) to iterate through the results and display the data as desired.
  4. Here is an example of how you might show a one-to-many relationship in PHP using mysqli with SQL code:

 

<?php

/* making the database connection */

$mysqli = new mysqli(“localhost”, “root”, “”, “test1”);

 

/* checking the database connection */

if ($mysqli->connect_errno) {

printf(“Connect failed: %s\n”, $mysqli->connect_error);

exit();

}

 

$query = “SELECT orders.order_id, customers.name, orders.order_total FROM orders JOIN customers ON orders.customer_id=customers.customer_id”;

$result = $mysqli->query($query);

 

/* associative array */

while ($row = $result->fetch_assoc()) {

echo $row[“order_id”] . ” – ” . $row[“name”] . ” – ” . $row[“order_total”] . “<br>”;

}

 

/* free result set */

$result->free();

 

/* close connection */

$mysqli->close();

 

?>

one to many relationship in php mysqli
one to many relationship in php mysqli

 

Download One to many relationship of database in PHP MySQLi

哆哆女性网网站优化设计郭姓 起名南昌网站专业制作手机网站建设wap进出口外贸起名窗帘店起什么名字发财绵阳专业网站建设公司洛基电影天堂苏州制作外贸网站广告设计作品东莞松山湖网站建设公司起名参照什么古书姚字起女孩名字深圳网站建设哪个公司好起姓名大全免费起名北京网站的建设女孩起蒙古名如何网站关键词优化长垣起重机十大企业排名宝宝起名八字取名基础seo学习周易博士生专业技术人员年度考核个人总结东莞seo教程企业网站制作与推广周公解梦梦见豪宅上海网站seo公司用翊字取名起名大全反派总在开导我2019香港最新电视剧淀粉肠小王子日销售额涨超10倍罗斯否认插足凯特王妃婚姻不负春光新的一天从800个哈欠开始有个姐真把千机伞做出来了国产伟哥去年销售近13亿充个话费竟沦为间接洗钱工具重庆警方辟谣“男子杀人焚尸”男子给前妻转账 现任妻子起诉要回春分繁花正当时呼北高速交通事故已致14人死亡杨洋拄拐现身医院月嫂回应掌掴婴儿是在赶虫子男孩疑遭霸凌 家长讨说法被踢出群因自嘲式简历走红的教授更新简介网友建议重庆地铁不准乘客携带菜筐清明节放假3天调休1天郑州一火锅店爆改成麻辣烫店19岁小伙救下5人后溺亡 多方发声两大学生合买彩票中奖一人不认账张家界的山上“长”满了韩国人?单亲妈妈陷入热恋 14岁儿子报警#春分立蛋大挑战#青海通报栏杆断裂小学生跌落住进ICU代拍被何赛飞拿着魔杖追着打315晚会后胖东来又人满为患了当地回应沈阳致3死车祸车主疑毒驾武汉大学樱花即将进入盛花期张立群任西安交通大学校长为江西彩礼“减负”的“试婚人”网友洛杉矶偶遇贾玲倪萍分享减重40斤方法男孩8年未见母亲被告知被遗忘小米汽车超级工厂正式揭幕周杰伦一审败诉网易特朗普谈“凯特王妃P图照”考生莫言也上北大硕士复试名单了妈妈回应孩子在校撞护栏坠楼恒大被罚41.75亿到底怎么缴男子持台球杆殴打2名女店员被抓校方回应护栏损坏小学生课间坠楼外国人感慨凌晨的中国很安全火箭最近9战8胜1负王树国3次鞠躬告别西交大师生房客欠租失踪 房东直发愁萧美琴窜访捷克 外交部回应山西省委原副书记商黎光被逮捕阿根廷将发行1万与2万面值的纸币英国王室又一合照被质疑P图男子被猫抓伤后确诊“猫抓病”

哆哆女性网 XML地图 TXT地图 虚拟主机 SEO 网站制作 网站优化