Log in

View Full Version : help me to solve this problem



sujith787
07-26-2007, 02:44 PM
i have a table in mysql DB.
table is

name age phone
-----------------------------
sam 24 18191
joseph 30 62781
mary 19 54271
jancy 24 78236
michel 29 78152


this is the table. iam using dream wear 8.
i connected the db with php page,

<?php require_once('Connections/db2.php'); ?>
<?php
mysql_select_db($database_DB2, DB2);
$query_Recordset1 = "SELECT * FROM test";
$Recordset1 = mysql_query($query_Recordset1, $DB2) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?>

and then i have
created the recordset. (name of the recordset is recordset1)

here i want to display all rows in the "name" colum one by one in my output page like

sam
joseph
mary
jancy
michel

iam using the command

<p><?php echo $row_Recordset1['name']; ?></p>

but here i can get only the first record (row) of the "name" colum;

how can i get all other rows in the "name" colum in single page.

please post the code to access all the rows or tell me how to do this please help me.

Twey
07-26-2007, 02:48 PM
while($row_Recordset1 = mysql_fetch_array($query_Recordset1)) {
?>
<p><?php echo $row_Recordset1['name']; ?></p>
<?php
}... although a table may be more suitable.

sujith787
07-26-2007, 03:58 PM
TWEy.... thany you for your reply. but i cant understand wht your telling. where i have to write this code. whts that "although table may be more suitable.

please explain me. iam very very new to this

jonnyynnoj
07-26-2007, 06:17 PM
<?php require_once('Connections/db2.php'); ?>
<?php
mysql_select_db($database_DB2, DB2);
$query_Recordset1 = "SELECT * FROM test";
$Recordset1 = mysql_query($query_Recordset1, $DB2) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);

while($row_Recordset1 = mysql_fetch_array($query_Recordset1)) {
?>
<p><?php echo $row_Recordset1['name']; ?></p>
<?php
}
?>
Above is where you should place the code. Twey said it would be better to use a table because that's what you are creating, his is a short example which uses the <p> tag.

Below is an example of how you can do this in a table:


<table>
<tr><td>Name</td><td>Age</td><td>Phone</td></tr>
<?php require_once('Connections/db2.php'); ?>
<?php
mysql_select_db($database_DB2, DB2);
$query_Recordset1 = "SELECT * FROM test";
$Recordset1 = mysql_query($query_Recordset1, $DB2) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);

while($row_Recordset1 = mysql_fetch_array($query_Recordset1)) {
?>
<tr>
<td><?php echo $row_Recordset1['name']; ?></td>
<td><?php echo $row_Recordset1['age']; ?></td>
<td><?php echo $row_Recordset1['phone']; ?></td>
</tr>
<?php
}
?>
</table>

sujith787
07-26-2007, 06:43 PM
thanks for your replies.....i think i didnt said the problem clearly...
here i tell the problem.. please help me

name age phone
-------------------------------
sam 24 18191
joseph 30 62781
mary 19 54271
jancy 24 78236
michel 29 78152

this is table....................

<p><?php echo $row_Recordset1['name']; ?></p>

this will give output as -----------> sam

now i want the second row "joseph" in the next line i mean output should be

sam
joseph
marry
jancy
michel OK?

simpley said...


i have a table in my DB
using PHP i want to display that table in my dynamic web page.
when we use <?php echo $row_Recordset1['name']; ?> i can only access the first row record. how can i acces the second row record as well as 3rd roww and all. wht is the php code for this.

<?php echo $row_Recordset1['name']; ?> will give first row of "name" colum---> sam
?????????????????????????????????????? will give second row of "name" colum---> joseph

whts this ????????????????????????

Twey
07-26-2007, 06:44 PM
You also need to remove this line:
$row_Recordset1 = mysql_fetch_assoc($Recordset1); from all the code above, since otherwise it will "pop" the topmost row from the stream and it won't appear in the output.
<?php echo $row_Recordset1['name']; ?> will give first row of "name" colum---> sam
?????????????????????????????????????? will give second row of "name" colum---> joseph

whts this ????????????????????????The same statement, after you've used mysql_fetch_array() (which combines the functions of mysql_fetch_row() and mysql_fetch_assoc(), rendering the latter two effectively but not officially obsolete) to pop another row from the recordset.
$row_Recordset1 = mysql_fetch_array($Recordset1);
print $row_Recordset1['name']; // sam
$row_Recordset1 = mysql_fetch_array($Recordset1);
print $row_Recordset1['name']; // joseph
$row_Recordset1 = mysql_fetch_array($Recordset1);
print $row_Recordset1['name']; // mary

jonnyynnoj
07-26-2007, 07:06 PM
Unless I'm not understanding correctly, you want to show all the records, not just the first?
If so the code that me and Twey posted should work fine. They both use the while function which will loop through all the records found in the database.

For example,

<table>
<tr><td>Name</td><td>Age</td><td>Phone</td></tr>
<?php require_once('Connections/db2.php'); ?>
<?php
mysql_select_db($database_DB2, DB2);
$query_Recordset1 = "SELECT * FROM test";
$Recordset1 = mysql_query($query_Recordset1, $DB2) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);

