Msaada kuhusu SQL & PHP

hero7

JF-Expert Member
Jun 15, 2014
222
70
Habari!

Naomba msaada katika SQL jinsi ya kufanya relation na kuconnect na php natumia wamp server.

Asante!
 
no nahitaji kuconnect na databse ila ningepanda jua pia nq jinsi ya kuconnect na server
tengeneza file la dbconnection.php na pest hizi kodi

<?php
define('db_user','username');
define('db_host','localhost');
define('db_pass','password');
define("db","database name");
$con = new mysqli(db_host,db_user,db_pass,db);

if($con->connect_errno){
echo 'Connection Failed '.$con->connect_error;
}

?>

hizi kodi zina connect moja kwa moja na server pamoja na database.
hizo define
ni constant variables kwaiyo una weza kutumia variables kama variable

mfano

$db_user = "username";
$db_pass = "password";
$db_host = "localhost"; server
$db_db = "database name";

$con = new mysqli($db_host,$db_user,$db_pass,$db_db);
if($con->connect_errno){
echo "Connection faild".$con->error;
}

Nazani itasaidia
 
ili kuweza kuselect tables data unafanya hivi

una file la include("dbconnection.php");

then

unatumia $con-> variable

$sql = $con->query("select * from tablename where id = 1");
if($sql->num_rows > 0){
echo "Yes there is an iterm";
}else{
echo "nothing found";
}
 
ili kuweza kuselect tables data unafanya hivi

una file la include("dbconnection.php");

then

unatumia $con-> variable

$sql = $con->query("select * from tablename where id = 1");
if($sql->num_rows > 0){
echo "Yes there is an iterm";
}else{
echo "nothing found";
}
asante
 
ili kuweza kuselect tables data unafanya hivi

una file la include("dbconnection.php");

then

unatumia $con-> variable

$sql = $con->query("select * from tablename where id = 1");
if($sql->num_rows > 0){
echo "Yes there is an iterm";
}else{
echo "nothing found";
}
then naomba unisaidie kunifanunulia maana relation kataka databse mfn na databse tuite forum ambayo inatable 3 table 1,table2, na table3,
 
then naomba unisaidie kunifanunulia maana relation kataka databse mfn na databse tuite forum ambayo inatable 3 table 1,table2, na table3,
unaitaji nikufafanulie kiaje jinsi ya kuaccess datas zilizo kuwemo humo au
 
hyo mifano nimetoa kwa ajili ya kuweza kunielezea vzuri relation kupitia hzo table
ngoja nijaribu kuendana na swali lako( unaitaji kuaccess data zilizo kuwepo katika table tatu ambazo zina info sawa kwamfano table 1 ni users table , table 2 ni posts table na table 3 ni categories table so unawezaje kupata info kwa user mmoja ambae ka post katika category flani kwa kutumia single query ?? nazani ndo swali lako la relation hili au nime kosea ??
 
ngoja nijaribu kuendana na swali lako( unaitaji kuaccess data zilizo kuwepo katika table tatu ambazo zina info sawa kwamfano table 1 ni users table , table 2 ni posts table na table 3 ni categories table so unawezaje kupata info kwa user mmoja ambae ka post katika category flani kwa kutumia single query ?? nazani ndo swali lako la relation hili au nime kosea ??
umepatia sema umeenda mbele sana,ila fanya hili ni swali jipya ili kunielewa nahitaji maana ya kufanya relation katika databsase
 
aaah kumbe unataka maana kwa definition relation database ni set ya tables ambazo zina taarifa zilizo fungwa katika categories. kila tables ina taarifa moja au zaidi ya categories kwenye columns.kila row ina taarifa ya pekiyake kwaajiri ya categories ambayo ilioelezwa na columns.

hiyo ni definition kama ntakuwa nimekosea au sijakizi uwelewa wako basi labda uingie deep kwasababu wote tuna uwelewa tofauti kutokana na maswali yanayo uliza. ukiniuliza jinsi ya kufanya relation labda ninge kujibu kwa kodi zaid.
 
aaah kumbe unataka maana kwa definition relation database ni set ya tables ambazo zina taarifa zilizo fungwa katika categories. kila tables ina taarifa moja au zaidi ya categories kwenye columns.kila row ina taarifa ya pekiyake kwaajiri ya categories imbayo ilioelezwa na columns.

hiyo ni definition kama ntakuwa nimekosea au sijakizi uwelewa wako basi labda uingie deep kwasababu wote tuna uwelewa tofauti kutokana na maswali yanayo uliza. ukiniuliza jinsi ya kufanya relation labda ninge kujibu kwa kodi zaid.
ok naomba kwa code by sql kufanya reltion naomba tutumie kupitia uo mfano wa hzo table.
 
ok naomba kwa code by sql kufanya reltion naomba tutumie kupitia uo mfano wa hzo table.
Sawa tunatumai realistic tables users,posts,na comments

chakwanza tabidi post table iwe na instance ya users na comments iwe na instance ya posts

kwaiyo tabidi utengeneze hizo table kupitia php na uzipe instance zake kwa kutumia user id.

mfano

users table

$con->query("create table if not exists users(
id int(11) not null auto_increment,
username varchar (50) not null,
primary key(id),
unique key(username)
)");

posts table

$con->query("create table if not exists posts(
id int(11) not null auto_increment,
user_id int(11) not null,
post text not null,
primary key(id),
foreign key (user_id) references users (id)
)");

comments table

$con->query("create table if not exists comments(
id int(11) not null auto_increment,
post_id int(11) not null,
comments text not null,
primary key(id),
foreign key (post_id) references posts(id)
)");

umeona hapo kuwa posts table ina husiano wa id na users table na comments table ina huusiano na posts id , kwakutumia foreign key ( ) references () hivi ndo unavo weza kuweka huusiano wa tables.
 
Sawa tunatumai realistic tables users,posts,na comments

chakwanza tabidi post table iwe na instance ya users na comments iwe na instance ya posts

kwaiyo tabidi utengeneze hizo table kupitia php na uzipe instance zake kwa kutumia user id.

mfano

users table

$con->query("create table if not exists users(
id int(11) not null auto_increment,
username varchar (50) not null,
primary key(id),
unique key(username)
)");

posts table

$con->query("create table if not exists posts(
id int(11) not null auto_increment,
user_id int(11) not null,
post text not null,
primary key(id),
foreign key (user_id) references users (id)
)");

comments table

$con->query("create table if not exists comments(
id int(11) not null auto_increment,
post_id int(11) not null,
comments text not null,
primary key(id),
foreign key (post_id) references posts(id)
)");

umeona hapo kuwa posts table ina husiano wa id na users table na comments table ina huusiano na posts id , kwakutumia foreign key ( ) references () hivi ndo unavo weza kuweka huusiano wa tables.
Nimeku pm bosi
 

Similar Discussions

Back
Top Bottom