I want to select a row from a database table, using where id= 'x'.
The thing is that I want to be able to change that 'x' dynamically, so if I need to select a row where id= 'y', I want to be able to just load y in another row and read it from that row.
Sorry for my bad english.
<?php
$id = a field from another table
$result = mysqli_query($con,"SELECT * FROM text where id= '$id'");
while($row = mysqli_fetch_array($result))
{
echo $row['text_area'];
}
?>
Here is an image that I hope will explain this better.
Try this code:
<?php
$result1 = mysqli_query($con,"SELECT * FROM id_val");
while($row1 = mysqli_fetch_assoc($result1)){
$result = mysqli_query($con,"SELECT * FROM text where id= '{$row1['value']}'");
while($row = mysqli_fetch_assoc($result))
{
echo $row['text_area'];
}
}
?>
Related
Here I have a translator that must get data from mysql and then show them like a $_name = array ($_en, $_ru, $_ua) and then I want to choose data by cases [0] or [1] or [3]
I need to show mysql result an array
I've already try this it but it doesn't work
<?php
$result1 = mysql_query("SELECT t_name, t_ru, t_ua, t_en FROM transl_db ");
while ($row = mysql_fetch_array($result1)) {
t_name = array ('t_ru', 't_ua', 't_en' )
}
?>
$result = mysql_query("SELECT t_name, t_ru, t_ua, t_en FROM transl_db ");
while ($row = mysql_fetch_array($result))
$t_names[] = $row;
Try
$result1 = mysql_query("SELECT t_name, t_ru, t_ua, t_en FROM transl_db ");
while ($row = mysql_fetch_array($result1)) {
$t_names[] = array ('t_ru'=>$row['t_ru'],'t_ua'=> $row['t_ua'],'t_en'=>$row['t_en'] );
}
or
while ($row = mysql_fetch_array($result1)) {
$t_names[] = $row;
}
now data available on $t_names
I tried it but I have to get also $t_name from mysql and then have it like an array
$result1 = mysql_query("SELECT t_name, t_ru, t_ua, t_en FROM transl_db ");
while ($row = mysql_fetch_array($result1)) {
('t_name'=>$row['t_name'] )= array ('t_ru'=>$row['t_ru'],'t_ua'=>
$row['t_ua'],'t_en'=>$row['t_en'] );
};
?>
I'm looking for a way to search a MySQL database for specific values and put these in an array.
The contacts table has name,email,group,Phone...
I would like to search the database by group and return the email adresses in an array, separated by , (comma) to use further in my code.
What is the best way to do this?
$result = mysqli_query($link,"SELECT * FROM Contacts WHERE Group='Group 1'")
or die(mysqli_error());
...
while($row = mysqli_fetch_array( $result ))
{
array ( row->email,...)
}
Here you have different options beginning with:
Update your select query to only display the email, if you don't require the other fields this is the way to go
In your loop just add only the email field to the array
$dataArray[] = $row->email;
$result = mysqli_query($link,"SELECT email FROM Contacts WHERE Group='Group 1'")
or die(mysqli_error());
$emailArray = [];
while($row = mysqli_fetch_array( $result ))
{
array_push($emailArray,$row->email);
}
$responseEmail = implode(",", $emailArray);
Hope this will works!
I found the way of what I was trying to do. I needed a selectbox with values from the database.
....
$query = mysqli_query($link,"SELECT * FROM XXX WHERE category='$category' AND stelplaats='$stelplaats'");
echo '<td><select name ="collega" required>';
echo '<option value"">---</option>';
while ($row2 = mysqli_fetch_array( $query ))
{
echo '<option value =" '.$row2['id'].'"';
echo '>'.$row2['name'].'</option>';
}
echo '</select></td></tr>';
....
Hi gus this is my first question in this coumunity i hope someone answer me
when i want to search from array of mysql_fetch_arrray he give me on error
this is the code
$key = "Meca";
$query = "SELECT * FROM subject WHERE `name` LIKE '%$key%' Or `sale` LIKE '%$key%' Or `Emphet` LIKE '%$key%' ";
$sql = mysql_query($query);
$num = mysql_num_rows($sql);
while ($num >0)
{
$row = mysql_fetch_object($sql);
$num--;
$row = mysql_fetch_object($sql);
in_array($row , "Meca");
}`
You are not clear with your question;
Anyway I give a try:
$key = "Meca";
$query = "SELECT * FROM subject WHERE name LIKE '%$key%' Or sale LIKE '%$key%' Or Emphet LIKE '%$key%'";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) {
if(in_array($row , "Meca")){
echo "$key is present this time."
}
}
Note that:
I am not on your requirement. So I just make a rough sketch.
You have not provided your error message.
You may have error in your code(SQl or PHP)[like usage of names
'Emphet'].
Anyway give a try.
I'm trying to get an array of id's from my database and then be able to echo out each id.
Something like this:
$query = mysql_query("SELECT id FROM TableName WHERE field = 'test' ORDER BY id DESC") or die(mysql_error());
$row = mysql_fetch_array($query);
echo "array: ".$row[1]." <br>";
echo "array: ".$row[2]." <br>";
echo "array: ".$row[3]." <br>";
This doesn't seem to be working though?
The problem is that mysql_fetch_array fetches an ARRAY, which is 0-based. You're fetching a single field from the database, which will be stored at $row[0] in your result array. Since you're echoing out only row[1] through row[3], you'll never see the result:
$row = mysql_fetch_array($query);
print_r($row);
should give you:
Array (
0 => 'id_field_value_here'
)
and
echo $row[0]
would also output
id_field_value_here
mysql_fetch_array fetches 1 row. You need to do something like
...
$res = array();
while ($row = mysql_fetch_array($query))
{
$res[] = $row;
}
//now $res[0] - 1st row, $res[1] - 2nd, etc
I have the following query, and would like to list only the first match.
$first = $_GET['category'];
$first = $first[0] . "%";
$query = mysql_query("SELECT * FROM lyrics WHERE authorclean LIKE '".$first."'") or die(mysql_error());
(?category=b)
So DISTINCT could do this right? This is what I tried, but did not work:
$query = mysql_query("SELECT DISTINCT authorclean FROM lyrics WHERE authorclean LIKE '".$first."'") or die(mysql_error());
EDIT: Here is the full code:
function getCategory() {
$first = $_GET['category'];
$first = $first[0] . "%";
$query = mysql_query("SELECT DISTINCT authorclean FROM lyrics WHERE authorclean LIKE 'B%'") or die(mysql_error());
//$query = mysql_query("SELECT * FROM lyrics WHERE authorclean LIKE '".$first."'") or die(mysql_error());
if(mysql_num_rows($query) == 0) {
echo "Geen resultaten gevonden.";
} else {
while ($row = mysql_fetch_assoc($query)) { ?>
<p><?= $row['author']; ?></p>
<?php }
}
}
(B% is just for testing)
If I run this following query in the database directly I get two results. If I run with the code above I just get an empty page (except for the html thats already there).
SELECT DISTINCT authorclean FROM lyrics WHERE authorclean LIKE 'B%'
You should use LIMIT 1 to list only the first match.
If you have a a table "tbl_lyrics" with fields: author lyrics year and is filled for example as follows:
author_A lyrics_A year_A
author_A lyrics_A1 year_A1
author_A1 lyrics_A2 year_A
author_B lyrics_B1 year_B1
if you do
select distinct(author) from tbl_lyrics where author like '%author_A%'
you are going to get: author_A and author_A1. NOT the first one that matches.
If you want the first one that matches you can do:
select author from (select distinct(author) as author from tbl_lyrics where author like '%author_A%') where rownum <2;
this will return author_A only.
Limit is used with MySql but would not work with oracle databases