parametr's from mysql_fetch_assoc() don't want to displays ; - php

i can't understand why echo don't displays value. Query return values in phpmyadmin sow query is fine. Please help
$query="SELECT `doctor_name` , `doctor_secondname`
FROM `doctors`
INNER JOIN `patients` ON `patients`.doctor_id = `doctors`.doctor_id
WHERE `patients`.patient_id = '{.$patient_id.}'" ;
$result = mysql_query($query) or die(mysql_error());
print_r($row);
if(mysql_num_rows($result) > 0) {
$row = mysql_fetch_assoc($result);
echo "Your doctor is :";
echo $row['doctor_name'];
echo $row['doctor_secondname'];
}

Try This:
$query="SELECT doctor_name , doctor_secondname
FROM doctors
INNER JOIN patients ON patients.doctor_id = doctors.doctor_id
WHERE patients.patient_id = '".$patient_id."'" ;

Related

How to compare value in two tables from the same database

I have been trying to compare value in a table with another value in another table but only the else part is executing.
<?php
$sql = "SELECT * FROM user WHERE user_id=$row[user_id]";
$result = $conn -> query ($sql);
if ($result -> num_rows > 0) {
while ($row = $result ->fetch_assoc()) {
$sql1 = "SELECT * FROM jobpost WHERE jobpost_id=$_GET[id] ";
$result1 = $conn -> query ($sql1);
$row_count = mysqli_num_rows($result);
$row1_count = mysqli_num_rows($result1);
$remaining_rows = min($row_count, $row1_count);
$row = mysqli_fetch_assoc($result);
$row1 = mysqli_fetch_assoc($result1);
if($row["experience"] > $row1["experience"])
{
//some code to display something
echo "1";
}
else
{
echo "2";
}
} }
?>
Welcome to SO, it is hard to read your source code. You can use a simple query like this:
SELECT IF(u.experience > j.experience,1,0) FROM
(
(SELECT experience FROM user WHERE user_id = 1) u
JOIN
(SELECT experience FROM jobpost WHERE jobpost_id = 8) j
)
Try this query here: http://sqlfiddle.com/#!9/1742c0
Please check our datasource. Maybe the else case is correct.

select statments not working

