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'];
}
Related
I'm using these codes.
$blog = mysql_query("SELECT * FROM blog ORDER BY id");
while($sutun = mysql_fetch_array($blog)) {
$fake = $sutun["date"];
echo "$fake";
}
When i use echo"$fake"; I can see all of my rows. But when I use <?php echo "$fake" ?> , It shows just 1 row for me.
I want to all of my rows while I'm using <?php echo "$fake" ?>.
Beacuse The echo"$fake"; in with in a loop it will echo at every iteration thats why you can see all your rows but <?php echo"$fake"; ?> executed when the loop is done so only the last row will be echoed;
You should seperate your logic like
<?php
$blog = mysql_query("SELECT * FROM blog ORDER BY id");
while($sutun = mysql_fetch_array($blog)) {
$fake = $sutun["date"];
?>
<?php
echo $fake;
}
I'm having a problem in this simple SQL/PHP query...
<?php
$course=$row['course'];
include('../db.php');
$cat=$row['cat'];
$result = mysql_query("SELECT * FROM question WHERE course='$course' AND cat='$cat'");
while($row = mysql_fetch_array($result))
{
echo $row['question'].'?<br>';
$qid=$row['qid'];
echo '<input type="hidden" name="qqqq[]" value="'.$qid.'" />';
echo '<select name="answer[]">';
echo '<option>Select Answer></option>';
$resultik = mysql_query("SELECT * FROM choices WHERE question='$qid' ORDER BY RAND() LIMIT 4");
while($rowik = mysql_fetch_array($resultik))
{
echo '<option>';
echo $rowik['opt'];
echo '</option>';
}
echo '</select><br><br>';
}
?>
Basically, this is a online examination. I want to display all the questions if the student will login. And the questions will be order/arrange according by their course. But eventually, there's no display at all. Not even a single letter will display.
Any help would be appreciated. Thank you so much.
In this there must some POST or GET values to get the course and cat which means
$course=$row['course'];
$cat=$row['cat'];
Since the $row is empty this is the case it will not display anything. Check with isset() like following
$course = isset($row['course']) ? $row['course'] : 'COURSE';
$cat = isset($row['cat']) ? $row['cat'] : 'CAT';
The included file include('../db.php'); please check the database connectivity has established or not?.
I want to do a a query to a mysql database that returns multiple rows and columns. I then want to assign the results to a variable and echo them out later in the page. However, the method I am using is long, tedious, and for this project impractical. here is what I am doing.
$result = mysql_query("SELECT * FROM people WHERE open_or_closed !='Closed' ORDER BY
number",$c) or die("two");
$number=mysql_num_rows($result);
if($mynumber>0){
$data= mysql_fetch_array($result,MYSQL_ASSOC);
$full_name1=mysql_result($result,0, 'full_name');
$phone_number1=mysql_result($result,0, 'phone_number');
$one=1;
}
if($mynumber>1){
$full_name2=mysql_result($result,1, 'full_name');
$phone_number2=mysql_result($result,1, 'phone_number');
$two=2;
}
Later when I want to echo it, I will not know if there is a record there or not, so I will have to
<?php if($one==1){echo '<div id="blackline"></div>';}?>
<div id="titletext"><?php echo $full_name1; ?></div><br />
<div id="datetext"><?php echo $phone_number1; ?></div>
Try this
$result = mysql_query("SELECT * FROM people WHERE open_or_closed !='Closed' ORDER BY
number",$c) or die("two");
$number=mysql_num_rows($result);
if($number>0)
{
$i=0;
while($row_result = mysql_fetch_array($result))
{
$full_name[$i][] = $row_result['full_name'];
$phone_number[$i][] = $row_result['phone_number'];
$i++;
}
}
Hey working on a small project but I seem to be having a problem with a specific part of my code.
The first part works beautifully and displays products within a product category.
http://mkiddr.com/phptests/shopping/category.php?id=2
However the issue seems to be having the category description to show, which is derived from a separate query into its own array. I have used
echo(mysqli_num_rows($result2));
This seems to count the correct amounts of rows from the query, indicating the SQL is working perfectly.
I would appreciate help on this I am a complete needbe and I have patched and edited this code to my needs (supplied by university). P.S I am aware there are vulnerabilities within security.
<?php
session_start();
include "conn.php";
include "header.php";
if (isset($_GET['id'])){
$CategoryID = $_GET['id'];
$q="SELECT ProductID, ProductName FROM Products WHERE CategoryID=$CategoryID";
$d="SELECT `Desc` FROM ProductCategories WHERE CategoryID=$CategoryID";
$result = mysqli_query($_SESSION['conn'],$q);
$result2 = mysqli_query($_SESSION['conn'],$d) or die(mysql_error());
echo "<div>";
while ($row = mysqli_fetch_row($result)){
echo "<p><a href='product.php?id=".$row[0]."'>".$row[1]."</a></p>";
}
echo "</div>";
mysqli_free_result($result);
//Description
echo(mysqli_num_rows($result2)); //Test SQL
echo "<div>";
while ($myResult = mysqli_fetch_assoc($result2)){
echo "<p>".$myResult[0]."</p>";
}
echo "</div>";
}
include "footer.php";
?>
You're fetching wrong:
while ($myResult = mysqli_fetch_assoc($result2)){
^^^^^--- produces a non-numerically keyed array
You probably want
echo $myResult['name_of_field']
or
mysqli_fetch_row($result2)
^^^--returns a numerically keyed array.
instead.
I have a code that I have used over and over again before and now it's messing up. All I want to do is list information from the database into the table on the page, but now it will only show one result, instead of all the results it has found.
<table>
<tr><td style="background-color:#009745; color:#FFFFFF"><center><strong>Address Book</strong></center></td></tr>
<tr>
<?php
$getids = mysql_query("SELECT id, first_name, last_name FROM accounts WHERE s1='$id' ORDER BY id DESC", $db);
if (mysql_num_rows($getids) > 0) {
while ($gids = mysql_fetch_array($getids)) {
$ab_id = $gids['id'];
$ab_fn = $gids['first_name'];
$ab_ln = $gids['last_name'];
}
?>
<td><?= $ab_id ?> - <?= $ab_fn . " " . $ab_ln ?></td>
<?php
} else {
?>
<td><center>No Contacts</center></td>
<?php
}
?>
</tr>
</table>
please help me with this.
Thank You for your help :)
I love this site!! I can always get answers when I need them.
I saw two thing wrong
you are using mysql_fetch_array and later you are using string indexes to print the result
print the things in loop it is overriding values and just storing last row
if (mysql_num_rows($getids) > 0) {
while ($gids = mysql_fetch_assoc($getids)) {
$ab_id = $gids['id'];
$ab_fn = $gids['first_name'];
$ab_ln = $gids['last_name'];
echo '<td>'.$ab_id.' -'. $ab_fn.''.$ab_ln.' </td>';
}
In this messy code you're closing the while loop too early:
while ($gids = mysql_fetch_array($getids)) {
$ab_id = $gids['id'];
$ab_fn = $gids['first_name'];
$ab_ln = $gids['last_name'];
}
Only the last retrieved row is used later on. Also, don't use mysql_fetch_array if you're not accessing the numeric indeces of your result. Use mysql_fetch_assoc instead.