I have wrote a select query using function but it is not returning data from MySQL database... can any one help me in this regard. I'm uploading screenshots.
Here is the name of my database, 'classified'
this is conn.php file
$conn = mysql_connect('localhost','root','');
echo mysql_select_db('classified',$conn);
function to fetch record from database, 'adds' table
On index.php i've wrote this code.
include 'functions/crud_functions.php'
$adds = get_all_adds();
foreach($adds as $add) {
<tr>
<td><?php echo $add['id']; ?></td>
<td><?php echo $add['ad_title'];?></td>
<td><?php echo $add['cat_id'];?></td>
<td><?php echo $add['ad_description'];?></td>
<td><?php echo $add['avatar'];?></td>
<td><?php echo $add['price'];?></td>
<td><?php echo $add['contact'];?></td>
<td><?php echo $add['name'];?></td>
<td><?php echo $add['time'];?></td>
<td>Edit | Del </td>
</tr>
but this is not displaying records.
Change :
if ($result) {
return TRUE;
} else {
return FALSE;
}
into this :
if ($result) {
return $data;
} else {
return array();
}
Related
I'm new in PHP with OCI connection. I want to retrieve some data from database and insert it into a table form. But it keep show an error
Trying to get property 'attribute' of non-object
I have try to use oci_fetch object / oci_fetch_array, but it still the same. I also have followed some tutorial but it doesn't help.
This is for my mini project for this semester.
Here my source code:
$sql="SELECT borrow.book_id, book_title, borrow.stud_id, stud_name, book_bdate, return_date, due_date
FROM book
JOIN borrow
ON book.book_id = borrow.book_id
JOIN student
ON borrow.stud_id = student.stud_id
where borrow.stud_id = '$stud_id'
ORDER BY 5 DESC ";
$query=oci_parse($link,$sql) or die ("error here!");
oci_execute($query);
while (($row = oci_fetch_array($query, OCI_ASSOC)) != false) {
?>
<td><?php echo $row->stud_id; ?></td>
<td><?php echo $row->book_id; ?></td>
<td><?php echo $row->book_title; ?></td>
<td><?php echo $row->book_bdate; ?></td>
<td><?php echo $row->due_date; ?></td>
<td><?php echo $row->return_date; ?></td>
<td>
<center><a href='return-book.php?book_id=<?php echo $row->book_id; ?>'>Update</a></center>
</td>
<tr>
<?php
}
}
oci_close($link);
?>
Oracle returns field names as uppercase by default so you need to use uppercase indexes like so:
Here the solution where I got.
Btw thank you everyone for helping me.
Correct Your while loop You are fetching data as array:
oci_fetch_array($query, OCI_ASSOC) // return associated array
while (($row = oci_fetch_array($query, OCI_ASSOC)) != false) {
?>
<td><?php echo $row['stud_id']; ?></td>
<td><?php echo $row['book_id']; ?></td>
<td><?php echo $row['book_title']; ?></td>
<td><?php echo $row['book_bdate']; ?></td>
<td><?php echo $row['due_date']; ?></td>
<td><?php echo $row['return_date']; ?></td>
<td>
<center><a href='return-book.php?book_id=<?php echo $row['book_id']; ?>'>Update</a></center>
</td>
<tr>
<?php
}
I've got an MSSQL server running with several large tables. I'm trying to place them into an HTML table so that I can display all of the data in a nice CSS modified webpage. I'm using PHP to ferry the information from SQL to my HTML script, but the only way I've found so far to generate such a table is by hardcoding all of the SQL column names into my PHP-SQL query.
As you can see below, this is the setup required for just one such table. Is there any more concise way to get the information from SQL into a formatted HTML table? I've looked around for a number of hours perhaps for some sort of PHP scripted loop that can iterate through all of the SQL columns, but I haven't found anything. I greatly appreciate any input!
<table id="capacitors">
<thead>
<tr>
<td>Part Number</td>
<td>Capacitance</td>
<td>Capacitance Tolerance</td>
<td>Case Package</td>
<td>Case Package SI</td>
<td>Dielectric Char.</td>
<td>Dielectric Mat.</td>
<td>Halogen Free Stat.</td>
<td>Insulation Resistance</td>
<td>Lead Free Stat.</td>
<td>Lifecycle Stat.</td>
<td>Mounting Style</td>
<td>Operating Temp.</td>
<td>Packaging</td>
<td>Pin Count</td>
<td>Reach SVHC Comp.</td>
<td>Rohs Stat.</td>
<td>Size Height</td>
<td>Size Length</td>
<td>Size Thickness</td>
<td>Size Width</td>
<td>Temp. Coefficient</td>
<td>Voltage Rating DC</td>
</tr>
</thead>
<tbody>
<?php
foreach ($db->query($sql) as $rows){
?>
<tr>
<td><?php echo $rows['part_number']?></td>
<td><?php echo $rows['capacitance']?></td>
<td><?php echo $rows['capacitance_tolerance']?></td>
<td><?php echo $rows['case_package']?></td>
<td><?php echo $rows['case_package_si']?></td>
<td><?php echo $rows['dielectric_characteristic']?></td>
<td><?php echo $rows['dielectric_material']?></td>
<td><?php echo $rows['halogen_free_status']?></td>
<td><?php echo $rows['insulation_resistance']?></td>
<td><?php echo $rows['lead_free_status']?></td>
<td><?php echo $rows['lifecycle_status']?></td>
<td><?php echo $rows['mounting_style']?></td>
<td><?php echo $rows['operating_temperature']?></td>
<td><?php echo $rows['packaging']?></td>
<td><?php echo $rows['pin_count']?></td>
<td><?php echo $rows['reach_svhc_compliance']?></td>
<td><?php echo $rows['rohs_status']?></td>
<td><?php echo $rows['size_height']?></td>
<td><?php echo $rows['size_length']?></td>
<td><?php echo $rows['size_thickness']?></td>
<td><?php echo $rows['size_width']?></td>
<td><?php echo $rows['temperature_coefficient']?></td>
<td><?php echo $rows['voltage_rating_dc']?></td>
</tr>
<?php
}
?>
</tbody>
</table>
This is basic example using PHP Driver for SQL Server:
<?php
# Settings
$server = 'server\instance,port';
$database = 'database';
$user = 'user';
$password = 'password';
$tablename = 'tablename';
# Connection
$cinfo = array(
"Database" => $database,
"ReturnDatesAsStrings" => true,
"UID" => $user,
"PWD" => $password
);
$conn = sqlsrv_connect($server, $cinfo);
if($conn === false)
{
echo "Error (sqlsrv_connect): ".print_r(sqlsrv_errors(), true);
exit;
}
# SQL statement
$sql = "SELECT * FROM [".$tablename."]";
$stmt = sqlsrv_prepare($conn, $sql);
if($stmt === false) {
echo "Error (sqlsrv_prepare): ".print_r(sqlsrv_errors(), true);
exit;
}
# Columns names
echo '<table id="'.$tablename.'">';
echo "<thead>";
echo "<tr>";
$metadata = sqlsrv_field_metadata($stmt);
foreach($metadata as $field) {
echo "<td>".$field['Name']."</td>";
}
echo "</tr>";
echo "</thead>";
# Table rows
echo "<tbody>";
if (!sqlsrv_execute($stmt)) {
echo "Error (sqlsrv_execute): ".print_r(sqlsrv_errors(), true);
exit;
}
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
echo "<tr>";
foreach($row as $value) {
echo "<td>".$value."</td>";
};
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
# End
sqlsrv_free_stmt($stmt);
sqlsrv_close($conn);
?>
Maybe use SHOW COLUMNS or DESCRIBE
$columns = "SHOW COLUMNS FROM <table>";
$output = mysqli_query($conn,$sql);
while($row = mysqli_fetch_array($result)){
echo $row['Field']."<br>";
}
I'm kind of new in php and i'm searching a way to get some words from a specific column that i get from a sql server stored proc. These 'words' will me to use them as style for my css
Example :
Column HTML_CODE has sometime 'bold', 'green' or something else. Sometime they are in the same column, not always in the same order. I don't know how to use them in my loop. See what i has until now :
$result = sqlsrv_query($conn, $sql);
while ($row = sqlsrv_fetch_array($result))
{
//print_r( $row ); // debug code
if ($row['HTML_CODE'] == 'BOLD(), Green()'){
$couleur='green';
$font= 'bold';
}
else {
$couleur='black';
$font= 'normal';
}
?>
<tbody>
<tr>
<?php echo "<tr style=\"font-weight:$font; color:$couleur;\">"; ?>
<td><?php echo ($row['Nom']); ?></td>
<td><?php echo ($row['Quantite']); ?></td>
<td><?php echo ($row['Montant']); if (is_numeric($row['Montant'])) {
echo ' $';
}?></td>
<td><?php echo ($row['#GL']); ?></td>
<td><?php echo ($row['Debit']); if (is_numeric($row['Debit'])) {
echo ' $';
} ?></td>
<td><?php echo ($row['Credit']); if (is_numeric($row['Credit'])) {
echo ' $';
}?></td>
</tr>
</tbody>
if( strstr($row['HTML_CODE'], "BOLD()")){ $font= 'bold'; }
This is View file. I am trying to pass- "$row['ISBN'] . ':SearchByAuthor:' . $Author"- abc:SearchByAuthor:aka - value to function increment_author to controller file.
<?php
?>
<tr>
<td><?php
echo $row['ISBN'];
?></td>
<td><?php
echo $row['name'];
?></td>
<td><?php
echo $row['title'];
?></td>
<td><?php
echo $row['year'];
?></td>
<td><?php
echo $row['price'];
?></td>
<td><?php
echo $row['publisher'];
?></td>
<td><?php
echo $row['number'];
?></td>
<td> <a href="<?php
echo base_url();
?>/increment_author/<?php
echo $row['ISBN'] . ':SearchByAuthor:' . $Author;
?>">
Add to cart</a></td>
</tr>
<?php
?>
This is contoller file and I am doing some calculations and trying the same to return to above search page, which is view. However, it's not working. I am trying to convert native php code to codeIgniter framework. Please help me out in passing parameters from Controller to View and vice versa. Thanks in Advance
public function increment_author($SearchByAuthor)
{
session_start();
$db = new mysqli('localhost','root','','cheapbook') or die('Error connecting to MySQL server.');
mysqli_set_charset($db, 'utf8');
if(isset($SearchByAuthor))
{
// echo $_GET['add'];
$pieces=explode(":", $SearchByAuthor);
$quantity="SELECT D.ISBN, D.title, sum(D.number) number FROM
(SELECT A.ISBN, title, number number FROM
book A, Stocks B WHERE A.ISBN='$pieces[0]' and
A.ISBN=B.ISBN)D
GROUP BY D.ISBN, D.title";
$result = mysqli_query($db,$quantity);
while($quantity_row = mysqli_fetch_array($result))
{
if($_SESSION['cart_'.$pieces[0]])
{
$chan=0;
}
else
{
$_SESSION['cart_count']+=1;
}
if($quantity_row['number']!=$_SESSION['cart_'.$pieces[0]] )
{
$_SESSION['cart_'.$pieces[0]]+=1;
$_SESSION["cartTotal"]+=1;
}
}
$data['SearchByAuthor']=$pieces[0];
}
//header("location:search_vi?SearchByAuthor=".$pieces[0]);
//$data['SearchByAuthor']=$pieces[0];
$this->load->view('search',$data);
}
You don't have to use session_start actually you should use Session Library.
Also there is a whole Database layer in CI.
Try to debug your problem in more details. Check if your $data variable contains proper result, and in your view this will be visible via $SearchByAuthor variable.
I use codeignIter. I use this code in view, why the window is not get close when I get click. I use this function
function setproduct(id,partno,nama,qtyscpend,ketsc,index) {
var check=0;
for (i=0;i<window.opener.$("#txtNoOfRow").val();i++) {
if (id==window.opener.$("#ids_"+i).val()) {
alert("Data Sudah Anda Pilih");
check=1;
}
}
if (check == 0){
window.opener.document.getElementsByName("ids_"+index)[0].value = id;
window.opener.document.getElementsByName("partno_"+index)[0].value = partno;
window.opener.document.getElementsByName("partname_"+index)[0].value = nama;
window.opener.document.getElementsByName("qtyscpend_"+index)[0].value = qtyscpend;
window.opener.document.getElementsByName("ketsc_"+index)[0].value = ketsc;
window.close();
}
}
Then this for show data:
<?php
$no = 1;
foreach($query->result() as $row) {?>
<tr class="even gradeA">
<td><?php echo $no ;?></td>
<td><?php echo $row->partno;?></td>
<td><?php echo $row->partname;?></td>
<td><?php echo $row->perpo;?></td>
<td><?php echo date('d F Y',strtotime($row->sched)); ?></td>
<td><?php echo $row->qtyscpend;?></td>
<td><?php echo $row->ketsc;?></td>
<td align="center">Pilih</td>
</tr>
<?php
$no++;
}
?>
What I want is when I click that align, the windows form is closed and send the variable like I wrote.
Javascript can only close those windows that were previously open with script, it is a security precaution.
https://developer.mozilla.org/ru/docs/Web/API/Window/close