why do I need to query twice here? Why can't I just query the one below, change it to 'SELECT * FROM ...' and use it query for the rest of the script? When I try to do that, the second half part of my script won't recognize the query from the beginning, and I have to query again.
$getImages = 'SELECT image_id, image_name FROM images';
<select name="image_id">
<?php while ($row = $images->fetch_assoc()) { ?>
<option value="<?= $row['image_id']; ?>"
<?php if (isset($_GET['image_id']) && $_GET['image_id'] == $row['image_id']) {
echo 'selected';
} ?>
><?= $row['image_name']; ?></option>
<?php } ?>
</select>
$sql = "SELECT image_name, caption FROM images WHERE image_id = $image_id";
$result = $conn->query($sql);
if ($result->num_rows) {
$row = $result->fetch_assoc();
?>
<figure><img src="images/<?= $row['image_name']; ?>.jpg" width=600px height=auto>
<figcaption><?= $row['caption']; ?></figcaption>
</figure>
<?php } else { ?>
<p>Image not found</p>
<?php } ?>
thank you :)
The first loop exhausts all the results of the query. You need to either re-execute the query or rewind the result resource back to the first record.
Far far far easier to pull your object to an array, then iterate through the array so that you aren't getting stuck with exhausting our object rows.
Related
I have a PHP function that returns a single row from a localhost MySQL table like so:
<?php
//include database connection process
require_once("includes/conn.inc.php");
//prepare statement
$stmt = $conn->prepare("Select * FROM races WHERE raceID = ?");
$id = 1;
$stmt->bind_param("i", $id);
$stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_assoc();
?>
What I would like to do with this array is output the data from the individual fields in their own HTML <div> or <p> with a heading indicating what they mean in a separate div. I currently user the print_row method that outputs everything in one. All the data I want is there, but I'd like it separated out into name/value paragraphs or divs.
//what I currently have
<?php
print_r($row);
?>
Is there a way to do this?
Thanks in advance,
Mark
I didn't understand the question very well but I think I understand what you need.
Use while to iterate trough each row.
while($row = $resultDesc->fetch_assoc())
{
echo '<p><strong>Description:</strong></p> ';
echo '<p>'. $row['description'] . '</p>';
}
That's not the exact solution but atleast shows you the path.
You can use foreach
<?php foreach ($row as $key => $val): ?>
<p><strong><?php echo $key; ?>:</strong></p>
<p>
<?php
//output relevant attribute
//of query run on page load
echo $val;
?>
</p>
<?php endforeach; ?>
I want to do the next thing but I don't know how to do this, I'll try to explain me
I have an field generated by php code like this (Works)
<select id="profiles_select" name="profiles_select">
<?php
do {
?>
<option value="<?php echo strtoupper($system['profile']);?>">
<?php echo strtoupper($system['profile']);?></option>
<?php
} while($system = mysql_fetch_assoc($r)); //the "$r" it's the query
$rows = mysql_num_rows($r);
if($rows > 0) {
mysql_data_seek($r, 0);
$systemas = mysql_fetch_assoc($r);
}
?>
</select>
The query
<?php
$q="SELECT DISTINCT profile FROM sys_profiles";
$r=mysql_query($q,$ConecLocal) or die(mysql_error());;
$systemas=mysql_fetch_assoc($r);
$tsys=mysql_num_rows($r);
?>
What I need?
I need generate another similar to first generated by php code but, this time I need made a Query including the value of the first , something like this:
<?php
$value_select=$_GET['profiles_select'];
$q2="SELECT DISTINCT systems FROM sys_profiles where profile=$value_select";
$r2=mysql_query($q,$ConecLocal) or die(mysql_error());;
$profiles2=mysql_fetch_assoc($r);
$tsys=mysql_num_rows($r);
?>
Next of the query I need show in the another the query result, something similar to the first select (generated by php), but do the query when the first of the it's selected
<select id="systems_select" name="system_select">
<?php
do {
?>
<option value="<?php echo strtoupper($system['systems']);?>">
<?php echo strtoupper($profiles2['systems']);?></option>
<?php
} while($profiles2 = mysql_fetch_assoc($r2)); //the "$r2" it's the another query
$rows2 = mysql_num_rows($r2);
if($rows2 > 0) {
mysql_data_seek($r2, 0);
$systemas = mysql_fetch_assoc($r2);
}
?>
</select>
Thanks for the help.
i have create an array to diusplay a random file name from my mysql database. unfortunatly it doesnt show correctly.
i need the explode to work based on file id to show the correct banner picturse for that tv series.
<?php include '../connect/dbseries.php' ?>
<?php include 'Sbarray.php' ?>
<?php
$names = explode ("|", $row['4']);
?>
<center><?php
while($row=mysql_fetch_array($result2)){
echo '<img src="../images/series/'. $names[array_rand($names,1)].'" width="800" height="150" style="padding:2px;">';
}
?>
</center>
my array page is
<?php $result2 = mysql_query("SELECT
ID,
pretty_name,
sortname,
Genre,
Bannerfilenames,
CurrentBannerFilename,
Posterfilenames,
PosterBannerFileName,
summary,
Fanart,
IMDB_ID
FROM online_series
order by sortname ASC;");
if (!$result2) {
echo 'Could not run query: ' . mysql_error();
exit;
}
?>
<?php $row = mysql_fetch_row($result2);
//setup array
$banner = $row['4'];
?>
that is all the code i have on the page. any help would be appreciated as it only shows images from row 1 instead of for each row/ tv series
i asume it has something to do with the explode command but cant figure out how to correct it.
thanks in advance
you can check with print_r($names) whether it works. If I understand your problem correctly, you want a random pic of each row. Now you do the variable names before you iterating through the results - so it uses always the first:
<?php $result2 = mysql_query("SELECT
ID,
pretty_name,
sortname,
Genre,
Bannerfilenames,
CurrentBannerFilename,
Posterfilenames,
PosterBannerFileName,
summary,
Fanart,
IMDB_ID
FROM online_series
order by sortname ASC;");
if (!$result2) {
echo 'Could not run query: ' . mysql_error();
exit;
}
?>
<center><?php
while($row=mysql_fetch_assoc($result2)){
$names = explode ("|", $row['Bannerfilenames']);
//for check whether explode works
print_r($names);
echo '<img src="../images/series/'. $names[array_rand($names,1)].'" width="800" height="150" style="padding:2px;">';
}
?>
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'];
}
In my Details table, when the complete field is '0' I want a certain image to appear when it is '1' I want a different image to appear. At the moment only the second image is showing (when complete field is 1)
This is the code I am using:
$id = $_SESSION['user_id'];
$isFinished= mysql_query("SELECT complete From details where user_id = $id") ?>
<p> <img src='<?php if($isFinished) echo "correct.png"; else echo "incorrect.png"; ?>' /><a href="details.php">Details</p>
Any ideas would be great!
The WHERE clause of your query is specifying only select records where complete = '1'
So you'll only get 1 back in the results.
You should remove that from the WHERE clause completely and let your php IF statement decide whether complete = '1'
the function
function getOne($query){
$res = mysql_query($query);
if (!$res) {
trigger_error("db: ".mysql_error()." in ".$query);
return FALSE;
}
if ($row = mysql_fetch_row($res)) {
return $row[0];
}
}
the code
$sql = "SELECT complete From details where user_id = ".intval($_SESSION['user_id']);
$isFinished = getOne($sql);
?>
<p>
<img src='<?php if($isFinished): ?>correct.png<? else: ?>incorrect.png<? endif ?>' />
Details
</p>
Your SQL statement only selects entries with complete='1'.
You should remove the "and complete='1'" from your statement.
Edite:
ALso as mentioned above your if statement only check if your query returned something or got an error.
It should be
if($isFinished['complete'] == '1') {echo "correct.png";}else{echo "correcy.png";}
It should be something like this:
$id = $_SESSION['user_id'];
$result= mysql_query("SELECT complete From details where user_id = $id and complete='1'") ?>
$row = mysql_fetch_array( $result );
<p>
<?php if($row['complete']): ?>
<img src="correct.png"/>
<?php else: ?>
<img src="incorrect.png"/>
<?php endif; ?>
<a href="details.php">
Details</a>
</p>
The mysql_query function returns false only when the query is invalid. It does not return false if there are zero rows.
You should use the mysql_num_rows function to determine if there was a row or alternatively use the mysql_fetch_* functions to fetch the value of complete.
Example 1 (Similar to your original (unedited) question):
$result = mysql_query("SELECT 1 From details where user_id = $id where complete = 1");
$isFinished = mysql_num_rows($result);
Example 2 (Alternative):
$result = mysql_query("SELECT complete From details where user_id = $id");
$record = mysql_fetch_assoc($result);
$isFinished = $record['complete'];
Is this the whole code?
In this case $isFinished is not the content retreived from db: mysql_query returns false if query is wrong otherwise an object where you can fetch the results.
In this case if($isFinished) is always true as the query is correct!
you miss the fetch part ater query execution!
http://php.net/manual/en/function.mysql-query.php
For example:
$id = $_SESSION['user_id'];
$result = mysql_query("SELECT complete From details where user_id = $id");
$isFinished= mysql_num_rows($result);
<p> <img src='<?php if($isFinished) echo "correct.png"; else echo "incorrect.png"; ?>' /><a href="details.php">Details</p>
This way if query returns one ore more rows then $isFinished is 1 or more otherwise $isFinished is 0. Your if - else should then work properly
Note i did not change the your original SQL, change it if you need.