1

I have two tables in mysql. Following is my script.

<?php
$conn=mysql_connect("localhost",'root','');
mysql_select_db('test');

$sql    = "SELECT data_student.name,ozekimessagein.msg 
            FROM ozekimessagein,data_student
            WHERE ozekimessagein.sender=data_student.sender 
            ORDER BY ozekimessagein.msg";

$res=mysql_query($sql);
$cn=mysql_num_rows($res);
for($x=0;$x<$cn;$x++)
{
    list($name,$msg)=mysql_fetch_row($res);
    echo "<li>$name,$msg";
}
mysql_close($conn);
?>

this should display the name from table data_student table and the message from ozekimessagein as long as the sender in data_student table match with the sender in the ozekimessagein table. But it doesn't work.

arun
  • 3,667
  • 3
  • 29
  • 54
Chem Loco
  • 11
  • 1
  • Off Topic : check this [Why shouldn't I use mysql_* function in PHP?](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-function-in-php) – arun Nov 27 '12 at 08:52

2 Answers2

0

Try this ::

SELECT 

data_student.name,
ozekimessagein.msg 

FROM ozekimessagein 
inner join data_student     on ozekimessagein.sender=data_student.sender 

ORDER BY ozekimessagein.msg
Sashi Kant
  • 13,277
  • 9
  • 44
  • 71
  • i already try such code..still it doesn't work. . it shpuld display for example **Name**: Message . .But this paramaters will be fetch from different tables as long as sender from ozekimessagein table match with the sender in data_student table. – Chem Loco Nov 27 '12 at 08:52
0

try this

 <?php
  $conn=mysql_connect('localhost','root','');
    mysql_select_db('test');

 $sql    = "

   SELECT s.name AS names, o.msg AS msgs
  FROM ozekimessagein o
  INNER JOIN data_student s
   ON o.sender = s.sender
   ORDER BY s.name";

  $res=mysql_query($sql);

  while ($row = mysql_fetch_assoc($res)) {

 echo "<li>".$row['names']." : ".$row['msgs']."</li>";
     }
  mysql_close($conn);
  ?>
echo_Me
  • 37,078
  • 5
  • 58
  • 78
  • i did try your code. .But unfortunately nothing appears on the browser. – Chem Loco Nov 27 '12 at 09:21
  • try make this in top and see what error u are getting `ini_set('display_errors', 1);error_reporting(E_ALL);` – echo_Me Nov 27 '12 at 09:34
  • can u post your sql table also ? – echo_Me Nov 27 '12 at 09:39
  • im currently using a xammp server.. I already check mysql tables(ozekimessagein & data_student) they both contains a sender column, it goes along with the code. i don't know where the error is. As i try to reload the page, i end up nothing in the webpage. – Chem Loco Nov 27 '12 at 09:50
  • try put this under `mysql_select_db` to see if it connected to your database `if (!$conn) { die('Could not connect: ' . mysql_error()); }` – echo_Me Nov 27 '12 at 09:54
  • it says there is an error on while condition .. supplied argument is not a valid MySQL result resource. – Chem Loco Nov 27 '12 at 10:06
  • still nothing appears on the webpage..anyway thank you peter. . :) – Chem Loco Nov 27 '12 at 10:14