SQL Query only retrieving one row - php

I have a a php website with some code on it to pull from a database after the user has defined some search terms and then show them a table with all their information in it
The problem is even when i do a select * from the tables, i am only getting the first row back.
Code:
$result = mysql_query("SELECT trees.*
FROM trees
INNER JOIN price
ON trees.ID=price.treeid
");
$num_rows = mysql_num_rows($result);
if($num_rows == 0) {
echo "No rows retrieved";
} else {
echo $num_rows;
}
i have 2 rows in my database:
Spruce El Sorbeous Sprucious Green Green Green 100 200
its a tree ma! true NULL NULL NULL
Mayday el daymay red green white 10 4000000
GOING DOWN true Grey true false
when i print out the $num_rows up there, it is only one.
When i print out my table below, there is only one row:
echo '<table border = 0 cellpadding=0 >';
echo '<tr>';
echo '<td><b><u>Name</b></u></td>
<td><b><u>Latin Name</b></u></td>
<td><b><u>Primary Color</b></u></td>
<td><b><u>Secondary Color</b></u></td>
<td><b><u>Fall Color</b></u></td>
<td><b><u>Trunk Color</b></u></td>';
echo '<td><b><u>Description</b></u></td>
<td><b><u>Height</b></u></td>
<td><b><u>Spread</b></u></td>
<td><b><u>Drought Resistant?</b></u></td>
<td><b><u>Flowering?</b></u></td>
<td><b><u>Fruit Producing?</b></u></td>';
echo '</tr>';
while ($row = mysql_fetch_assoc($result)) {
echo '<tr>';
echo '<td>';
echo $row['name'];
echo '</td>';
echo '<td>';
echo $row['latinname'];
echo '</td>';
echo '<td>';
echo $row['primarycolor'];
echo '</td>';
echo '<td>';
echo $row['secondarycolor'];
echo '</td>';
echo '<td>';
echo $row['fallcolor'];
echo '</td>';
echo '<td>';
echo $row['trunkcolor'];
echo '</td>';
echo '<td>';
echo $row['description'];
echo '</td>';
echo '<td>';
echo $row['height'];
echo '</td>';
echo '<td>';
echo $row['spread'];
echo '</td>';
echo '<td>';
echo $row['droughtresistant'];
echo '</td>';
echo '<td>';
echo $row['flowering'];
echo '</td>';
echo '<td>';
echo $row['fruitproducing'];
echo '</td>';
echo '</tr>';
}
echo '<table>';

INNER JOIN will only return values that have NOT NULL values in both tables. You are probably joining stuff with a NULL value. Use a LEFT or a RIGHT join instead!

Since there's no example of the data in the PRICE table, I'm guessing that only one row was joined.
The question remains, why are you doing that JOIN? You're not collecting any data from the PRICE table, so what's the point.
BTW, where in the data is the ID?

Related

select table from PHP MyAdmin using PHP file

Good day,
I am trying to select a table from MySQL and generaly I use this code:
$sql="CALL selectCreatedTableByName('".$tableNameIn."')";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table id='restable'>";
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['nameE'] . "</td>";
echo "<td>" . $row['nameN'] . "</td>";
echo "</tr>";
}
echo "</table>";
}
$conn->close();
but now, when I don't know the names of the table's columns, how can I select this table?
Thank you so much.
You are not selecting the table, you are selecting from the table. (at least I'm guessing that is the case since we don't know what your procedure actually does).
The PHP array returned uses the attribute names from the resultset as keys hence....
while($row = $result->fetch_assoc()) {
echo "<tr>\n";
foreach($row as $name=>$value) {
echo "<td>$value</td>\n";
}
echo "</tr>\n";
}
If you want a header row, then use a state variable to flag the first row.
$fetched=0;
while($row = $result->fetch_assoc()) {
if (!fetched) {
echo "<tr>\n";
foreach($row as $name=>$value) {
echo "<th>$name</th>\n";
}
echo "</tr>\n";
}
$fetched++;
echo "<tr>\n";
foreach($row as $name=>$value) {
echo "<td>$value</td>\n";
}
echo "</tr>\n";
}

Why php can't print data from mysql database?

