Listing mysqli table entries in html using php - php

I'm a beginner and I can't figure out for the life of me how to list products from a mysqli table on a webpage using php. Currently I just manage to get the first row to repeat for the number of rows that exist in the table using the following code:
<?php
$p_sql = "SELECT * FROM products";
$p_query = mysqli_query($db_conx, $p_sql);
$productData = mysqli_fetch_array($p_query, MYSQL_ASSOC);
$num = mysqli_num_rows($p_query);
?>
<!DOCTYPE html>
<?php print "There are currently $num rows in the table<P>";
for ($row=0; $row<$num; $row++){
$name = $productData["product_name"];
echo "$name <br>";
};
?>
I know within my for loop I don't include $row but I don't know how to properly include it. Your help would be much appreciated!

Instead of using your for() loop, use a while() loop with your $productData = mysqli_fetch_array($p_query, MYSQL_ASSOC)
<?php
$p_sql = "SELECT * FROM products";
$p_query = mysqli_query($db_conx, $p_sql);
$num = mysqli_num_rows($p_query);
?>
<!DOCTYPE html>
<?php print "There are currently $num rows in the table<P>";
while($productData = mysqli_fetch_array($p_query, MYSQL_ASSOC)){
echo $productData["product_name"]."<br />";
};
?>

Related

How can I add an Sql Query in the middle of HTML code?

I want to make a login system, where the user can see all the messages he has received. In my database the insurance_company is the username and that is what I want it to show by. Here is my code. How do I fix it?
- Thanks Prem
<?php
$strSQL = "SELECT * FROM claims WHERE insurance_company = $_SESSION['user']";
$rs = mysql_query($strSQL);
while($row = mysql_fetch_array($rs)) {
echo $row['Claim'] . "<br />";
}
?>
PHP code can be written anywhere in HTML code. You need to just place your PHP code between tags. As you can see in the example bellow I have used php code between p tags.
<?php
$strSQL = "SELECT * FROM claims WHERE insurance_company = $_SESSION['user']";
$rs = mysql_query($strSQL);
while($row = mysql_fetch_array($rs)) {?>
<p> <?php echo $row['Claim'] . "<br />"; ?> <p>
<?php }
?>
If you want it to show insurance_company, you must echo this value in the response array:
echo $row['insurance_company']

Using PHP while loop on a dynamic page won't work

I am trying to show it all with while loop on a dynamic page, but it wont show anything at all..
if (isset($_GET['id'])) {
$genre = $_GET['id'];
$sql = "SELECT * FROM movstream_genre WHERE ID = '$genre'";
$result = $db->query($sql);
$row = $result->fetch_assoc();
}else{
$sql = "SELECT ID, genre FROM movstream_genre";
$result = $db->query($sql);
}
<html>
<body>
<ul>
<?php while ( $row = $result->fetch_assoc() ): ?>
<li><?=$row['genre'];?></li>
<?php endwhile; ?>
</ul>
</body>
</html>
Anyone know why it wont show anything on the dynamic page, but if the page is not dynamic it works fine :)
Thanks.
If dynamic page means that your sending an id in Url an you are not getting result.
I think that the problem is that you are using $row = $result->fetch_assoc(); in if condition and also in while.
Please use fetching in one place. Better be in while loop.
Hope it helps.
I figured it out
if (isset($_GET['id'])) {
$genre = $_GET['id'];
$sql = "SELECT ID, genre FROM movstream_genre";
$result = $db->query($sql);
}
This it how it needs to look when its a dynamic page :)
well i think it is, it works now.
please echo the result ...
<li><?php echo $row['genre'];?></li>
And in href, it is only genre OR genre.php
In the first query..
$sql = "SELECT * FROM movstream_genre WHERE ID = '$genre'";
$result = $db->query($sql);
$row = $result->fetch_assoc();// this line not required
comment this line
$row = $result->fetch_assoc();
It looks like you haven't enclosed the first part of your PHP code in <?php ?> tags

mysql query within a mysql query

I'm trying to display information from a table in my database in a loop, but for certain information, I'm referencing other tables. When I try to get data from other tables, any data following will disappear. here is the code I am using:
`
//Below is the SQL query
$listing = mysql_query("SELECT * FROM Musicians");
//This is displaying the results of the SQL query
while($row = mysql_fetch_array($listing))
{
?>
...html here...
<? echo $row['name']; ?>
<? echo $row['Town']; ?>
<?
$CountyRef = $row['CountyId'];
$county = mysql_query("SELECT * FROM County WHERE CouInt='$CountyRef'");
while($row = mysql_fetch_array($county))
{
echo $row['CouName'];
}
?>
<?php echo $row['instrument']; ?>
<?php echo $row['style']; ?>`
My problem is that everything after the second while loop is not displaying. Anyone have any suggestions?
Thanks
Second loop should say $row2. $row is being overwritten. Both variables should be named different from each other.
You can acomplish that with a one single query:
SELECT *,
(SELECT CouName FROM County WHERE CouInt=mus.CountyId) as Country
FROM Musicians mus;
You final code should looks like:
<?php
$listing = mysql_query("SELECT *,
(SELECT CouName FROM County WHERE CouInt=mus.CountyId) as Country
FROM Musicians mus;");
//This is displaying the results of the SQL query
while($row = mysql_fetch_assoc($listing))
{
echo $row['name'];
echo $row['Town'];
echo $row['Country']; //Thats all folks xD
echo $row['instrument'];
echo $row['style'];
} ?>
Saludos ;)
And that?:
while($row2 = mysql_fetch_array($county)) {
echo $row2['CouName'];
}

