Jump to content

php and mysql


juice

Recommended Posts

im creating a simple website and database for a project with minimal functionality. All i'm supposed to do is create a website that communicates with a database. i chose to do that project with php, ajax and mysql in xamp. Now i've decided to work on the database first, i want it to be portable where i can just put the completed code in folder on a flash drive and go to school with it. where i'll jus be able to copy it to the htdocs folder in the xamp and it will automatically create the database and run all the queries. For some reason, no matter what i try it won't show up in the phpmyadmin databases section. I desperately need help, i wanna kno if my idea is possible and how can i go about it.. here's my code.. thanks much to any help, will be greatly appreciated..

 

the config.php

<?php$db_name="dscdb";$db_user="root";$db_password="";$db_server="localhost";?>

heres my connection mysqlconnection.php

<?phpinclude($_SERVER['DOCUMENT_ROOT'].'/FinalProject/config.php');$con=mysql_connect($db_server,$db_user,$db_password) or die("Connection Failure: ".mysql_error());mysql_select_db($db_name,$con);	?>

and heres my database and query creation.. setup.php

<?phpinclude($_SERVER['DOCUMENT_ROOT'].'/Finalproj/config.php');{$con = mysql_connect($db_server,$db_user,$db_password); // database connect//Database creation	mysql_query("CREATE DATABASE  IF NOT EXISTS $db_name" $con) or die("creating database fails".mysql_error());	USE $db_name;	mysql_select_db($db_name) or die(mysql_error());			//Students Table creation	mysql_query("CREATE Table IF NOT EXISTS 'Students'('StudID' varchar NOT NULL (15) , primary key (StudentID), 	'FirstName' varchar(15) NOT NULL, 'LastName' varchar (15) NOT NULL, 'BirthDate' DATE NOT NULL, 'Address' varchar(30) NOT NULL, 'MajorTitle' 	 Foreign key (MajorTitle) References Majors(MajorTitle) NOT NULL, 'Username' varchar(15) NOT NULL, 'Password' varchar(8) NOT NULL)") 	or die("Failed to create Table".mysql_error());		mysql_query($con,"INSERT into Students(StudID, FirstName, LastName, BirthDate, Address, MajorTitle, Username, Password) 				values ('123', 'lee', 'daniel', '1990-01-08', 'Bioche', 'Computer Science', 'lee', 'daniel')");					mysql_query($con,"INSERT into Students(StudID, FirstName, LastName, BirthDate, Address, MajorTitle, Username, Password) 				values ('456', 'danphil', 'daniel', '1992-06-25', 'Bioche', 'Electronics', 'danphil', 'daniel')");					mysql_query($con,"INSERT into Students(StudID, FirstName, LastName, BirthDate, Address, MajorTitle, Username, Password) 				values ('789', 'eber', 'raver', '1988-03-28', 'Colihaut', 'Physics', 'eber', 'raver')")		//$hashedpassword=sha1("password");// Instructor Table creation		mysql_query("CREATE Table IF NOT EXISTS 'Instructors'('InstrucID' varchar NOT NULL (15), primary key (InstrucID), 	'FirstName' varchar (15) NOT NULL, 'LastName' varchar (15) NOT NULL, 'Address' varchar(30) NOT NULL, 'Username' varchar (15) NOT NULL,	'Password' varchar(8) NOT NULL) ") or die("Failed to create Table".mysql_error());		mysql_query($con,"INSERT into Instructors(InstrucID, FirstName, LastName, Address, Username, Password) 				values ('abc', 'larry', 'thomas', '1982-09-14', 'Goodwill', 'larry', 'thomas')");					mysql_query($con,"INSERT into Instructors(InstrucID, FirstName, LastName, Address, Username, Password) 				values ('def', 'james', 'victor', '1986-09-06', 'Grandbay', 'james', 'victor')");					mysql_query($con,"INSERT into Instructors(InstrucID, FirstName, LastName, Address, Username, Password) 				values ('ghi', 'barnaby', 'jack', '1977-11-23', 'ireland', 'barnaby', 'jack')");//Registrar Table creation	mysql_query("CREATE Table IF NOT EXISTS 'Registrars' ('RegID' varchar NOT NULL (15), primary key(RegID),	LastName varchar (15) NOT NULL, Username varchar (15) NOT NULL, Password varchar(15) NOT NULL) ") 	or die("Failed to create Table".mysql_error());		mysql_query($con,"INSERT into Registrars(RegID, LastName, Username, Password) 				values ('abc123', 'jones', 'jones', 'jones')");	//Majors table creation	mysql_query("CREATE Table IF NOT EXISTS 'Majors' ('MajorType' varchar NOT NULL (15), primary key (MajorID), 'MajorTitle' varchar (20) NOT NULL)") 	or die("Failed to create Table".mysql_error());  	mysql_query($con,"INSERT into Majors('MajorType', 'MajorTitle') 				values ('FAS', 'Computer Science')");	mysql_query($con,"INSERT into Majors('MajorType', 'MajorTitle') 				values ('FAS', 'Physics')");					mysql_query($con,"INSERT into Majors('MajorType', 'MajorTitle') 				values ('FAAT', 'Electronics')");				echo "<script>alert('The webApp has been installed successfully')</script> ";		mysql_close();}}?>
Link to comment
Share on other sites

i'm starting to think now its a problem the phpmyadmin... i've tried 3 different ways now.. mysql, mysqli and now PDO.. can any one suggest something i could try or tweak..

 

here's the PDO code i hav now

<?php$db_name="dscdb";$db_user="root";$db_password="";$db_server="localhost";$user='admin';$password="";		$dbconn = new PDO(mysql:host = $db_server; $db_name; charset = utf8, $db_user, $db_password);					// create a new database		 $dbconn->exec("CREATE DATABASE '$db_name`;                CREATE USER '$user'@'localhost' IDENTIFIED BY '$passw';                GRANT ALL ON `$db_name`.* TO '$user'@'localhost';                FLUSH PRIVILEGES;") 				or die(print_r($dbconn->errorInfo(), true));		// check to see if database was successfully created				if($db_name){				echo 'connected to database';			}		else			{			echo 'Not Successfuly Created';			}						$dbconn = null;	?>
Link to comment
Share on other sites

What are you using to develop; WAMP, XXAMP, MAMP, LAMP? Or on a live server?

 

Maybe you need to update your PHP to a version that supports mysqli, PDO. If you're using a version that has mysqli/PDO support, then something wrong with configuration somewhere most likely.

Link to comment
Share on other sites

yh.. im using xamp.. i even reinstalled it last night.. installed the latest phpmyadmin to.. every thing is up to date.. so far i tried; mysql, mysqli, PDO and now perl and nothing is working.. im really beigining to think its a configuration problem in the phpmyadmin, mysql server or xampp.. can anyone tell me what should i try.. im using the latest php 5.5.9, phpmyadmin 4.1.13, MySQL 5.6.16 and apache 2.4.7... this is really keeping me back now n giving headaches..

Edited by juice
Link to comment
Share on other sites

PhpMyAdmin allows you to administer the database. You're running Php code. That has nothing to do with PhpmyAdmin other than using the database and tables that you probably created using PhpMyAdmin. You might also enter a few test values into the tables using PhpMyAdmin. Your first Php debugging test would probably be to prove that your code can read those tables and obtain those known values.

Link to comment
Share on other sites

well i added the database to the phpmyadmin, manually and then tried to connect to it wih php.. i used mysqli.. it connected :).. now i'm having trouble adding the tables with php code but i'll get around it sooner or later... thanks for everyones help... if i get any other problem i will let you'll know...

