The code should open all the rows of gamename column of games table and put 1700 rows into drop down menu, but it only displays a blank dropdown with 1700 rows.
// Connect to server and select database.
mysql_connect("$host", "$username", "$password") or die(mysql_error());
mysql_select_db("$db_name") or die(mysql_error());
$i=0;
$result = mysql_query("SELECT gamename FROM games");
$storeArray = Array();
echo '<select name="game" style="width: 400px">';
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$storeArray[] = $row[i];
echo "<option>".$storeArray[i]."</option>";
$i= $i+1;
}
?>
</select>
You should try it like this:
<?php
mysql_select_db("$db_name") or die(mysql_error());
$sql = "SELECT gamename FROM games";
$query = mysql_query($sql);
echo '<select name="game" style="width: 400px">';
while ($row = mysql_fetch_assoc($query)) {
echo '<option>'.$row['gamename'].'</option>';
}
echo '</select>';
?>
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$storeArray[] = $row[i];
echo "<option>".$storeArray[i]."</option>";
$i= $i+1;
}
For one thing, you're using i and $i interchangeably here; this may or may not cause an issue. You're assigning the ith member of $row into $storeArray, and that's not going to work after the first row, as there's only one item in your SELECT.
Why not just do:
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "<option>".$row['gamename']."</option>";
}
mysql_select_db("$db_name") or die(mysql_error());
$i=0;
$result = mysql_query("SELECT gamename FROM games");
$storeArray = Array();
echo '<select name="game" style="width: 400px">';
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$storeArray[] = $row['gamename'];
echo "<option>".$storeArray['gamename']."</option>";
$i= $i+1;
}
You have more than one issue here... But give this a try. (Also I would start to use PDO or mysqli if I were you...)
$result = mysql_query("SELECT gameid, gamename FROM games");
//$storeArray = Array();
echo '<select name="game" style="width: 400px">';
while ($row = mysql_fetch_assoc($result)) {
$gamename = $row['gamename'];
$gameid = $row['gameid'];
echo "<option'".$gameID."'>".$gamename."</option>";
}
echo '</select>';
This is assuming you have an ID field in your games table. You weren't assigning the options any value. Which won't be useful. Also you weren't pulling the data in the way you had it.
Related
$i = 1;
$tbl = "";
$field = "";
if($_POST['jenis']=="Bahan Baku")
{
$tbl = "bahanbaku";
$field = "bb_";
}
else if($_POST['jenis']=="Bahan Penunjang")
{
$tbl = "bahanpenunjang";
$field = "bp_";
}
<select class="span10" name="jenis" class="add-on">
<?php
$opt = "SELECT bb_nama FROM ".$tbl."";
$result = mysql_query($opt);
if($row = mysql_fetch_array($result))
{
echo "<option>". $row[$field.'nama'] ."</option>";
}
?>
</select>
The result is none of the selected rows displayed. which line is wrong?
while ($row = mysql_fetch_array($result))
{
echo "<option>". $row[$field.'nama'] ."</option>";
}
Change
if($row = mysql_fetch_array($result))
for
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
in order to iterate over the entire resultset
$opt = "SELECT bb_nama FROM ".$tbl."";
you are selecting the field bb_nama, but when you retrieving time you are doing $row[$field.'nama']. the field is mismatch while the table value is changing.
in other case, query should be
$opt = "SELECT ".$field."_nama FROM ".$tbl."";
I need to do a search in each $row2 variable, so i could find if there is columns that are not equal to $row1 column.
This is what I tried,
$result1 = mysql_query('SELECT gpu FROM ng2s3_content');
$result2 = mysql_query("SELECT gpu FROM bigc3_gpu");
while ($row1 = mysql_fetch_array($result1)) {
while ($row2 = mysql_fetch_array($result2)) {
foreach ($row2 as $ar) {
if ($ar == $row1[0]) {
}
else {
echo $ar;
}
}
}
}
looks like you want distinct values from your database ,
use mysql query , that will return a resource with distinct values ,
$result = mysql_query('SELECT gpu FROM ng2s3_content UNION SELECT gpu FROM bigc3_gpu');
while ($row = mysql_fetch_array($result)) {
echo $row['gpu'];
}
I have a table named members with 3 rows. The following code attempts to display all the rows in the members table. It displays the 1'st record 3 times instead of displaying each record once.
<?php
$con = new mysqli("localhost", "root", "jce123", "profile");
if (mysqli_connect_errno()) {
printf("Connection failed: %s\n", mysqli_connect_error());
exit();
}
$sql = "SELECT * FROM members";
$res = mysqli_query($con,$sql);
$num = mysqli_num_rows($res);
$array = mysqli_fetch_assoc($res);
$keysarray = array_keys($array);
$valuearray = array_values($array);
for ($i=0; $i < $num; $i++) {
for($j = 0; $j < count($array); $j++) {
echo "<table>";
echo "<tr><td>".$keysarray[$j].": </td><td>".$valuearray[$j]."</td></tr>";
echo "</table>";
}
echo "<br><br>";
}
mysqli_close($con);
?>
I've updated this per suggestions:
$sql = "SELECT * FROM members";
$res = mysqli_query($con,$sql);
$num = mysqli_num_rows($res);
$array = mysqli_fetch_assoc($res);
while ($row = mysqli_fetch_assoc($res)) {
foreach($array as $key => $value){
echo "<table>";
echo "<tr><td>".$key.": </td><td>".$value."</td></tr>";
echo "</table>";
}
echo "<br><br>";
}
That's because, your $num = 3, count($array) = 3 and the outer for loop has no bearing on inner for loop.
Where you have $array = mysqli_fetch_assoc($res);, you will only receive one row from your database. You need something like:
while ($row=mysqli_fetch_assoc($res))
{
// processing for each row
}
Any basic tutorial will tell you this. Even the PHP docs provide an example.
you can use do something like this..
while ($row=mysqli_fetch_assoc($res))
{
// processing for each row
e.g
echo $row['id'];
echo $row['name'];
etc
}
try this... this peace of code is not tested but just to give you an idea...
$sql = "SELECT * FROM members";
$res = mysqli_query($con,$sql);
$num = mysqli_num_rows($res);
while ($row = mysqli_fetch_assoc($res)) {
echo "<table>";
echo '<tr><td> id = "'.$row['id'].'":
</td><td> name = "'.$row['name'].'"</td></tr>";
echo "</table>";
}
I have been stuck with a problem and I am newbie in mysql and php, Here is the code first , so that I can explain in detail:
$metros = array(1,263);
foreach($metros as $metro_id) {
$sql = "SELECT cuisine_id, cuisine_name_en FROM poi_restaurant_cuisines";
$result = mysql_query($sql);
$cuisine_id = array();
$cusine_name = array();
while($row = mysql_fetch_assoc($result)) {
$cuisine_id[] = $row['cuisine_id'];
$cuisine_name[] = $row['cuisine_name_en'];
}
foreach ($cuisine_id as $cuisine) {
$sql = "
SELECT COUNT(*)
FROM poi AS p
LEFT JOIN poi_restaurant AS pr USING (poi_id)
WHERE p.poi_address_prefecture_id = '$metro_id'
AND pr.poi_restaurant_cuisine_id_array
AND find_in_set('$cuisine', poi_restaurant_cuisine_id_array)
AND p.poi_status = 1";
$result = mysql_query($sql);
$count_cuisine = array();
while($row = mysql_fetch_array($result)) {
$count_cuisine[$metro_id][$cuisine] = $row['COUNT(*)'];
}
echo "<table border = 1 cellpadding= 5 cellspacing= 5 width= 100>";
echo "<tr><th>CuisineID</th><th>Count</th></tr>";
echo "<tr><td>";
echo $cuisine;
echo "</td><td>";
echo $count_cuisine[$metro_id][$cuisine];
echo "</td><td>";
echo "</tr>";
echo "</table>";
}
}
The poi_restaurant_cuisine_id_array contains csv values. I am able to produce the count and the cuisine ID on the web page. I want to replace the cuisine ID with the name of the cuisine. I am not very good at sql or either PHP as I am a beginner. I hope I am being clear enough. Any help is highly appreciated ...Thank you.
Try this:
echo '<table border="1" cellpadding="5" cellspacing="5" width="100">';
echo "<tr><th>Cuisine</th><th>Count</th></tr>";
$metros = array(1,263);
foreach($metros as $metro_id) {
$sql = "SELECT cuisine_id, cuisine_name_en FROM poi_restaurant_cuisines";
$result = mysql_query($sql);
$cuisines = array();
while($row = mysql_fetch_assoc($result)) {
$cuisines[] = array(
'id' => $row['cuisine_id'],
'name' => $row['cuisine_name_en'],
);
}
foreach ($cuisines as $cuisine) {
$sql = "
SELECT COUNT(*)
FROM poi AS p
LEFT JOIN poi_restaurant AS pr USING (poi_id)
WHERE p.poi_address_prefecture_id = '$metro_id'
AND pr.poi_restaurant_cuisine_id_array
AND find_in_set('{$cuisine['id']}', poi_restaurant_cuisine_id_array)
AND p.poi_status = 1";
$result = mysql_query($sql);
$count_cuisine = array();
while($row = mysql_fetch_array($result)) {
$count_cuisine[$metro_id][$cuisine['id']] = $row['COUNT(*)'];
}
echo "<tr>
<td>{$cuisine['name']}</td>
<td>{$count_cuisine[$metro_id][$cuisine['id']]}</td>";
</tr>";
}
}
echo "</table>";
Assuming cuisine_id is a unique identifier, then just use it as the array index....
while($row = mysql_fetch_assoc($result)) {
$cuisines[$row['cuisine_id']] = $row['cuisine_name_en'];
}
....
foreach ($cuisines as $cuisine_id=>$cuisine_name_en) {
However storing multiple values in a single column is a very bad idea.
Generating and running queries in a loop is another very bad idea.
It is possible to reduce this to a single query, declared and invloked outside the inner loop but because your data is not mormalized, this is rather complex.
The query I have below will only show me one result even if there are multiple matching entries (completely or partially matching). How do I fix it so it will return all matching entries:
//$allowed is a variable from database.
$sql = "SELECT `users`.`full_name`, `taglines`.`name`, `users`.`user_id` FROM
`users` LEFT JOIN `taglines` ON `users`.`user_id` = `taglines`.`person_id`
WHERE ( `users`.`user_settings` = '$allowed' ) and ( `users`.`full_name`
LIKE '%$q%' ) LIMIT $startrow, 15";
$result = mysql_query($sql);
$query = mysql_query($sql) or die ("Error: ".mysql_error());
$num_rows1 = mysql_num_rows($result);
if ($result == "")
{
echo "";
}
echo "";
$rows = mysql_num_rows($result);
if($rows == 0)
{
}
elseif($rows > 0)
{
while($row = mysql_fetch_array($query))
{
$person = htmlspecialchars($row['full_name']);
}
}
}
print $person;
Because your overwriting $person on each iteration.
Hold it in a $person[] array if your expecting more then one. Then loop through it with a foreach loop when you intend to output.
Not related but your also querying twice, you only need 1 $result = mysql_query($sql);
Update (Simple Outputting Example):
<?php
$person=array();
while($row = mysql_fetch_array($query)){
$person[] = array('full_name'=>$row['full_name'],
'email'=>$row['email'],
'somthing_else1'=>$row['some_other_column']);
}
//Then when you want to output:
foreach($person as $value){
echo '<p>Name:'.htmlentities($value['full_name']).'</p>';
echo '<p>Eamil:'.htmlentities($value['email']).'</p>';
echo '<p>FooBar:'.htmlentities($value['somthing_else1']).'</p>';
}
?>
Or an alternative way to is to build your output within the loop using concatenation.
<?php
$person='';
while($row = mysql_fetch_array($query)){
$person .= '<p>Name:'.$row['full_name'].'</p>';
$person .= '<p>Email:'.$row['email'].'</p>';
}
echo $person;
?>
Or just echo it.
<?php
while($row = mysql_fetch_array($query)){
echo '<p>Name:'.$row['full_name'].'</p>';
echo '<p>Email:'.$row['email'].'</p>';
}
?>