...

View Full Version : mysql table contents



sajuk
02-12-2007, 10:45 PM
Hi,

i am using the following code to view mysql db contents of a table, but when i view the page from a browser it is not showing any contents from the db,, any ideas what is wrong with the code..?

The code is below

List db contents here



<?
//connect to mysql
//change user and password to your mySQL name and password
mysql_connect("localhost","mysql_username","mysql_password");

//select which database you want to edit
mysql_select_db("db_info");

//select the table
$result = mysql_query("select * from address_book");


?>



Thanks
saj

koyama
02-13-2007, 12:27 AM
Your code seems fine so far, but I don't see any code that will actually print any results.

Try this instead which just prints the raw data results: (Notice also the die statements which are good for debugging when things go wrong)


mysql_connect("localhost","mysql_username","mysql_password") or die("couldn't connect");

mysql_select_db("db_info") or die("couldn't select database");

$result = mysql_query("select * from address_book") or die("Bad query");

while($row = mysql_fetch_array($result)) {
var_dump($row);
}

sajuk
02-13-2007, 08:26 AM
Hi,

thank you for that piece of information, now i can see the info from the DB but it is not formatted, i want to format it so it is in a neat table listed below is the results of the first few of lines of what is showing,:--

List db contents here array(26) { [0]=> string(1) "1" ["address_book_id"]=> string(1) "1" [1]=> string(1) "1" ["customers_id"]=> string(1) "1" [2]=> string(1) "m" ["entry_gender"]=> string(1) "m" [3]=> string(9) "ACME Inc." ["entry_company"]=> string(9) "ACME Inc." [4]=> string(4) "John" ["entry_firstname"]=> string(4) "John" [5]=> string(3) "Doe" ["entry_lastname"]=> string(3) "Doe" [6]=> string(12) "1 Way Street" ["entry_street_address"]=> string(12) "1 Way Street" [7]=> string(0) "" ["entry_suburb"]=> string(0) "" [8]=> string(5) "12345" ["entry_postcode"]=> string(5) "12345" [9]=> string(10) "NeverNever" ["entry_city"]=> string(10) "NeverNever" [10]=> string(0) "" ["entry_state"]=> string(0) "" [11]=> string(3) "223" ["entry_country_id"]=> string(3) "223" [12]=> string(2) "12" ["entry_zone_id"]=> string(2) "12" } array(26)

Thanks
saj

neomaximus2k
02-13-2007, 09:55 AM
then you would do something like...



mysql_connect("localhost","mysql_username","mysql_password") or die("couldn't connect");

mysql_select_db("db_info") or die("couldn't select database");

$result = mysql_query("select * from address_book") or die("Bad query");
?>
<table cellpadding="0" cellspacing="2" border="1">
<tr><th>Header 1</th><th>header2</th><th>header 3</th>
<?php
while($row = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td>".$row['address_book_id']."</td>";
echo "<td>".$row['customers_id']."</td>";
echo "<td>".$stripslashes($row['entry_firstname'])."</td>";
echo "</tr>";
}
@mysql_free_result($sql);
?>
</table>



That of course is not the full code you are looking for but explains how you would get the content into a table.

guelphdad
02-13-2007, 01:22 PM
Wouldn't hurt for you to read a tutorial either, this is standard stuff. Search on "mysql and php tutorial" in any search engine.

sajuk
02-14-2007, 12:43 AM
Hi,

i have just tried the code for listing the results into a table, but page only shows the header1 header2 header3 cells the rest comes up blank it , it is not showing the db contents at all,

Thanks
saj



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum