Jump to content

php code to display all data from mysql


sandrine

Recommended Posts

hi i am new to this or any forum. i am a complete novice and need some help i am trying to get my php code to display all the data from a mysql but everytime i run the php code on my website i get "the number of record is x number" instead of the data from the mysql can anyone help below is the full php code i am using. to clarify i want all the data from the mysql and not how many record there are in mysql many thanks.

 

<?php

$db="a3205133_test";

$link = mysql_connect('xxxxxxxxxx', 'xxxxxxx', 'xxxxxx');

if (! $link)

die(mysql_error());

mysql_select_db($db , $link)

or die("Couldn't open $db: ".mysql_error());

$result = mysql_query( "SELECT * FROM birthdays" )

or die("SELECT Error: ".mysql_error());

$num_rows = mysql_num_rows($result);

print "There are $num_rows records.<P>";

print "<table width=200 border=1>\n";

while ($get_info = mysql_fetch_row($result)){

print "<tr>\n";

foreach ($get_info as $field)

print "\t<td>$field</td>\n";

print "</tr>\n";

}

print "</table>\n";

mysql_close($link);

?>

Link to comment
Share on other sites

You can post your code in tag [ code] ... [ /code] or [ php] ... [/code] it will be easier to read like that :

 

<?php
$db="a3205133_test";
$link = mysql_connect('xxxxxxxxxx', 'xxxxxxx', 'xxxxxx');
if (! $link)
die(mysql_error());
mysql_select_db($db , $link)
or die("Couldn't open $db: ".mysql_error());
$result = mysql_query( "SELECT * FROM birthdays" )
or die("SELECT Error: ".mysql_error());
$num_rows = mysql_num_rows($result);
print "There are $num_rows records.<P>";
print "<table width=200 border=1>\n";
while ($get_info = mysql_fetch_row($result)){ 
print "<tr>\n";
foreach ($get_info as $field) 
print "\t<td>$field</td>\n";
print "</tr>\n";
}
print "</table>\n";
mysql_close($link);
?>

 

I see nothing wrong with your code, in facts it should work. Did you get a error message or just nothing ? Did your database table is empty ?

 

You can add this at the beggining of your php to display the error :

 

  error_reporting(E_ALL);
  ini_set('display_errors', true);

Link to comment
Share on other sites

thanks for the reply drak no my data base is not empty its just when i click on display record from data base the php code executes and returns the answer "there are x number of records" instead of wats actually in the records like the names and the birthdays. anyway i will recheck the database and insert the error code at the top like you suggested once again many thanks for a quick reponse theonlydrak. ::)

Link to comment
Share on other sites

I didn't understand why. I change the database info to match mine and the table and it work fine on my computer.

 

It display both "there are x number of records" AND a html table that containt the data from the database.

 

Did it show you "there are 0 number of records" or any error when you add the code i told you ? What the exact ouput of your script (view source) maybe there CSS that hide the table.

 

All function seem to be PHP 4 so i don't think there any problem with that.

 

You can try to add a flush(); after each line to see if it's change something, but i don't think unless you have a huge table.

 

<?php
$db="a3205133_test";
$link = mysql_connect('xxxxxxxxxx', 'xxxxxxx', 'xxxxxx');
if (! $link)
die(mysql_error());
mysql_select_db($db , $link)
or die("Couldn't open $db: ".mysql_error());
$result = mysql_query( "SELECT * FROM birthdays" )
or die("SELECT Error: ".mysql_error());
$num_rows = mysql_num_rows($result);
print "There are $num_rows records.<P>";
print "<table width=200 border=1>\n";
while ($get_info = mysql_fetch_row($result)){ 
print "<tr>\n";
foreach ($get_info as $field) 
print "\t<td>$field</td>\n";
print "</tr>\n";
flush();    <---------
}
print "</table>\n";
mysql_close($link);
?>

Link to comment
Share on other sites

Thanks drak i checked the database table and guess What it had the records in it eg. record 1 record 2 etc.....but the data for the name and birthday is missing this despite the fact that when i run the following php code to generate a insert form where i insert a name and birthday ( sorry i know u said to use tags to make it easier to read i don't know what or how to do that)

 

<html><head><title>Birthdays Insert Form</title>

<style type="text/css">

td {font-family: tahoma, arial, verdana; font-size: 10pt }

</style>

</head>

<body>

<table width="300" cellpadding="5" cellspacing="0" border="2">

<tr align="center" valign="top">

<td align="left" colspan="1" rowspan="1" bgcolor="64b1ff">

<h3>Insert Record</h3>

<form method="POST" action="birthdays_insert_record.php">

<?

print "Enter name: <input type=text name=name size=20><br>\n";

print "Enter Birthday: <input type=text name=birthday size=10><br>\n";

print "<br>\n";

print "<input type=submit value=Submit><input type=reset>\n";

?>

</form>

</td></tr></table>

</body>

</html>[/color]

 

 

.....i click on submit on the form generated from the php code above and then i am presented with a message "record added" but when i check the mysql database only an additional record is added but not the actual information i.e name and the birthdate. below is the php code which is executed after the one above. its the php code that is supposed to insert the information into the data base

 

<html><head><title>Birthdays Insert Record</title></head>

<body>

<?

/* Change next two lines */

$db="";

$link = mysql_connect('', '', '');

if (! $link)

die(mysql_error());

mysql_select_db($db , $link) or die("Select Error: ".mysql_error());

$result=mysql_query("INSERT INTO birthdays (name, birthday) VALUES ('$name','$birthday')")or die("Insert Error: ".mysql_error());

mysql_close($link);

print "Record added\n";

?>

<form method="POST" action="birthdays_insert_form.php">

<input type="submit" value="Insert Another Record">

</form>

<br>

 

<form method="POST" action="birthdays_dbase_interface.php">

<input type="submit" value="Dbase Interface">

</form>

 

 

 

</body>

</html>

 

but u were right when u said the code for displaying the information from mysql works fine but for somereason the php code above for adding the records is not adding the information to the mysql database.

i am sorry if it all sounds a bit silly but maybe you can help i don't know.....anyways thanks  :)

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.