problem occurred while creting comment system for my web site
select statement not working
$reslt = mysqli_query($connection,"SELECT * FROM tbl_users where id='".$_SESSION['id']."'");
$row= mysqli_fetch_array($reslt);
$comm = mysqli_query($connection,"SELECT * FROM tbl_comments where id='".$_SESSION['id']."'");
While( $row= mysqli_fetch_array($comm))
{
$comm[] = $row;
}
if i remove second statement ($comm) first statement works fine
My second question
how can i fetch data from database in php ?
Here is the code
$comm = mysqli_query($connection,"SELECT * FROM tbl_comments where id='".$_SESSION['id']."'");
While( $row= mysqli_fetch_array($comm))
{
$comm[] = $row;
}
Not getting results (comments)
echo $row['comment'];
Unsure of your schema, but perhaps something like this:
$result = $connection->query("
SELECT
C.comment AS comment, U.user AS user
FROM
tbl_comments C, tbl_users U
WHERE
C.id = U.id
");
while($data = $result->fetch_assoc()) {
echo"$data[comment] by $data[user] </br>";
}

How I can divide two statement in PHP

<?php
$q2 = "SELECT SUM(crd)
FROM coe_courses
INNER JOIN student_record ON coe_courses.course_number =
student_record.course_number WHERE student_record.id =".$_SESSION['user_id'] ;
$result = mysqli_query($con , $q2 ) ;
if($row = mysqli_fetch_array($result)){
echo $row["SUM(crd)"];
}
$q2a = "SELECT SUM(points) FROM student_record where student_record.id =".$_SESSION['user_id'] ;
$result = mysqli_query($con , $q2a ) ;
if($row = mysqli_fetch_array($result)){
echo $row["SUM(points)"];
}
?>
I wrote this code but how I can make division for those statements at the end of this code
echo $row["SUM(crd)"] /$row["SUM(points)"];
First of all you should store those two values in different variables because as per your code you are overriding the $row variable so you will get only second result. After that you can simply do this.
if(!empty($b)) {
$ans = (int) ($a/$b);
}

Fetch assoc error

Hello
i'm trying to make a table with this query but i cant get it to work. the thing is that it cant select 2 query's at a time but i dont know another way for it..
$active_ids = '1, 3, 4';
$query = "SELECT * FROM users WHERE id IN ({$active_ids})";
$result = $mysqli->query($query);
$query = "SELECT dj, count(*) AS n FROM timetable WHERE dj IN ({$active_ids}) GROUP BY dj";
$result = $mysqli->query($query);
while($row = $result->fetch_assoc()){
echo $row['username'], "<br/>";
echo $row['n'], "<br/>";
}
Try this:
$active_ids = '1, 3, 4';
$result = $mysqli->query("
(SELECT * FROM users WHERE id IN ({$active_ids}))
UNION
(SELECT dj, count(*) AS n FROM timetable WHERE dj IN ({$active_ids}) GROUP BY dj)
") ;
if (!$rec = mysqli_fetch_array($result)) {
echo ("Sorry, no records found");
}
else {
do {
echo ($rec["username"]);
echo "<br />";
echo ($rec["n"]);
echo "<br />";
}
while ($rec = mysqli_fetch_array($result));
}
$active_ids = '1, 3, 4';
$query = "SELECT users.* from users
LEFT JOIN timetable ON users.id=timetable.dj
WHERE users.id IN ({$active_ids})
UNION
SELECT timetable.dj,timetable.count(*) as n from timetable
RIGHT JOIN users ON timetable.dj=users.id
WHERE timetable.dj IN ({$active_ids}) GROUP BY timetable.dj
";
$result = $mysqli->query($query);
while($row = $result->fetch_assoc()){
echo $row['username'], "<br/>";
echo $row['n'], "<br/>";
}
I don't know it is certainly true but can you try this code ?

MySQL - A query based on another query

I write a lot of queries resembling the query example code below. I was wondering whether there was a more efficient code/script?
$query1 ="SELECT * FROM table1 WHERE date >= '$todaysdate' ";
$result1 = mysql_query($query1)
or die ("Error in query: $query1. " . mysql_error());
if (mysql_num_rows($result1) > 0) {
while($row1 = mysql_fetch_object($result1)) {
echo "$row1-date";
$query2 ="SELECT * FROM table2 WHERE table1ID >= '$row1-table1ID' ";
$result2 = mysql_query($query2)
or die ("Error in query: $query2. " . mysql_error());
if (mysql_num_rows($result2) > 0) {
while($row2 = mysql_fetch_object($result2)) {
echo "$row->datatable2";
}
}
}
}
Try using SQL JOINs, like the following example:
SELECT
*
FROM
table1
INNER JOIN
table2 ON (table2.table1ID = table1.ID)
WHERE
table1.date >= '2009-12-20';
I don't know about the structure of your tables, but I've modified your code so that it uses a join:
$query = 'SELECT table1.date, table2.datatable2 FROM table1, table2 WHERE table1.date >= \''.$todaysdate.'\' AND table2.table1ID >= table1.table1ID';
$result = mysql_query($query)
or exit('Error in query: '.$query.' '.mysql_error());
if (mysql_num_rows($result) > 0)
{
while($row = mysql_fetch_object($result))
{
echo $row->date;
echo $row->datatable2;
}
}
In this case you select multiple tables with FROM separated with commas, but you can also use INNER/OUTER/LEFT/RIGHT JOIN (see the link in the first answer).
You can use PDO.
Your queries should look like this..
$sth = $dbh->prepare("SELECT name, colour FROM fruit");
$sth->execute();
$result = $sth->fetchAll(PDO::FETCH_COLUMN);
// OR
$result = $sth->fetchAll(PDO::FETCH_OBJ);
var_dump($result);
PDO MANUAL: http://php.net/manual/en/book.pdo.php

Categories