error in storing data to mysql however echo gives correct output

in my tableX some datas are there which looks like this
<h1>ghhhhhh!</h1>
http://twitter.com/USERNAME
<h1></h1>
http://3.bp.blogspot.com/_fqPQy3jcOwE/TJhikN8s5lI/AAAAAAAABL0/3Pbb3EAeo0k/s1600/Srishti+Rai1.html
<h1></h1>
http://4.bp.blogspot.com/_fqPQy3jcOwE/TJhiXGx1RII/AAAAAAAABLc/XNp_y51apks/s1600/anus7.html
<h1></h1>
http://cyz.com/_fqPQy3jcOwE/TJhh1ILX47I/AAAAAAAABKk/gX-OKEXtLFs/s1600/4r-2.html
<h1></h1>
http://cyz.com/_fqPQy3jcOwE/TJhiHGgb-KI/AAAAAAAABK8/zEv_41YzMhY/s1600/19+(1).html
<h1></h1>
http://cyz.com/_fqPQy3jcOwE/TJhihkpZZKI/AAAAAAAABLs/zDnlZkerBd8/s1600/Pooja+Gurung.html
when i echo the same php code it gives correct output but when i am storing these details in mysql only one row is getting stored in mysql row.
my code is this
<?php
include('connect.php');
$idmg=$_POST["id"];
$res =mysql_query("select * from tablea");
$row = mysql_fetch_array($res);
if(sus== '0'){
$output=''.$row['content'].'';
echo $output;//this output gives the above result but when i store in db it stores first row
mysql_query ("INSERT INTO tablea (content) VALUES ('$output')");
} ?>
Your insert is failing because you have unescaped data in $output. Take DCoder's advice above and use PDO or mysqli.
Is there a reason you have sql_fetch_array() and not mysql_fetch_array() ?
You also need to iterate through your results if you want more than one row.
<?php
include('connect.php');
$idmg =$_POST["id"];
$res =mysql_query("select * from tablea");
$row = sql_fetch_array($res)
if($sus== '0'){
$output=mysql_real_escape_string($row['content']);
echo $output;
mysql_query ("INSERT INTO tablea (content) VALUES ('$output')");
}
?>
And as #DCoder said, you should be using prepared statements, the code you have now is vulnerable to SQL injection.
Your code some-what corrected:
<?php
include('connect.php');
$idmg=$_POST["id"];
$res = mysql_query("select * from tablea");
$row = mysql_fetch_array($res);
if($sus== '0'){ // what is sus? If variable.. should be $sus
$output = $row['content']; // .'' is literally nothing..
echo $output;
mysql_query ("INSERT INTO tablea (content) VALUES ('$output')");
}
?>
What I think you are trying to do:
<?php
include('connect.php');
$idmg = $_POST["id"]; // not actually used
$res = mysql_query('SELECT * FROM tablea');
while($row = mysql_fetch_array($res)) {
$output = $row['content'];
echo $output;
// do anything else you want.. in your case ?enter the data back in?
mysql_query("INSERT INTO tablea(content) VALUES('$output')");
}
?>
What you should be using:
<?php
$idmg = $_POST['id']; // <-- not actually used
$res = $mysqli_connection->query('SELECT * FROM tablea');
while($row = $res->fetch_array(MYSQLI_ASSOC)) {
$output = mysqli_connection->real_escape_string($row['content']);
echo $output;
// Do whatever else you like
$mysqli_connection->query("INSERT INTO tablea(content) VALUES('$output')");
}
$res->free();
$mysqli_connection->close();
?>

Problem populating a dropdown box with MySQL query results (PHP/MySQL)

so as the title states, using the following code I have got it populating the dropbox with a single result from the query, that result being the latest added in the table.
here is my code:
<?php
$query = "SELECT * FROM units_tb WHERE user_id='$userid'";
$result = mysql_query($query) or die (mysql_error());
while($row = mysql_fetch_assoc($result)){
$aa = "<option value='{$row['unit_id']}'>{$row['unit_code']}</option>";
}
?>
<select name="t_unit"><? echo $aa; ?></select>
The odd thing is, I use this same code for another field, and it works, populating the dropdown with all the results, however in this case it only fills in the last unit code in the table and not all of which are attached to the particular user id.
I would appreciate anyones thoughts :D
thanks
$aa .= "<option value='{$row['unit_id']}'>{$row['unit_code']}</option>";
add . before = and initiate $aa = ''; before while loop
<?php
$query = "SELECT * FROM units_tb WHERE user_id='$userid'";
$result = mysql_query($query) or die (mysql_error());
$options = "";
while($row = mysql_fetch_assoc($result)){
$options .= "<option value='{$row['unit_id']}'>{$row['unit_code']}</option>";
}
?>
<select name="t_unit"><? echo $options; ?></select>
should work. You forgot a . in your while loop

Categories