MySQLi output error - php

I have a problem. I have two tables in my database which I want to write as an output (their names) but it always writes only the first one in the database. Can anybody fix my code and explain why that fix is necessary?
$result = mysqli_query($conn, "SHOW TABLES");
while($tableName = mysqli_fetch_array($result))
{
$table = $tableName[0];
$result2 = mysqli_query($conn, "SELECT * FROM `".$table."`");
$query = "SELECT COUNT(*) FROM `".$table."`";
$result = mysqli_query($conn, $query);
$rows = mysqli_fetch_row($result);
$querys = "SELECT * FROM `".$table."` ORDER BY id DESC LIMIT 1";
$resulty = mysqli_query($conn, $querys);
$rowsy = mysqli_fetch_row($resulty);
while($row2 = mysqli_fetch_assoc($result2))
{
$p = $row2['authoroftopic'];
$s = $rows[0];
$l = $rowsy[1];
echo "<div class='discussionTable'>
<div class='dcolumn' style='width: 60%; position: absolute; padding-left: 10px;'>
<b><h3 style='padding: 15px;'><a href='discussion_gaming.php' style='color: white'>$table</a></b><br><p style='font-size: 13px'>Author: $p</p></h3>
</div>
<div style='padding-top: 10px;'>
<div class='dcolumn' style='width: 30%; float: right;'>
<p style='padding: 15px; line-height: 0px'>Total comments: $s</p>
<p style='padding-left: 15px; padding-top: 0px; line-height: 0px'>Last comment by: $l</p>
</div>
</div>
</div>";
}
}

You use the variable $result for two result sets at the same time - the first to hold the list of available tables, and the second one to gather data from a single table. You should rename one of the two occurences to not override the outer result within the loop

Related

