Page 1 of 2

Data Retrieval and Table Display

Posted: Mon Nov 07, 2011 9:06 am
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

Re: Data Retrieval and Table Display

Posted: Mon Nov 07, 2011 9:09 am
by Celauran
Did you use the .sql file to populate your database?

Re: Data Retrieval and Table Display

Posted: Mon Nov 07, 2011 9:26 am
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

Re: Data Retrieval and Table Display

Posted: Mon Nov 07, 2011 9:31 am
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?

Re: Data Retrieval and Table Display

Posted: Mon Nov 07, 2011 9:33 am
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..

Re: Data Retrieval and Table Display

Posted: Mon Nov 07, 2011 9:37 am
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.

Re: Data Retrieval and Table Display

Posted: Mon Nov 07, 2011 9:42 am
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...

Re: Data Retrieval and Table Display

Posted: Mon Nov 07, 2011 9:53 am
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

Re: Data Retrieval and Table Display

Posted: Mon Nov 07, 2011 10:15 am
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.*?

Re: Data Retrieval and Table Display

Posted: Mon Nov 07, 2011 10:28 am
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? :?

Re: Data Retrieval and Table Display

Posted: Mon Nov 07, 2011 10:34 am
by Celauran
So

Code: Select all

mysql -u root -A school_of_engineering
from the command line works fine?

Re: Data Retrieval and Table Display

Posted: Mon Nov 07, 2011 10:46 am
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? =/

Re: Data Retrieval and Table Display

Posted: Mon Nov 07, 2011 12:45 pm
by DarkSinister
I still don't get it and i cant find out how to check if it works fine from the command line.. =/

Re: Data Retrieval and Table Display

Posted: Mon Nov 07, 2011 1:24 pm
by mikosiko
in Windows... START -- RUN --- cmd ...<OK> black opening screen will give you a command line

Re: Data Retrieval and Table Display

Posted: Mon Nov 07, 2011 9:22 pm
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? =/