I am trying to execute this query but i got error " Undefined index:
lname".I want to count row from one column(fname) from table a and
select column(lname) from other table b. so please help me.
$result = mysql_query("SELECT COUNT(fname),lname FROM a,b");
while ($row = mysql_fetch_array($result))
{
echo "<tr><td>";
echo $row['lname'];
echo "</td>";
echo "<td>";
echo $row['COUNT(fname)'];
echo "</td></tr>";
}
If you still get an error you can try to fetch both separately:
$result = mysql_query("SELECT COUNT(fname) FROM a");
while ($row = mysql_fetch_array($result))
{
echo "<tr><td>";
echo $row['COUNT(fname)'];
echo "</td></tr>";
}
$result1 = mysql_query("SELECT lname FROM b");
while ($row = mysql_fetch_array($result1))
{
echo "<tr><td>";
echo $row['lname'];
echo "</td></tr>";
}
You need to use an alias. Use this:
$result = mysql_query("SELECT COUNT(fname) AS countfname,lname FROM a,b");
while ($row = mysql_fetch_array($result))
{
echo "<tr><td>";
echo $row['lname'];
echo "</td>";
echo "<td>";
echo $row['countfname'];
echo "</td></tr>";
}
Try this code:
$result = mysql_query("SELECT COUNT(a.fname) as fname,b.lname as lname FROM a,b");
while ($row = mysql_fetch_array($result))
{
echo "<tr><td>";
echo $row['lname'];
echo "</td>";
echo "<td>";
echo $row['COUNT(fname)'];
echo "</td></tr>";
}
Related
<?php
$sql = "SELECT * from movieinfo";
$sql_data = mysqli_query($db,$sql);
echo "<table>";
echo "<tr>";
echo "<td><b>Movie Name</b></td>";
echo"<td><center><b>Delete</b></center></td>";
echo"</tr>";
while($row = mysqli_fetch_array($sql_data,MYSQLI_ASSOC)) {
echo "<tr>";
echo "<td style='color:white'>";
echo $row['title'];
echo "<td><center><a href='adminpage.php?mid={$row['movieid']}'><button
class='contact100-form-btn' name='deletem'>Delete Movie</button></a>
</center></td>";
$_SESSION['delete'] = $row['movieid'];
echo "<td>";
echo"</tr>";
}
echo "</table>";
?>
if (isset($_POST['deletem'])) {
$delete=$_SESSION['delete'];
$query6 = "DELETE FROM movieinfo WHERE movieid=$delete";
mysqli_query($db, $query6);
}
i need to delete the particular row with the id selected so i am storing in session the id and deleting it but when i press on the deleting button it deletes the last row only its because session is storing all the id's because of loop how should i fix it?
<?php
$sql = "SELECT * from movieinfo";
$sql_data = mysqli_query($db,$sql);
echo "<table>";
echo "<tr>";
echo "<td><b>Movie Name</b></td>";
echo"<td><center><b>Delete</b></center></td>";
echo"</tr>";
while($row = mysqli_fetch_array($sql_data,MYSQLI_ASSOC)) {
echo "<tr>";
echo "<td style='color:white'>";
echo $row['title'];
**echo "</td>";
$mid = isset($_GET['mid']) ? $_GET['mid'] : '';
$del = "DELETE FROM movieinfo WHERE movieid=$mid";
mysqli_query($db, $del);
{
echo "<td><center><a href='adminpage.php?mid={$row['movieid']}'><button
class='contact100-form-btn' name='deletem'>Delete Movie</button></a>
</center></td>";
}
echo"</tr>";
}
echo "</table>";
?>
I have this code in my program:
<?php
session_start();
$_SESSION['user_id']=201102887;
$con = mysqli_connect('localhost', 'root', '');
if(!$con)
{
die("not ok");
}
mysqli_select_db($con,"uoh");
$q = "SELECT * FROM courses
INNER JOIN transfer_student_courses
ON transfer_student_courses.course_number = courses.course_number
INNER JOIN transfered_courses
ON transfer_student_courses.sn = transfered_courses.sn
AND transfer_student_courses.student_ID = " . $_SESSION['user_id'];
$result = mysqli_query($con , $q);
if($result){
echo "<table>";
echo "<tr>";
echo "<th>equivalent</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row["equivalent"]. "</td>";
echo "</tr>";
}
echo "</table>";
}
mysqli_select_db($con,"uoh");
$q = "SELECT * FROM courses
LEFT JOIN degree_plan
ON degree_plan.course_number = courses.course_number
LEFT JOIN student_record
ON courses.course_number = student_record.course_number
AND student_record.id = ". $_SESSION['user_id']."
WHERE degree_plan.major = 'COE'
ORDER BY term_no";
$result = mysqli_query($con , $q );
if($result){
echo "<table>";
echo "<tr>";
echo "<th>course</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row["code"]. "</td>";
echo "</tr>";
}
}
echo "</table>";
?>
I have two queries in this code, each of which give me a list of courses.
If a course appears in the first list, I do not want it to appear in the second list.
If you see the output of this code below, the first query gives me MATH 101, and the second query also gives me MATH 101.
I want MATH 101 to not appear in the second course list because it also appears in the first list.
How can I write a function in PHP language to do that?
Output:
equivalent
MATH 101
course
PHYS 101
CHEM 101
PE 101
IAS 101
MATH 101
ENGL 101
First of all, store all the equivalent courses in an array, say $equivalent array. And then in the second while loop use in_array() function to check if the course already got printed in the first table or not.
Here's the reference:
in_array()
So your code should be like this:
<?php
session_start();
$_SESSION['user_id']=201102887;
$con = mysqli_connect('localhost', 'root', '');
if(!$con){
die("not ok");
}
mysqli_select_db($con,"uoh");
$q = "SELECT * FROM courses INNER JOIN transfer_student_courses ON
transfer_student_courses.course_number=courses.course_number INNER
JOIN transfered_courses ON transfer_student_courses.sn=transfered_courses.sn
AND transfer_student_courses.student_ID = " . $_SESSION['user_id'];
$result = mysqli_query($con , $q) ;
$equivalent = array();
if($result){
echo "<table>";
echo "<tr>";
echo "<th>equivalent</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
$equivalent[] = $row["equivalent"];
echo "<tr>";
echo "<td>" . $row["equivalent"]. "</td>";
echo "</tr>";
}
echo "</table>";
}
mysqli_select_db($con,"uoh");
$q = "SELECT * FROM courses
LEFT JOIN degree_plan ON degree_plan.course_number= courses.course_number
LEFT JOIN student_record ON courses.course_number= student_record.course_number
AND student_record.id= ". $_SESSION['user_id']."
WHERE degree_plan.major='COE' ORDER BY term_no";
$result = mysqli_query($con , $q ) ;
if($result){
echo "<table>";
echo "<tr>";
echo "<th>course</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
if(in_array($row["code"], $equivalent)){
continue;
}
echo "<tr>";
echo "<td>" . $row["code"]. "</td>";
echo "</tr>";
}
}
echo "</table>";
?>
You could load the results of the first query into an array, then when you are displaying the results for the second query, before you display anything check if the result exists in your array, if it does, skip it.
So after your first query:
$equivalent = array(); // Setup a blank array to store the results
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row["equivalent"]. "</td>";
echo "</tr>";
$equivalent[] = $row['equivalent']; // Add the result into your array
}
Then in your courses portion of code:
while($row = mysqli_fetch_array($result))
{
if ( ! in_array($row['code'], $equivalent ) )
{
echo "<tr>";
echo "<td>" . $row["code"]. "</td>";
echo "</tr>";
}
}
This should stop the display of any entry that is in both result sets being displayed in the second.
HTH
Solution : in_array("word", $array)
Run a query get the result into an array :
while($row = mysqli_fetch_array($result))
{
arr1[]=$row["code"];
}
For the second query run the query and add reults as follows:
while($row = mysqli_fetch_array($result)){
if (in_array($row[code], $arr1))
{
//don't add to array2
}
else
{
arr2[] = $row["code"];
}
}
Then you may echo the elements as required.
I've made a script for a search mysql table, and instead of one table with more rows, he return me on results page one table for each person.
This is my results code by id from the search script. and returns table for each person
$id = $_GET['id'];
$sql = "select * from wp_certificari WHERE id = '{$id}'";
$rst = mysql_query($sql);
while($a_row = mysql_fetch_assoc($rst)) {
echo "<center>\n";
echo "<b>Detalii Certificat:</b>";
echo "<table border='1'>";
echo"<thead>";
echo "<th>Denumire certificare</th>";
echo "<th>Serie si numar certificare</th>";
echo "<th>Data certificarii</th>";
echo "<th>Valabilitate certificare</th>";
echo "<th>Sector Financiar</th></tr>";
echo"</thead>";
echo "<td class='lalign'>{$a_row['nume']}</td>" ;
echo "<td class='lalign'>{$a_row['serie_numar']}</td>" ;
echo "<td class='lalign'>{$a_row['data']}</td>" ;
echo "<td class='lalign'>{$a_row['valabilitate']}</td>" ;
echo "<td class='lalign'>{$a_row['sector_financiar']}</td></tr>" ;
echo"</table>";
echo "</center>\n";}
You need to write your table and table head html outside your while loop Add mysql_num_rows to check empty result and add mysql_real_escape_string to Escapes special characters in a string for use in an SQL statement
$id = $_GET['id'];
$id=mysql_real_escape_string($id);
$sql = "select * from wp_certificari WHERE id = '{$id}'";
$rst = mysql_query($sql);
$row=mysql_num_rows($rst);
if($row>0){
//outside while loop
echo "<center>\n";
echo "<b>Detalii Certificat:</b>";
echo "<table border='1'>";
echo"<thead>";
echo "<th>Denumire certificare</th>";
echo "<th>Serie si numar certificare</th>";
echo "<th>Data certificarii</th>";
echo "<th>Valabilitate certificare</th>";
echo "<th>Sector Financiar</th>";
echo"</thead>";
while($a_row = mysql_fetch_assoc($rst)) {
echo "<tr><td class='lalign'>{$a_row['nume']}</td>" ;
echo "<td class='lalign'>{$a_row['serie_numar']}</td>" ;
echo "<td class='lalign'>{$a_row['data']}</td>" ;
echo "<td class='lalign'>{$a_row['valabilitate']}</td>" ;
echo "<td class='lalign'>{$a_row['sector_financiar']}</td></tr>" ;
}
//outside while loop
echo"</table>";
echo "</center>\n";
}
Note:- Don't use mysql because it is deprecated instead use mysqli or
PDO
To prevent sql injection check this link How can I prevent SQL-injection in PHP?
You have to get table part outside from while loop.
$id = $_GET['id'];
$sql = "select * from wp_certificari WHERE id = '{$id}'";
$rst = mysql_query($sql);
echo "<center>\n";
echo "<b>Detalii Certificat:</b>";
echo "<table border='1'>";
echo "<thead>";
echo "<tr>";
echo "<th>Denumire certificare</th>";
echo "<th>Serie si numar certificare</th>";
echo "<th>Data certificarii</th>";
echo "<th>Valabilitate certificare</th>";
echo "<th>Sector Financiar</th></tr>";
echo "</thead><tbody>";
while($a_row = mysql_fetch_assoc($rst)) {
echo "<tr>";
echo "<td class='lalign'>{$a_row['nume']}</td>" ;
echo "<td class='lalign'>{$a_row['serie_numar']}</td>" ;
echo "<td class='lalign'>{$a_row['data']}</td>" ;
echo "<td class='lalign'>{$a_row['valabilitate']}</td>" ;
echo "<td class='lalign'>{$a_row['sector_financiar']}</td></tr>";
}
echo"</tbody></table>";
echo "</center>\n";
Basically I have 4 fields in a form. I want to the user search for books in a library by either title, or by author or both. I also want the user to set the length of the list of items and from the starting point, these are not restrictions though the user does not have to specify.
Here is the code:
require_once __DIR__.'/config.php';
session_start();
$dbh = new PDO('mysql:host=' . DB_HOST . ';dbname=' . DB_USERNAME, DB_USERNAME, DB_PASSWORD);
$title = $_GET["title"];
$authors = $_GET["authors"];
$st = $_GET["start"];
$ln = $_GET["length"];
$stmt = $dbh->prepare("SELECT title, authors, description, price FROM books WHERE title = :title LIMIT :length OFFSET :start");
$stmt->execute(array(':title' => $title,':start' => $st,':length' => $ln));
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
$title = $row['title'];
$authors = $row['authors'];
$description = $row['description'];
$price = $row['price'];
}
echo "<table>";
echo "<tr>";
echo "<td>Title</td>";
echo "<td>$title</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Authors</td>";
echo "<td>$authors</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Description</td>";
echo "<td>$description</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Price</td>";
echo "<td>$price</td>";
echo "</tr>";
echo "</table>";
So far it literally just returns me what I have typed in the input - so nothing much at all! Does anyone know how I can do this?
Adapt your code this way:
echo "<table>";
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
$title = $row['title'];
$authors = $row['authors'];
$description = $row['description'];
$price = $row['price'];
echo "<tr>";
echo "<td>Title</td>";
echo "<td>$title</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Authors</td>";
echo "<td>$authors</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Description</td>";
echo "<td>$description</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Price</td>";
echo "<td>$price</td>";
echo "</tr>";
}
echo "</table>";
to print all the rows returned by the query. Your code was printing just the last row.
I've a html form which is insert data to mysql database and then get those data with following php code (From supplier_jv table)
<?php
include("include/address2.php");
include("include/menu.php");
$uname_ad = $_SESSION['uname_ad'];
$id = $_GET['id'];
$sql = mysql_query("SELECT * FROM supplier_jv WHERE jv_id = '$id'");
$num = mysql_num_rows($sql);
if($num == 0)
{
echo "<p><font color=red>Accounts is emtpy</font></p>";
}
else
{
$re_name = mysql_fetch_array($sql);
echo "<center><h2>";
echo "<strong>Accounts of </strong>";
echo $re_name['jv_name'];
echo "</h2></center>";
echo "<center>";
echo "<table>";
echo "<table border='0' cellpadding='5' cellspacing='5' width='1000'>";
echo "<tr/>";
echo "<td><strong>Date</strong></td>";
echo "<td><strong>Particular</strong></td>";
echo "<td><strong>Folio(C)</strong></td>";
echo "<td><strong>Folio(J)</strong></td>";
echo "<td><strong>Debit</strong></td>";
echo "<td><strong>Credit</strong></td>";
echo "<td><strong>Balance</strong></td>";
echo "</tr>";
while($re= mysql_fetch_array($sql))
{
$day = $re['day'];
$month $re['month'];
$year = $re['year'];
$parti = $re['particulars'];
$folio = $re['folio'];
$folio2 = $re['folio2'];
$debit = $re['debit'];
$credit = $re['credit'];
$balance = $re['balance'];
$b = $debit - $credit;
$total_debit = mysql_query("SELECT SUM(debit) FROM supplier_jv");
$re_t = mysql_fetch_array($total_debit);
$t_d = $re_t['SUM(debit)'];
$total_credit = mysql_query("SELECT SUM(credit) FROM supplier_jv");
$re_t2 = mysql_fetch_array($total_credit);
$t_c = $re_t2['SUM(credit)'];
$b = $t_d - $t_c;
echo "<tr>";
echo "<td>$day/$month/$year</td>";
echo "<td>$parti</td>";
echo "<td>$folio</td>";
echo "<td>$folio2</td>";
echo "<td>";
echo number_format($debit);
echo "</td>";
echo "<td>";
echo number_format($credit);
echo "</td>";
echo "<td></td>";
echo "</tr>";
}
echo "<tr bgcolor='#f3f3f3'>";
echo "<td></td>";
echo "<td></td>";
echo "<td></td>";
echo "<td></td>";
echo "<td></td>";
echo "<td><strong>Total Balance-</strong></td>;
echo "<td><strong>";
echo number_format($b);
echo "</strong></td>";
echo "</tr>";
echo "</table>";
echo "</center>";
}
?>
Well, After insert data then it's show:
Notice: Undefined variable: b in E:\xampp\htdocs\Accounts\admin\content
\supplier_account.php on line 108
But if i insert data in second time then it's OK!!.
Any idea or solution.
Thanks
shibbir.
You need to use isset to see if it is set, not null and also avoid the Notice: Undefined variable error:
if (isset($b))
{
// your code here
}
Where you have:
$b = $t_d - $t_c;
Make sure that there is some value coming up for $t_d and $t_c