while($row_Recordset1 = mysql_fetch_array($query_Recordset1)) {
?>
<tr>
<td><?php echo $row_Recordset1['name']; ?></td>
<td><?php echo $row_Recordset1['age']; ?></td>
<td><?php echo $row_Recordset1['phone']; ?></td>
</tr>
<?php
}
?>
</table>

Would output:
<table>
<tr>
<td>Name</td>
<td>Age</td>
<td>Phone</td>
</tr>
<tr>
<td>Person 1 name</td>
<td>Person 1 age</td>
<td>Person 1 phone</td>
</tr>
<tr>
<td>Person 2 name</td>
<td>Person 2 age</td>
<td>Person 2 phone</td>
</tr>
etc...
</table>

sujith787
07-26-2007, 07:07 PM
thank you TWEY
your idea works great. i got the result.

but same time, i can access only the first colum rows.
here i want to print all the rows from all the colums

that means i want to display all the table records randomly. that is the important thing here.

sujith787
07-26-2007, 07:14 PM
name age phone
-----------------------------
sam 24 18191
joseph 30 62781
mary 19 54271
jancy 24 78236
michel 29 78152

this is DB table....

here i want to display the phone number 78152 ie 3rd colum (phone), 4th row..

then age 19 2nd colum(age), 3rd row....

like this i want to randomly display in my page.....

please give me the solution.....

how can i do this.

sujith787
07-27-2007, 03:13 AM
anybody know the solution?
please reply me.

sujith787
07-27-2007, 01:22 PM
thank you friends for help me to solve a part of my probs;

i got the solution....................

name age
-------------------
sam 22
john 32
peter 45
george 12 this is the DB table. table name is "TEST"


here is the code.
DB and PHP connection
-----------------------
<?php require('Connections/DB1.php'); ?>
<?php
mysql_select_db($database_DB1, DB1);
$query_Recordset1 = "SELECT * FROM test";
$Recordset1 = mysql_query($query_Recordset1, $DB1) or die(mysql_error());
$tot_Rec = mysql_num_rows($Recordset1);
?>

operation
---------

<?php
for($i=0;$i<$tot_Rec;$i++)
{
$row_Recordset1 = mysql_fetch_array($Recordset1);
$tot_Col = sizeof($row_Recordset1);
for ($j=0;$j<$tot_Col;$j++)
{
if($j=2){
echo $row_Recordset1[$j];
echo "<br>";
}
else { echo "bye <br>";}
}
}
?>


the output is:

sam
john
peter
george




22
32
45
12

but wht i want is not this.....

i want some thing different. here we can get data in order.
but i want to print randomly. for examle if we have M row N coll.
i want to display
(3rd row, 2nd coll) then (1st row, 4th coll)..............like this i want to acess data randamly. in diff palce of my out put page.

once again thank you friends for help me to solve a part of my probs;
please help me to solve this whole problem. thank you.

Twey
07-27-2007, 03:08 PM
<p>
<?php
require('Connections/DB1.php');
mysql_select_db($database_DB1);
$rs = mysql_query('select * from test order by rand()') or die(mysql_error());
$alldata = array();
while($row = mysql_fetch_array($rs))
$alldata = array_merge($alldata, $row);
usort($alldata, create_function('$a,$b', 'return rand() - 0.5;'));
foreach($alldata as $x) {
?>
<?php echo $x; ?>
<?php } ?>
</p>

sujith787
07-27-2007, 04:48 PM
hi TWEY

you are helping lot. thank you your help.

i cant understand the code. please explzin it........

plzzzzzzzzzzzzzzzzzzzzzz

Twey
07-27-2007, 04:51 PM
It gets all the data, puts it in a big array, randomises it, then spits it out.

If you have a lot of data, this will be horribly inefficient, since you'll need to store all the data in memory.

sujith787
07-27-2007, 05:06 PM
i got error

You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '()' at line 1

Twey
07-27-2007, 05:17 PM
Sorry, it's rand(), not random(). Edited.

sujith787
07-27-2007, 05:27 PM
anyway

i had done it in diff way. very very thank you TWYN

<?php require('Connections/DB.php'); ?>
<?php
mysql_select_db($database_DB, $DB);
$query_Recordset1 = "SELECT * FROM test";
$Recordset1 = mysql_query($query_Recordset1, $DB) or die(mysql_error());
$tot_Rec = mysql_num_rows($Recordset1);
?>



<?php
for($i=0;$i<$tot_Rec;$i++)
{
$row_Recordset1 = mysql_fetch_array($Recordset1);
$tot_Col = sizeof($row_Recordset1);
for ($j=0;$j<$tot_Col;$j++)
{
$out[$i][$j]=$row_Recordset1[$j];
}
}
?>
<?php echo $out[0][0]; ?> <br>
<?php echo $out[1][5]; ?> <br>
<?php echo $out[0][3]; ?> <br>
<?php echo $out[1][0]; ?>

</body>
</html>
<?php
mysql_free_result($Recordset1);
?>



this is the full source code, in this code you can access any data anywhere....

thank you all for helped me...................

is it right TWYN?