Data Retrieval and Table Display

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

DarkSinister
Forum Newbie
Posts: 12
Joined: Sun Oct 30, 2011 10:09 am

Data Retrieval and Table Display

Post by DarkSinister »

I asked my lecturer when we had class the other day about the code of retrieving data and displaying it in a table form..
The database and everything worked, but only during that class..
After that class I tried to check it again but it didn't work.. is there something wrong I'm doing here?

I put the database file (school_of_engineering.sql) into the same folder as the php (assignment.php) file..

but the result always displays "test 01" and does not display the table.. :(

here is the code in the assignment.php that i have come up with to display the table (inventory) from the database (school_of_engineering).

Code: Select all

<?php
//step 1: connect & select DB

$db = "school_of_engineering";
$table = "inventory";
//$conn = @mysqli_connect("localhost","root","")or die("Unable to connect to $db");

$conn = @mysqli_connect("localhost","root","") or die ("no mysql connection");
@mysqli_select_db($conn,$db) or die ("test 01");


//Step 2: query the table
$sql = "select * from $table";
$query = mysqli_query($conn, $sql);

echo "<table border='1' cellspacing = '3' cellpadding= '3'>";
echo "<table heading='1'";

//step3: retreive and display the data
$row = mysqli_fetch_row($query);
do{
	echo "<tr>";
	echo "<td>$row[0]</td>";
	echo "<td>$row[3]</td>";
	echo "<td>$row[5]</td>";
	echo "</tr>";
$row = mysqli_fetch_row($query);
}while($row);
echo "</table>";

//step 4: free the memory
mysqli_free_result($query);

//step 5: close the connection
mysqli_close($conn);

?>
So where is the wrong in this..
In case you need the file to check out the whole thing yourself, here is the link to get the file too..
http://speedy.sh/M357V/assignment.php
http://speedy.sh/HDxhd/school-of-engineering.sql
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Data Retrieval and Table Display

Post by Celauran »

Did you use the .sql file to populate your database?
DarkSinister
Forum Newbie
Posts: 12
Joined: Sun Oct 30, 2011 10:09 am

Re: Data Retrieval and Table Display

Post by DarkSinister »

Celauran wrote:Did you use the .sql file to populate your database?
the .sql is the file for the database..
I already filled it in with data, you can check it out..
its up there in the link..



oh and yes, i used PHPmyadmin to make the .sql database file
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Data Retrieval and Table Display

Post by Celauran »

The message shows that it's unable to select the school_of_engineering database. Can you connect to it manually? Have you checked for typos?
DarkSinister
Forum Newbie
Posts: 12
Joined: Sun Oct 30, 2011 10:09 am

Re: Data Retrieval and Table Display

Post by DarkSinister »

Celauran wrote:The message shows that it's unable to select the school_of_engineering database. Can you connect to it manually? Have you checked for typos?
In mine here, when I run the assignment.php
it displays only

Test 01

I've checked the typos, it was working when I tried it first time, I didn't change a thing in the codes even then, but its showing me this now.
I double checked with my tutorials that my lecturer gave me, mix and match with my codes, nothing seems to be having typo in the codes. 8O

*oh and i also checked it just now with my notes that the lecturer gave me... same thing..
Last edited by DarkSinister on Mon Nov 07, 2011 9:39 am, edited 1 time in total.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Data Retrieval and Table Display

Post by Celauran »

I wasn't asking about a typo in the code, but in the database name. It's only displaying "test 01" because that's what you've told it to do if it can't select the correct database.

From phpMyAdmin, go to the SQL tab and type the following

Code: Select all

SHOW DATABASES
After clicking "Go", make sure that school_of_engineering is in that list.
DarkSinister
Forum Newbie
Posts: 12
Joined: Sun Oct 30, 2011 10:09 am

Re: Data Retrieval and Table Display

Post by DarkSinister »

Celauran wrote:I wasn't asking about a typo in the code, but in the database name. It's only displaying "test 01" because that's what you've told it to do if it can't select the correct database.

From phpMyAdmin, go to the SQL tab and type the following

Code: Select all

SHOW DATABASES
After clicking "Go", make sure that school_of_engineering is in that list.
Oh I see, I'm still a newbie at this...
Hmm.. Alright hold on let me try...
DarkSinister
Forum Newbie
Posts: 12
Joined: Sun Oct 30, 2011 10:09 am

Re: Data Retrieval and Table Display

Post by DarkSinister »

Celauran wrote:I wasn't asking about a typo in the code, but in the database name. It's only displaying "test 01" because that's what you've told it to do if it can't select the correct database.

From phpMyAdmin, go to the SQL tab and type the following

Code: Select all

SHOW DATABASES
After clicking "Go", make sure that school_of_engineering is in that list.

Alright, after i did the SHOW DATABASES in the SQL tab in phpMyAdmin, I clicked "Go"..
but then the it said
Your SQL query has been executed successfully...
but it doesn't show any database, but I've clearly made the school_of_engineering my database.
I don't get it.. :?

After doing it again, this was written below it..

[Database]
information_schema
cdcol
mysql
performance_schema
phpmyadmin
school_of_engineering
test
webauth
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Data Retrieval and Table Display

Post by Celauran »

Is this on a shared hosting account or a local machine? Have you confirmed that your user has privileges on school_of_engineering.*?
DarkSinister
Forum Newbie
Posts: 12
Joined: Sun Oct 30, 2011 10:09 am

Re: Data Retrieval and Table Display

Post by DarkSinister »

Celauran wrote:Is this on a shared hosting account or a local machine? Have you confirmed that your user has privileges on school_of_engineering.*?
Ummm..its my personal laptop... the server becomes umm.. "localhost","root",""

So that's a local machine right? Well basically the priviledges and stuff is enabled, because I'm running it as admin...
I think if the privileges were not confirmed, it wouldn't even display a thing in the first place right? :?
Last edited by DarkSinister on Mon Nov 07, 2011 10:35 am, edited 1 time in total.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Data Retrieval and Table Display

Post by Celauran »

So

Code: Select all

mysql -u root -A school_of_engineering
from the command line works fine?
DarkSinister
Forum Newbie
Posts: 12
Joined: Sun Oct 30, 2011 10:09 am

Re: Data Retrieval and Table Display

Post by DarkSinister »

Celauran wrote:So

Code: Select all

mysql -u root -A school_of_engineering
from the command line works fine?
Wah? Where do i put that code? In the command line?

Where is the command line ya? =/
DarkSinister
Forum Newbie
Posts: 12
Joined: Sun Oct 30, 2011 10:09 am

Re: Data Retrieval and Table Display

Post by DarkSinister »

I still don't get it and i cant find out how to check if it works fine from the command line.. =/
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: Data Retrieval and Table Display

Post by mikosiko »

in Windows... START -- RUN --- cmd ...<OK> black opening screen will give you a command line
DarkSinister
Forum Newbie
Posts: 12
Joined: Sun Oct 30, 2011 10:09 am

Re: Data Retrieval and Table Display

Post by DarkSinister »

Okay so I wasn't wrong in the first time.. xD

it says here
"mysql" is not recognized as an internal or external command, operable command or batch file. o_O

This seems to be getting quite complicated,
anyone tried out the files for the result themselves? =/
Post Reply