list table
id
name
illness
recipe
directions
$illness = 'Highblood';
$list = mysql_query("SELECT * FROM list ",$con);
while($row=mysql_fetch_array($list, MYSQL_ASSOC)){
$test[] = $row['recipe'];
$test2[] = $row['directions'];
}
I want to only get the rows
$row['recipe'];
$row['directions'];
if the value of illness column is equals to highblood.
how do I do this?
I suggest to make some search on google to learn MySQL if you can't do basic query like this.
mysql_query("SELECT * FROM list WHERE illness = '".$illness."'",$con);
Select only required rows.
$list = mysql_query("SELECT recipe,directions FROM list WHERE illness='Highblood'",$con);
while($row=mysql_fetch_array($list, MYSQL_ASSOC)){
$test[] = $row['recipe'];
$test2[] = $row['directions'];
}
Related
I have three variables:
$school
$class
$subject
And a search String $searchquery
I want to search my database for the rows that match the Search Query... Currently I am using this:
$searchquery = str_replace(" ","|",$searchquery);
$sql = "
SELECT * FROM filedetails
WHERE school RLIKE '$searchquery'
OR class RLIKE '$searchquery'
OR subject RLIKE '$searchquery'";
Now, how this works, Suppose I enter Mathematics Class X. It converts the string into Mathematics|Class|X which is fine and search the Database and prints the results...
But there is one error:
It is printing all the rows which have either Mathematics or Class or X in them, so my output includes all the Classes of all Schools of Mathematics and All Subjects of All Schools of Class X...
Whereas I want it to show me All Schools having Mathematics and Class X...
P.S. - I have tried using AND instead of OR, but now I get empty result because no School Matches my Query String...
One way of doing is first make different 2-D Arrays of all the data searches and then take their intersection, so this will give the common row which matches all the keywords...
$sql = "SELECT * FROM filedetails";
$query = mysql_query($sql);
$array1 = array();
while ($row = mysql_fetch_array($query))
{
$array1[] = $row;
}
$sql = "SELECT * FROM filedetails WHERE school RLIKE '$searchquery'";
$query = mysql_query($sql);
$array2 = array();
while ($row = mysql_fetch_array($query))
{
$array2[] = $row;
}
$sql = "SELECT * FROM filedetails WHERE subject RLIKE '$searchquery'";
$query = mysql_query($sql);
$array3 = array();
while ($row = mysql_fetch_array($query))
{
$array3[] = $row;
}
$sql = "SELECT * FROM filedetails WHERE class RLIKE '$searchquery'";
$query = mysql_query($sql);
$array4 = array();
while ($row = mysql_fetch_array($query))
{
$array4[] = $row;
}
Now, what this code is doing that it is searching individually for every field and making separate result for each Query of each field, now We will be taking Intersection of the arrays so as to get the common row having the keywords
But there is one problem, suppose the $searchquery contains Details about School and Class, but no information about Subject, then the $array3 will be null, as the query will result NULL, then we need to consider handling null arrays which have been solved in Another Question Here...
Hope this helps... :)
I have a small issue that I can't figure out.
I have to pull data from two different tables, in one loop. I've never done that before, so I have no idea how. I tried two different queries. That looked like this:
$query = "SELECT * FROM colors ";
$color_select = mysqli_query($connection, $query);
$second_query = "SELECT * FROM votes";
$vote_select = mysqli_query($connection, $second_query);
And then put them into a loop:
while($row = mysqli_fetch_assoc($color_select) && $second_row = mysqli_fetch_assoc($vote_select))
{
$color = $row['Colors'];
$votes = $second_row['Votes'];
echo "<tr><td>$color</td><td>$votes</td></tr>";
}
But that didn't work. I didn't expect it to, just wanted to try. :) Maybe someone experienced can help me out. Thanks.
At the end of the day I need a table displayed, that has two columns, one of them contains the color name from one DB table and the other one contains a number of votes.
As requested: table structures.
Table: colors has only one field Colors.
Table: votes has four fields city_id, City, Colors and Votes
*************************EDIT**************************************
So fixed up the query as suggested, but is still shows nothing.
Here is the edited code:
$query = "SELECT * FROM colors,votes WHERE colors.Colors=votes.Colors";
$color_votes_select = mysqli_query($connection, $query);
while($row = mysqli_fetch_assoc($color_votes_select))
{ $color = $row['Colors'];
$votes = $row['Votes']; }
if table having relation.
try this in single query .
SELECT
`colors`.*,votes.*
FROM
`colors`
INNER JOIN
`votes` ON
`votes`.colorId = `colors`.Id
Most imp *****You should have some relationship between tables
Otherwise workaround
Run query on color, Save it in ArrayA
Run query on vote, Save it in ArrayB
Create New Array ArrayC
$arrayC = array();
Loop array A or C if they both contact same row count
array_push($ArrayC, key and value of color, key and value of votes);
Final loop ArrayC to print tr and td
First Relate These two tables, write color_id in votes table.
$query = "SELECT * FROM colors,votes where colors.id=votes.color_id";
$color_select = mysqli_query($connection, $query);
while($row = mysqli_fetch_assoc($color_select))
{
$color = $row['Colors'];
$votes = $row['Votes'];
}
Try this:
$query = "SELECT colors FROM colors";
$color_select = mysqli_query($connection, $query) or die (mysqli_error());
$second_query = "SELECT votes FROM votes"; //you only need column votes right?
$vote_select = mysqli_query($connection, $second_query) or die (mysqli_error());;
while( $row = mysqli_fetch_assoc($color_select) && $second_row = mysqli_fetch_assoc($vote_select)){
$color[] = $row['colors'];
$votes[] = $second_row['votes'];
echo "<tr><td>$color</td><td>$votes</td></tr>";
}
Short explanation:
It will fetch the select and store into an array (because what you were doing is selecting multiple rows into one single variable) and then just display with the echo.
Ok thanks to all of u guys for your answers.
I've made some changing but i still can't display my array ...
Look at what i've done :
$prog = array();
$i=0;
while($row = $get_programmation->fetch_assoc()){
$prog[$i] = $row;
$i++;
}
echo json_encode($prog);
I would have this a result like this : {"id":"0","artist":"xxxx", .... etc} and then use it in my iOS app to display what i want.
mysql_query("SELECT * FROM `programmation`");
Backticks instead of single quotes
Also in here $articles[] = $row;
in $row variable you have to specify MySQL table column names which you have to insert into array, for instance $row['id'], etc...
NOTE: See mysql_ deprecated post too
you can not use quotes in table name or field name but you can use backtick()
$get_programmation = mysql_query("SELECT * FROM 'programmation'");
should be:
$get_programmation = mysql_query("SELECT * FROM `programmation`");
or
$get_programmation = mysql_query("SELECT * FROM programmation");
Replace this $get_programmation = mysql_query("SELECT * FROM 'programmation'");
with this $get_programmation = mysql_query("SELECT * FROM programmation");
Then importantly change code in while loop as below
while($row = mysql_fetch_assoc($get_programmation)){
array_push($articles, $row['yourcol1'],$row['yourcol2']);
}
echo json_encode($articles);
$row is not actual array. You can get the value if it using your actual column name only. And here I have pushed it to the array in while loop to form as array.(Here I have mentioned only two column, You can take all your columns in such way.)
So I have a mysql table that contains an integer in each row. What I am trying to do is get the sum of the integers. Here is what I currently have that is working.
$cn = 0;
$sql = mysqli_query($con,"SELECT * FROM members WHERE member='$userid'");
while($row = mysqli_fetch_array($sql)) {
$i = $row['number'];
$cn = $cn + $i;
}
echo $cn;
So I make $cn equal zero then each time it goes through the loop it will add the number from the matching row.
Does anyone have a better idea on how to accomplish this? Thanks!
You don't need to use PHP or here a loop for that, because your database can do the job for you.
$sql = mysqli_query($con,"SELECT sum(number) FROM members WHERE member='$userid'");
If you want the sum of the column number for each member with a particular user id
$sum = 0;
$sql = mysqli_query($con,"SELECT SUM(number) as number_total FROM members WHERE member='$userid'");
while($row = mysqli_fetch_array($sql)) {
$sum = $row['number_total'];
}
$sum will have the total
I am trying to print the duplicate records of the table but only the single row is getting
echoed.However in mysql this query results all the duplicate records. Here is the query:
$q = mysql_query("SELECT * FROM add WHERE cust_id = '144' GROUP BY cust_id");
$r = mysql_fetch_array($q);
$s = mysql_num_rows($q);
while($s !=0)
{
echo $r;
$s=$s-1;
}
Whats wrong with the code?
$q = mysql_query("SELECT * FROM add WHERE cust_id = '144' GROUP BY cust_id");
while($r = mysql_fetch_array($q))
{
print_r($r);
}
You need to loop through the entire record set... you are only grabbing the first row:
$resultset = mysql_query("select * from add where cust_id = '144' group by cust_id");
while($row = mysql_fetch_assoc($resultset))
{
echo $row['column_name'];
}
Your SQL query will in practice only ever return 0 or 1 rows, due to the GROUP BY clause. Are you absolutely sure that that's the query you were executing in mysql?
well, if you want to get duplicate values, then this query will serve you well:
T = the table
f = the field to check for duplicates
id = the rows id
select id,f from T group by f having count(f)= 2;
or >2 if you want every value in f that occurs in more than one row.
having is like where but evaluated after group by.
Try the following:
$q = mysql_query("SELECT * FROM add WHERE cust_id = '144'");
while($r = mysql_fetch_array($q))
{
echo $r;
}