Link to comment
Share on other sites

please Dont be confused. you can go either way with pdo or mysqli. if code with one of them is not working dont switch it to another. it wont help anything. as you are starting stick with any one of them.

 

as already told, phpmyadmin is nothing but a php application which interacts with mysql server. so if you can work with phpmyadmin, it is obvious that you have mysql,apache and php is up and running. after that if something in your code is not working dont assume and try to debug things.

$dbconn = new PDO(mysql:host = $db_server; $db_name; charset = utf8, $db_user, $db_password);    $dbconn->setAttribute(PDO::ATTR_ERRORMODE,PDO::ERRORMODE_EXCEPTION); //It will throw exception when an error occurs. it will also show the reason of error with error message

unless it is for educational purpose table creation (or any other DDL part) usually does not happen in php scripts. you set up the table in phpmyadmin. and mostly use Database Query Language(DQL) or DML from your php script.

Edited by birbal
  • Like 1
Link to comment
Share on other sites

unless it is for educational purpose table creation (or any other DML part) usually does not happen in php scripts. you set up the table in phpmyadmin.

That's right, I never write code to create tables in my actual PHP scripts. I use phpMyAdmin to create the database and all of the tables first, and then my PHP code just uses the existing database to add records, delete them, update them, read them, etc.
  • Like 1
Link to comment
Share on other sites

thanks for all the advice but as i said in the beginning its for a school project and i want the code to be portable.. so i can transfer it to my schools' computer and it will automaticall create the database tables, which will make my website fully functional... i found the problem in my code after trial n error.. it was the quotation marks i placed in my SQl queries... i'm tryna finish it right now with msqli cause there's alot more documentation and examples online for it than PDO.. but when i'm done i will try doing it in PDO as well, it seems easier and faster to code because or its object oriented nature...

Link to comment
Share on other sites

i want the code to be portable

 

You can export and import database and tables. phpmyadmin has option for doing so.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...