HTML tables structure when using php+mysql [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 months ago.
Improve this question
I'm trying to generate HTML- table using following code:
<?php
$stid = oci_parse($conn, "select distinct concat(concat(order_no, '_'), sequence_no) from database
where order_no LIKE 'M114244' and release_no = '1'");
oci_execute($stid);
$stid2 = oci_parse($conn, "select oper_status_code from database
where order_no LIKE 'M114244' and release_no = '1' and operation_no = '22'");
oci_execute($stid2);
?>
<table style=" width: 60%; text-align: left; position: absolute; top:220px; left:440px; font-size: 10px;">
<tbody>
<tr>
<th>ORDER</th></td><th>STAGE</th></td>
</td>
<?php
while (($row = oci_fetch_array($stid, OCI_BOTH))) {
foreach ($row as $item) {
echo '</tr><td>'.($row[0]).'</td>';
}}
while (($row = oci_fetch_array($stid2, OCI_BOTH))) {
foreach ($row as $item) {
echo '</td><td>'.($row[0]).'</td>';
}}
?>
</tbody>
</table>
For some reason I will get duplicate M-numbers, which I should't get but this is not the main question this time. Maybe I will later on find out the reason for this. Please don't mind that Finnish language inside the table. I would like to echo this table with following structure:
That way the state of different stages could be seen from table.
Get the picture from the problem?
Appreciated from all possible help and tips.
<?php
$stid = oci_parse($conn, "select distinct concat(concat(order_no, '_'), sequence_no) from database
where order_no LIKE 'M114244' and release_no = '1'");
oci_execute($stid);
$stid2 = oci_parse($conn, "select oper_status_code from database
where order_no LIKE 'M114244' and release_no = '1' and operation_no = '22'");
oci_execute($stid2);
?>
<table style=" width: 60%; text-align: left; position: absolute; top:220px; left:440px; font-size: 10px;">
<tbody>
<tr>
<th>ORDER</th></td><th>STAGE</th>
</tr>
<?php $orders= [];
while (($row = oci_fetch_array($stid, OCI_BOTH))) {
foreach ($row as $item) {
$orders[] = ($row[0]);
}
}
$i = 0;
while (($row = oci_fetch_array($stid2, OCI_BOTH))) {
echo '<tr><td>'.$orders[$i].'</td>';
foreach ($row as $item) {
echo '<td>'.($row[0]).'</td>';
}
echo '</tr>';
$i++;
}
?>
</tbody>
</table>

I am getting only one row from database php

I have this following PHP code in search.php to gather information from the form in the same file. It's a search engine and when I put something into the search bar I get only one row even when I should get more than one row.
Here is php script:
<?php
$error = "";
$con=mysqli_connect('localhost', 'root', '');
$db=mysqli_select_db($con, 'vodici');
if(isset($_POST['button'])){ //trigger button click
$search=$_POST['search'];
$query=mysqli_query($con,"select distinct * from users where meno like '%{$search}%' || priezvisko like '%{$search}%' || mesto like '%{$search}%' || kraj like '%{$search}%' || rok_narodenia like '%{$search}%' || email like '%{$search}%' ");
if (mysqli_num_rows($query) > 0) {
while($row = mysqli_fetch_array($query)) {
$div = "<div style='border: 1px solid black; border-radius: 5px;'>".$row['id'];
$vysledok = "<h2 style='text-decoration: underline; padding-left: 2%;'>".$row['meno']." ".$row['priezvisko']."</h2>";
$mail = "<p style='padding-left : 2%; font-size: 11px;'><strong>Rok narodenia:</strong> ".$row['rok_narodenia']." <br><strong>E-mail: </strong> ".$row['email']."</p>";
$mesto = "<p style='padding-left: 2%'><strong>Mesto:</strong> ".$row['mesto']." <strong>Kraj:</strong> ".$row['kraj']."</p>";
$div_end = "</div>";
}
}else{
$error = "Nič sme nenašli :/ <br><br>";
}
}
mysqli_close($con);
?>
Here is form:
<form class="search-form" action="" method="post">
<div class="input-group">
<input name="search" type="text" class="form-control" placeholder="Vyhľadať...">
<div class="input-group-append">
<button class="btn btn-secondary" type="submit" name="button"><i class="fa fa-search"></button></i>
</div>
</div>
</form>
<p><?php if(isset($_POST['button']) && $error == "") {
while($row = mysqli_fetch_array($query)) {
echo $div,$vysledok,$mail,$mesto,$div_end;
}
}?></p>
<p><?php if(isset($_POST['button']) && $error !== "") {echo $error;}?></p>
What can I do here? I am out of ideas
Thanks
You only added one row. You need to add each row to the generated html.
$result = '';
if (mysqli_num_rows($query) > 0) {
while($row = mysqli_fetch_array($query)) {
$div = "<div style='border: 1px solid black; border-radius: 5px;'>".$row['id'];
$vysledok = "<h2 style='text-decoration: underline; padding-left: 2%;'>".$row['meno']." ".$row['priezvisko']."</h2>";
$mail = "<p style='padding-left : 2%; font-size: 11px;'><strong>Rok narodenia:</strong> ".$row['rok_narodenia']." <br><strong>E-mail: </strong> ".$row['email']."</p>";
$mesto = "<p style='padding-left: 2%'><strong>Mesto:</strong> ".$row['mesto']." <strong>Kraj:</strong> ".$row['kraj']."</p>";
$div_end = "</div>";
$result .= $div. $vysledok. $mail.$mesto. $div_end ;
}
}else{
$error = "Nič sme nenašli :/ <br><br>";
}
echo $result;
}
In compensation to wakeels answer, that is correct, I would like to ask you to use prepared statements like in my example below
$sql="
SELECT `id`,`meno`,`priezvisko`,`rok_narodenia`,`email`,`mesto`,`kraj`
FROM `users`
WHERE
`meno` LIKE '%?%'
OR `priezvisko` LIKE '%?%'
OR `mesto` LIKE '%?%'
OR `kraj` LIKE '%?%'
OR `rok_narodenia` LIKE '%?%'
OR `email` LIKE '%?%'
;"
$stmt = $con->prepare($sql);
$stmt->bind_param("s", $search);
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
echo $div,$vysledok,$mail,$mesto,$div_end;
}

Place <a> elements inside two different divs

I have this structure on my page:
*{
margin: 0px;
}
.div1{
width: 1000px;
background-color: grey;
overflow: hidden;
}
.div2{
width: 300px;
background-color: orange;
height: 500px
}
.div3{
width: 300px;
background-color: orange;
height: 500px;
}
.float_left{
float: left;
}
.float_right{
float: right;
}
<div class="div1">
<div class="div2 float_left">
</div>
<div class="div3 float_right">
</div>
</div>
And inside the two orange container's I want to put some smaller divs, but I read the data from a database. The first row should be in div1, the second in div2, the third in div1 and so on.
$sql = "SELECT * FROM question ORDER BY question_id DESC";
$result = mysqli_query($db,$sql);
while($row = mysqli_fetch_assoc($result)){
}
But how can I do something like that? Can I open a div container closed container to place a div container inside the container?
You should really read and try by yourself, this is pretty basic question.
There are many ways, here is one of them.
Declare 2 variables to store the results for Div1 and Div2.
Declare a count variable and use the odd and even property to decide who turn is it to store the results.
Output the results.
PHP:
$sql = "SELECT * FROM question ORDER BY question_id DESC";
$result = mysqli_query($db,$sql);
$resultsForDiv1 = "";
$resultsForDiv2 = "";
$count = 0;
while($row = mysqli_fetch_assoc($result)){
if ($count%2 == 0) {
$resultsForDiv1 .= $row[0]; // You should change it to whatever data you need from $row.
}
else {
$resultsForDiv2 .= $row[0]; // You should change it to whatever data you need from $row.
}
$count++;
}
Html:
<div class="div1">
<div class="div2 float_left">
<?php echo $resultsForDiv1; ?>
</div>
<div class="div3 float_right">
<?php echo $resultsForDiv2; ?>
</div>
</div>
What you should do is store the data from the $row variable into new variables which you can then output in the two columns. Like this:
<?php
$sql = "SELECT * FROM question ORDER BY question_id DESC";
$result = mysqli_query($db, $sql);
// Use this to toggle column on the sql results
$left_right = true;
while ($row = mysqli_fetch_assoc($result)) {
if ($left_right) {
// Add to left column variable
// [some code to add the data from $row to $left_column_content]
} else {
// Add to right column variable
// [some code to add the data from $row to $right_column_content]
}
// Switch columns by inverting the boolean from true to false, false to true
$left_right = !$left_right;
}
?>
<div class="div1">
<div class="div2 float_left">
<?php echo $left_column_content; ?>
</div>
<div class="div3 float_right">
<?php echo right_column_content; ?>
</div>
</div>

PHP - empty table appearing after first result from nested while loop

I'm getting a table appearing after the first result of the nested while loop.
What I'm trying to do is display the usernames as a list by selecting from the "users" table, then comparing that list with the project engineers from the "current projects" table.
This way there is a list of project engineers with their assigned projects underneath their names.
Unfortunately, I'm after the first nested while loop runs, it spits out a empty table and I'm not sure how to get rid of it.
I'm suspecting it has to do with the initial variable of $proj_engineer = "";
Does anyone know where this empty table is coming from and how to get rid of it?
Below is the code:
<?php
$query = mysql_query("SELECT * FROM `users` ORDER BY `username` ASC") or die(mysql_error());
while ($row = mysql_fetch_assoc($query)) {
$user_id = $row['user_id'];
$username = $row['username'];
$proj_engineer = "";
$query1 = mysql_query("SELECT * FROM `current_projects` WHERE `proj_engineer`='$username' ORDER BY `proj_engineer` ASC") or die(mysql_error());
while ($row1 = mysql_fetch_assoc($query1)) {
$proj_id = $row1['proj_id'];
$proj_engineer = $row1['proj_engineer'];
}
echo "<table border=1><tr><td colspan=12><p class='bold18'>" . $proj_engineer . "</p></td></tr>";
$query3 = mysql_query("SELECT * FROM `current_projects` WHERE `proj_engineer`='$username' ORDER BY `proj_id` DESC") or die(mysql_error());
while ($row3 = mysql_fetch_assoc($query3)) {
$proj_id = $row3['proj_id'];
$proj_number = $row3['proj_number'];
$proj_name = $row3['proj_name'];
$proj_sort = $row3['proj_sort'];
$proj_start = $row3['proj_start'];
$proj_finish = $row3['proj_finish'];
$proj_overstat = $row3['proj_overstat'];
$proj_dwgstat = $row3['proj_dwgstat'];
$proj_soostat = $row3['proj_soostat'];
$proj_substat = $row3['proj_substat'];
$proj_engineer = $row3['proj_engineer'];
$proj_drafter = $row3['proj_drafter'];
$proj_rating = $row3['proj_rating'];
$proj_pending = $row3['proj_pending'];
$proj_notes = $row3['proj_notes'];
$start_time = date("m/d/y", $proj_start);
$finish_time = date("m/d/y", $proj_finish);
echo "
<tr>
<td width=40>$proj_number</td>
<td width=100>$proj_engineer</td>
<td width=100><a href='./project-page.php?proj_id=$proj_id'>$proj_name</a></td>
<td width=40>$start_time</td>
<td width=40>$finish_time</td>
<td width=110>
<div style='position:relative; background:url(images/bar01.gif); width:$proj_overstat; height:20;'>
<div style='position:absolute; bottom:0; left:0; width:$proj_overstat; font-weight:bold; color:#000000; vertical-align:middle; height:20; text-align:center;'>$proj_overstat%</div>
</div>
</td>
<td width=110>
<div style='position:relative; background:url(images/bar02.gif); width:$proj_dwgstat; height:20;'>
<div style='position:absolute; bottom:0; left:0; width:$proj_dwgstat; font-weight:bold; color:#000000; vertical-align:middle; height:20; text-align:center;'>$proj_dwgstat%</div>
</div>
</td>
<td width=110>
<div style='position:relative; background:url(images/bar03.gif); width:$proj_soostat; height:20;'>
<div style='position:absolute; bottom:0; left:0; width:$proj_soostat; font-weight:bold; color:#000000; vertical-align:middle; height:20; text-align:center;'>$proj_soostat%</div>
</div>
</td>
<td width=110>
<div style='position:relative; background:url(images/bar04.gif); width:$proj_substat; height:20;'>
<div style='position:absolute; bottom:0; left:0; width:$proj_substat; font-weight:bold; color:#000000; vertical-align:middle; height:20; text-align:center;'>$proj_substat%</div>
</div>
</td>
<td width=40 align='center'><a href='project-notes.php?proj_id=$proj_id'><img src='images/note.png' border=0></a></td>
<td width=40 align='center'><a href='./project-edit.php?proj_id=$proj_id'>EDIT</a></td>
<td width=40 align='center'><a href='./project-delete.php?proj_id=$proj_id'>DELETE</a></td>
</tr>
";
}
echo "</table><br>";
}
?>
This might be because, your query: -
$query1 = mysql_query("SELECT * FROM `current_projects` WHERE
`proj_engineer`='$username' ORDER BY `proj_engineer` ASC")
or die(mysql_error());
fetched you an empty value for $proj_engineer. You can do a check before printing your table, whether your variable contains a value instead. Try printing the $proj_engineer before printing the table.
You can enclose your code echoing the table inside an if construct, which will print table only when your value is not empty.

$_POST variable is duplicating

I do not even know how to google this one...imagine it is something stupid...but any help would be great...
passing a variable when submitting a form...when echo the $_POST it is good...but when i put it into a php variable it is duplicated
<?
//list transactions by month
if ($_POST['m']=="yes"){
$table = $_POST['month'];
$_SESSION['table']=$_POST['month'];
$conn = mysql_connect("localhost", "mss_records", "3205") or die(mysql_error());
mysql_select_db('store_records', $conn) or die(mysql_error());
$result = mysql_query("SELECT * FROM $table");
while($row = mysql_fetch_array($result))
{
$id=$row['transaction'];
$date=$row['date'];
$time=$row['time'];
$paid=$row['payment'];
$total=$row['total'];
echo '<style type="text/css">
<!--
.list {
font-family: Georgia, "Times New Roman", Times, serif;
font-size: 12px;
color: #000;
padding: 2px;
border: 2px solid #009;
}
.view {
width: 100px;
}
-->
</style>
<div class="list">
<p><span style="color: #900">Transaction #</span>'.$id.'
<span style="color: #900">Date:</span>'.$date.'
<span style="color: #900">Time:</span>'.$time.'<span style="color: #900">
Paid By:</span>'.$paid.' <span style="color: #900">Total:</span>'
.number_format($total, 2).'
<form name="form1" method="post" action="find.php">
<label>
<input type="submit" name="view" id="view" value="'.$id.'">
</label>
</form>
</p>
</div>
<p></p>';
}
}
//view transaction after viewing by month
if (isset($_POST['view'])){
$conn = mysql_connect("localhost", "mss_records", "3205") or die(mysql_error());
mysql_select_db('store_records', $conn) or die(mysql_error());
$table = $_SESSION['table'];
echo "this is the number ".$_POST['view'];
$post=$_POST['view'];
echo "this is the post ".$post;
$result = mysql_query("SELECT * FROM $table WHERE transaction = '$post'")
or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$items=$row['transaction'];
}
echo $items;
}
?>
after the user goes through the first selection and on the second window the output is...
this is the number 46this is the $post 4646
Your query is mysql_query("SELECT * FROM $table WHERE transaction = '$post'"). Therefore the value of $items=$row['transaction']; is also going to be 46. When you echo out everything without line breaks, it smashes everything together.
POST is not duplicating anything, you are just echoing $items directly after it.
Try this:
$table = $_SESSION['table'];
echo "this is the number ".$_POST['view']."<br /> \n";
$post=$_POST['view'];
echo "this is the post ".$post."<br /> \n";
$result = mysql_query("SELECT * FROM $table WHERE transaction = '$post'")
or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$items=$row['transaction'];
}
echo $items;
}

Categories