In my MySQL table I have a column name, and there are various repeating names within the table. If I want to display all other column formation for all the people with name chris I can use:
$sql = mysql_query("SELECT * FROM favorites WHERE name='$name'")
if($info = mysql_fetch_assoc($sql))
echo {$info['name']}
echo {$info['age']}
echo {$info['address']}
But then I want to echo the details of the second chris, in the table who could be in any random row within the table I can't specify that information. Is there a way to accomplish this? Thank you.
Try this:
while($row = mysql_fetch_assoc($sql)) {
echo $row['name'];
echo $info['age'];
echo $row['address'];
}
while ($row = mysql_fetch_assoc($sql))
{
echo $row['name'];
echo $row['age'];
echo $row['address'];
}
Related
I am just running a SQL query to fetch a particular data from the database and would like to display in horizontal way inspite of the vertical format, Can we do something to display the data as required.
For Getting the clear understanding of the question i have attached the Output which i am getting and the output which i required after the SQL query is executed.
Thanks in Advance.
Query:
<?php
include 'connect-db.php';
$query = "SELECT distinct work_component FROM daily_progress_demo WHERE package_no='$package_no'";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
echo "<center>".$row['work_component']."</center>";
echo "<br />";
}
?>
you can do like this
echo "<table>"
echo "<tr>"
while($row = mysql_fetch_array($result)){
echo "<td>".$row['work_component']."</td>";
}
echo "</tr>";
echo "</table>";
echo $row['work_component']."<br>";
I want to store result first in variables so I could use them but this wouldn't iterate anything. See I tried to store row's values in variables first and called them in td of table but it shows nothing.
Does it have a syntax error or something. What could be the method to store in variables first?
$query_test = "SELECT itemname, categoryname, manufacturername,
price, shopname,itemurl, itemimage, typename
FROM prices p,
items i,
shops s,
categories c,
manufacturers m,
types t,
modules mo
WHERE p.shopid=s.shopid
AND i.categoryid=c.categoryid
AND p.itemid=i.itemid
AND i.ManufacturerId=m.ManufacturerID
AND i.ModuleId= mo.ModuleID
AND i.TypeId=t.TypeID
AND i.categoryid=1004
AND s.shopid=5003";
$result = mysqli_query($conn, $query_test);
echo "<table border='1'>";
echo "<tr><td>"."Name"."</td>";
echo "<td>"."Category"."</td>";
echo "<td>"."Manufacturer"."</td>";
echo "<td>"."Price"."</td>";
echo "<td>"."Shop"."</td>";
echo "<td>"."Type"."</td>";
while ($row = mysqli_fetch_array($result, MYSQLI_BOTH)) {
//output a row here
$name = $row['itemname'];
$category = $row['categoryname'];
$manufacturer = $row['manufacturername'];
$price = $row['price']:
$shop = $row['shopname'];
$type = $row['typename'];
echo "<tr><td>".$name."</td>";
echo "<td>".$category."</td>";
echo "<td>".$manufacturer."</td>";
echo "<td>".$price."</td>";
echo "<td>".$shop."</td>";
echo "<td>".$type."</td></tr>";
/*echo "<tr><td>".($row['itemname'])."</td>";
echo "<td>".($row['categoryname'])."</td>";
echo "<td>".($row['manufacturername'])."</td>";
echo "<td>".($row['price'])."</td>";
echo "<td>".($row['shopname'])."</td>";
echo "<td>".($row['typename'])."</td></tr>";*/
}
echo "</table>";
Do a var_dump($row) in your while loop to see what values are stored in $row.
Also, given how you are accessing them, you should probably do:
while( $row = mysqli_fetch_assoc($result))
PHP and MySQL:What causes a query to exclude the first record in a table:
for example i have a script like this:
$query = "SELECT * FROM cars WHERE car_name = 'BMW'";
$results = mysql_query($query);
echo "<table border='1'>";
echo "<tr>";
echo "<th>Vehicle Name:<th>";
echo "</tr>";
while($row = mysql_fetch_array($result)){
$name = $row['car_name'];
echo "<tr>";
echo "<td>$name<td>";
echo "</tr>";
}
echo "</table>";
All rows are returned except the first one.Please help a brother out folks.
Not an answer, but too long for a comment:
Let's take a peak at your table cars.
What does
$qs = array(
array('total #rows', 'SELECT Count(*) FROM cars'),
array('#BMW', "SELECT Count(*) FROM cars WHERE car_name='BMW'"),
array('#LIKE BMW', "SELECT Count(*) FROM cars WHERE car_name LIKE '%BMW%'"),
array('#car_names', "SELECT Count(*) FROM (SELECT distinct car_name as foo FROM cars) as bar")
);
foreach( $qs as $query ) {
echo $query[0], "<br />\r\n";
$result = mysql_query($query[1]) or die(mysql_error());
while ( false!==($row=mysql_fetch_row($result)) ) {
echo ' ', $row[0], "\r\n";
}
}
print if placed in your script instead of your posted code?
The output should be something like
total #rows<br />
6
#BMW<br />
2
#LIKE BMW<br />
3
#car_names<br />
4
BTW: the mysql_* extension is deprecated,
see http://docs.php.net/manual/en/mysqlinfo.api.choosing.php
EDIT
If two or more columns of the result have the same field names, the last
column will take precedence. To access the other column(s) of the same name,
you must use the numeric index of the column or make an alias for the
column. For aliased columns, you cannot access the contents with the
original column name
You are using mysql_fetch_array and this is what it says in the documentation. I never use mysql* functions so I wouldn't have jumped to this type of conclusion quickly. Use mysql_fetch_assoc($results) and I'm 99% sure it will resolve your issue. The why this would be different is in the paragraph above from the documentation. I assume your first row is identical to at least 1 of the below rows. Which means it is very likely you're missing more than just the first one. May or may not be the case.
END EDIT
Add 4 things to your code.
echo "<tr>";
echo "<th>ID</th>"; // THIS
echo "<th>Vehicle Name:</th>"; // Add closing tags........
echo "</tr>";
echo mysql_num_rows($results); // THIS (compare this to your MYSQL output row count)
while($row = mysql_fetch_array($results)){ **THIS... you have $results set with query, but $result here*** make sure both are $results OR $result
$id = $row['YOUR_AI_ID']; // THIS
$name = $row['car_name'];
echo "<tr>";
echo "<td>$id</td>"; // THIS
echo "<td>$name</td>"; // Add closing tags.......
echo "</tr>";
}
Go go do now. Come back with results.
.hey guys how can i populate a dropdown list with the list of tables from a certain database?
$db = mysql_select_db('thepillar');
$sql = "SHOW TABLES";
$result = mysql_query($sql);
echo '<form method="post" id="try" action="pillar.php">';
echo 'Select Batch: ';
echo '<select name="batch" id="batch">';
echo '<option>';
while($r = mysql_fetch_assoc($result))
{
$tables = $r;
echo '<option>'.$tables.'</option>';
}
.i have tried the code above but the dropdown list is only filled with the word "Array" multiple times depending on how many tables are there in the database.
.help pls!
while($r = mysql_fetch_array($result))
{
echo $r[0]."<br />";
}
replace
$tables = $r;
with
$tables = $r['Tables_in_thepillar'];
also you got an extra echo '<option>';
above the loop
Your $tables variable is an associative array. You need to specify which index of the array you want to output between the <option> tags.
echo '<option>'.$tables['TABLE_NAME'].'</option>';
See the print_r output for what the index name is.
I am trying to loop though my users database to show each username in the table in their own row. I can only get it to show one user but loops through this the number of rows there are. Code is below
<?php
require_once ('../login/connection.php');
include ('functions.php');
$query = "SELECT * FROM users";
$results=mysql_query($query);
$row_count=mysql_num_rows($results);
$row_users = mysql_fetch_array($results);
echo "<table>";
for ($i=0; $i<$row_count; $i++)
{
echo "<table><tr><td>".($row_users['email'])."</td></tr>";
}
echo "</table>";
?>
Thanks
mysql_fetch_array fetches a single row - you typically use it in a while loop to eat all the rows in the result set, e.g.
echo "<table>";
while ($row_users = mysql_fetch_array($results)) {
//output a row here
echo "<tr><td>".($row_users['email'])."</td></tr>";
}
echo "</table>";
You're only fetching one row:
$row_users = mysql_fetch_array($results);
You're never calling a fetch again so you're not really looping through anything.
I'd suggest changing your loop to the following:
echo "<table>";
while ($row = mysql_fetch_array($results)) {
echo "<tr><td>".($row['email'])."</td></tr>";
}
echo "</table>";
The while loop will loop through the results and assign a row to $row, until you run out of rows. Plus no need to deal with getting the count of results at that point. This is the "usual" way to loop through results from a DB in php.
In the new MYSQLI, I use this coding
$query = "SELECT * FROM users";
$results=mysqli_query($con,$query);
$row_count=mysqli_num_rows($results);
echo "<table>";
while ($row = mysqli_fetch_array($results)) {
echo "<tr><td>".($row['id'])."</td></tr>";
}
echo "</table>";
mysqli_query($con,$query);
mysqli_close($con);
Your problem is in this line:
echo "<table><tr><td>".($row_users['email'])."</td></tr>";
You're echoing out a <table> tag again. Remove that so your code looks like this:
echo "<table>";
for ($i=0; $i<$row_count; $i++)
{
echo "<tr><td>".($row_users['email'])."</td></tr>";
}
echo "</table>";
You don't need to output a table within a table the way you're doing it.
$result = mysql_query("SELECT `email` FROM `users`");
$num_emails = mysql_num_rows($result);
echo "<table><caption>There are/is $num_emails email(s)</caption>";
while ($row = mysql_fetch_assoc($result)) {
echo "<tr><td>{$row['email']}</td></tr>";
}
echo '</table>';
Something like that should work.