PHP failed to print data of two columns of a mysql tableļ¼Œthe result only shows the first row of data which proved to be right,with the rest rows show
" Undefined offset".
$query1='select MNO from mima01 order by MNO;';
$query2='select MTYPE from mima01 order by MNO;';
$result1=mysqli_query($conn,$query1);
$result2=mysqli_query($conn,$query2);
$username=mysqli_fetch_array($result1);
$usertype=mysqli_fetch_array($result2);
$count='select count(*) from mima01;';
$usercount=mysqli_query($conn,$count);
$usernum=mysqli_fetch_array($usercount);
for($i=0;$i<$usernum[0];$i++) {
echo '<tr><td style="text-align:center">' ;
echo $username[$i];
echo '</td>';
echo '<td style="text-align:center">' ;
echo $usertype[$i];
echo '</td></tr>';
}
I've rewritten your code to the normal way you would walk through a query result:
$query = 'select MNO, MTYPE from mima01 order by MNO;';
$result = mysqli_query($conn, $query);
while ($data = mysqli_fetch_array($result)) {
echo '<tr><td style="text-align:center">' .
$data['MNO'] .
'</td>' .
'<td style="text-align:center">' .
$data['MTYPE'] .
'</td></tr>';
}
Please note that I have no way to run this code so I can't be a 100% sure it will work.
from what I understand I think your solution is:
$query='SELECT MNO,MTYPE FROM mima01 ORDER BY MNO DECS;'; //use DECS|ASC
$result=mysqli_query($conn,$query);
$username=mysqli_fetch_array($result);
// to count the rows
$count=mysqli_num_rows($result);;
echo $count;
// to display data
while($row=mysqli_fetch_array($result) {
echo '<tr><td style="text-align:center">';
echo $row['MNO'];
echo '</td>';
echo '<td style="text-align:center">';
echo $row['MTYPE'];
echo '</td></tr>';
}
Hope it was helpful

php mysql select data to gridview

when select data from database using php-mysql and display in html table it shows as (A).
but i want to display it as (b)
help me...
Sample (and very simple) code would be:
// assuming the PDO connection is established and kept in $db
$st = $db->prepare("SELECT Date, Num, Value1, Value2 FROM the_table");
if ($st->execute()) {
$previousDate = NULL;
$previousNum = NULL;
while ($row = $st->fetch()) {
echo '<tr>';
echo '<td>';
if ($previousDate != $row['Date']) {
echo $row['Date'];
$previousDate = $row['Date'];
}
echo '</td>';
echo '<td>';
if ($previousNum != $row['Num']) {
echo $row['Num'];
$previousNum = $row['Num'];
}
echo '</td>';
echo '<td>'. $row['Value1'] .'</td>';
echo '<td>'. $row['Value2'] .'</td>';
echo '</tr>';
}
}

How to fetch all rows of sql query from MySQL database into array in php

I am writing an application to build a chart using the google chart API.
The data is dynamic, and resides in a MySQL database.
The issue being faced is that the array that contains the datasource to be displayed only contains the first row returned by the query. Even after looping.
This is the code:
include 'connect.php';
$sql="SELECT result,COUNT(result) value
FROM test
WHERE result LIKE 'GY%'
GROUP BY result";
$result=mysqli_query($link,$sql);
$myurl[]="['Option','Value']";
while($row=mysqli_fetch_assoc($result))
{
$result=$row['result'];
$value=$row['value'];
$Title="My Results";
$myurl[]="['".$result."',".$value."]";
}
<script type="text/javascript">
google.load('visualization', '1', {'packages':['corechart']});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = new google.visualization.arrayToDataTable([
<?php
echo (implode(",",$myurl));?>
]);
var options={
title:'<?php echo($Title);?> '
};
Only the first row being returned is used in the chart,therefore it shows that value as 100%. How can i ensure ALL the row values are used?
Modify the query like this
$sql="SELECT result value
FROM test
WHERE result LIKE 'GY%'
GROUP BY result";
$result=mysqli_query($link,$sql);
And the number of results you can access like below.
mysqli_num_rows($result)
If you want an associative array, use this:
$query = "SELECT column FROM `table` WHERE id='" . $id . "'"; / Whatever your query is
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
You can then call $row and the column name from the row with a numbered id that is $id by using $row['columnname']; I'm also pretty sure that you can use print_r($row['columnname']); without including the WHERE id='" . $id . "'"; in the query and instead just having $query = "SELECT column FROMtable; and it will print out all of the data (every row) in that column.
Also, if you're just looking to draw data to a chart, using a google API could over complicate things, as you could just output the data to an HTML table with this code:
<?php
$query = "SELECT FROM `table`";
$result = mysql_query($query);
echo '<table class="table">';
echo '<tr class="table">';
echo '<th class="table">Column</th>';
echo '</tr>';
while($row = mysql_fetch_assoc($result)
{
echo '<tr class="table">';
echo '<th class="table">' . $row['column'] . '</th>';
echo '</tr>';
}
echo '</table>';
?>
Add any other things you need using echo in php then the html code. You can also put buttons in the table, and pretty much anything else. This site should give any more info you would need on HTML tables: http://www.w3schools.com/html/html_tables.asp
Here's another little example I made of an HTML form echoed in php. It has buttons, etc.
$query = "SELECT * FROM `users` WHERE status='accepted'";
$result = mysql_query($query) or die(mysql_error());
echo '<table class="table">';
echo '<h2>Accounts</h2>';
echo '<tr class="table">';
echo '<th class="table">Name</th>';
echo '<th class="table">Email</th>';
echo '<th class="table">Username</th>';
echo '<th class="table">Date Created</th>';
echo '<th class="table">Decline</th>';
echo '<th class="table">Accept</th>';
echo '<th class="table">Give Admin</th>';
echo '</tr>';
while($row = mysql_fetch_assoc($result))
{
echo '<tr class="table">';
echo '<th class="table">' . $row['name'] . '</th>';
echo '<th class="table">' . $row['email'] . '</th>';
echo '<th class="table">' . $row['username'] . '</th>';
echo '<th class="table">' . $row['trn_date'] . '</th>';
echo '<form method="post">';
echo '<th class="table"><input type="submit" name="decline_' .$row["id"]. '" value="decline" ></input></th>';
echo '<th class="table"><input type="submit" name="accept_' .$row["id"]. '" value="accept" ></input></th>';
echo '<th class="table"><input type="submit" name="giveadmin_' .$row["id"]. '" value="accept/give admin" ></input></th>';
echo '</form>';
echo '</tr>';
}
echo '</table>';
Hope that helped:)

Creating grid rather than a table

For the table below, rather than paginate, I would like to create a grid of results.
So I would like to echo out a set of 10 results, then continue the process 200 pixels to the right. Then after 10 more results, continue 200 pixels to the right of that.
How can I do this?
$query2 = "SELECT street1, city, state, zip, phone, website
FROM addresses
WHERE submissionid=$submissionid
ORDER BY street1 DESC";
$result2 = mysql_query($query2);
$arr2 = array();
echo "<table class=\"commentecho3\">";
while ($row2 = mysql_fetch_array($result2)) {
echo '<tr>';
echo '<td>'.strtoupper($row2["street1"]).'</td>';
echo '</tr>';
echo '<tr>';
echo '<td>'.strtoupper($row2["city"]).', '.strtoupper($row2["state"]).' '.strtoupper($row2["zip"]).'</td>';
echo '</tr>';
echo '<tr>';
echo '<td>'.strtoupper($row2["phone"]).'</td>';
echo '</tr>';
echo '<tr>';
echo '<td>'.strtoupper($row2["website"]).'</td>';
echo '</tr>';
echo '<tr>';
echo '<td></td>';
echo '</tr>';
echo '<tr>';
echo '<td></td>';
echo '</tr>';
}
echo "</table>";
Is it really necessary to use tables there? Like Webgal suggested, could be really easy done using floated divs:
Create a class in css
.foo{float:left;padding-right:200px;}
Update PHP (this can be done different ways):
$i=0;
echo('<div class="foo">');
while ($row2 = mysql_fetch_array($result2)) {
echo '<p>'.strtoupper($row2["street1"]).'</p>';
.................................
$i++;
if ($i=10){
echo('</div><div class="foo">');
}
}
echo('</div>');

Categories