Selecting a particular record

Codes here !

Moderators: macek, egami, gesf

Beady
New php-forum User
New php-forum User
Posts: 6
Joined: Sat Jan 04, 2003 3:30 am
Location: UK
Contact:

Selecting a particular record

Postby Beady » Sat Jan 04, 2003 3:37 am

Hi,

I have a MySQL database with two tables. I need to be able to show a list of records from one table and one field from the other table like this
Table1Title1 Table1SubHeading1 Table2PageName Table1Desc1
Table1Title2 Table1SubHeading2 Table2OtherPageName Table1Desc2

Can someone point me in the right direction please

Regards

Bernard Davis

User avatar
Joan Garnet
Moderator
Moderator
Posts: 387
Joined: Sat Aug 03, 2002 2:56 am
Location: Mars
Contact:

Postby Joan Garnet » Sat Jan 04, 2003 6:23 am

Well,
there are other ways...
but I think this is faster -->


Code: Select all

<?php
$server = "localhost" ;
$login = "your_login" ;
$pass = "your_pass";
$table1 = "table1_name";
$table2 = "table2_name";
$data_base = "my_database_name";
$conection = mysql_connect($server,$login,$pass);     
mysql_select_db($data_base,$conection);
////////////////////////////////////////////////////////
$sql_one = "select * from ".$table1." order by id desc";
$sql_two = "select * from ".$table2." order by id desc";
////////////////////////////////////////////////////////
$result_one=mysql_query($sql_one , $conection);
$result_two=mysql_query($sql_two , $conection);
////////////////////////////////////////////////////////
$i=0;
while ($row = mysql_fetch_array ($result_two)) {
   for ($j=0; $j<mysql_num_rows($result_two); $j++){
      $my_array[$j][$i] = $row[$j];
   }
   $i++;
}
print_r ($my_array);//you'll see the shape of this array, just comment this two lines when you see it clear
echo"<p>";
////////////////////////////////////////////////////////////////
/// This is the output. You can define it as you want ////
///////////////////////////////////////////////////////////////
while ($row = mysql_fetch_array ($result_one)) {
   echo $row[0]." - ".$row[1]." - ".$my_array[0][1]." - ".$row[2]." - ".$row[3]." - "."<br>";
}
?>


I hope It helped
bye!

Beady
New php-forum User
New php-forum User
Posts: 6
Joined: Sat Jan 04, 2003 3:30 am
Location: UK
Contact:

Almost but not quite

Postby Beady » Sun Jan 05, 2003 3:08 am

Hi,

Many thanks for your response, it almost does the trick. I can see how you are using an array to build the list of entries, but I still can't get it to work.

Perhaps if I ask a slightly different question -

How do I simply build an array which contains one field (say 'pageName' or field number 1) from every record in a table?

I know how to access the array once it is built, but I can't see how it is built in the first place.

Regards

Bernard Davis

User avatar
Joan Garnet
Moderator
Moderator
Posts: 387
Joined: Sat Aug 03, 2002 2:56 am
Location: Mars
Contact:

Postby Joan Garnet » Sun Jan 05, 2003 10:05 am

Maybe...

Code: Select all

while ($row = mysql_fetch_array ($result)) {
   echo $row["pageName"];
}


:?:

stempy
New php-forum User
New php-forum User
Posts: 4
Joined: Fri Feb 14, 2003 10:41 am

i think along the same Q

Postby stempy » Fri Feb 14, 2003 10:53 am

ok i have a field in the table that has multiple words in that field.

i want to be able to pull that field and then make an array of those words.

example

keywords ( job,setup, office, supplies)

that is in the field of keywords in the table.

i want to pull that out of the table that is in one row of data in the table and put that in an array so that i can do a string compare


help


:D :idea:

User avatar
Joan Garnet
Moderator
Moderator
Posts: 387
Joined: Sat Aug 03, 2002 2:56 am
Location: Mars
Contact:

Postby Joan Garnet » Fri Feb 14, 2003 12:06 pm

ok,
I think this example will help you:

Code: Select all

<?php
$my_string = "    job , setup , office , supplies ";
$my_array = explode(",", $my_string);
for($i=0;$i<sizeof($my_array);$i++){
   $no_white_spaces = trim($my_array[$i]);
   echo $no_white_spaces."<br>";   
}
?>


bye!


Return to “mySQL & php coding”

Who is online

Users browsing this forum: No registered users and 1 guest