I'm quite new to sql and I'm having trouble writing this
i have a table like this
id name
1 A
2 B
3 C
1 D
1 E
1 F
...
I want a query that will return to me the names of the ID i give it... say if i ask for 1, i want it to return A D E F
i tried this in PHP:
$result3 = mysql_query("SELECT name FROM table WHERE id='1'", $link);
$pfilearray=array();
$pfilearray=mysql_fetch_assoc($result3);
print_r($pfilearray);
but this gives me only the first name found with id 1, what am i missing?
You should iterate the whole result set using while() and mysql_fetch_assoc(). example:
while(($rs=mysql_fetch_assoc($result3))!=null)
{
$pfilearray[]=$rs['name'];
}
print_r($pfilearray);
You can use this code and verify you could able to retieve
while($row = mysql_fetch_array($result3))
{
echo $row['name'];
echo "<br>";
}
Loop through your result set using mysql_fetch_array().
For more help click here
mysql_fetch_assoc returns one row from your result set so it will only return the first value. You need to loop through your result set ($result3 in your code) and get all the rows.
btw, the mysql_ family of functions is quite old and most people would suggest using the mysqli_ family, or even better, PDO
EDIT:
A very basic PDO example:
$ddh = new PDO( /* connection details */ );
$sth = $dbh->prepare('SELECT name FROM table WHERE id= ? ');
$sth->execute(array(1));
$names = $sth->fetchAll();
Try this
$result3 = mysql_query("SELECT name FROM table WHERE id='1'", $link);
while($row=mysql_fetch_array($result3))
{
echo $row['name'];
}
Related
How to pass value inside row array to another variable since the result of the row array I need to do another query for selection in other table. In both echo $row['Plate'] and $plateID can show the value, but for another query doesn't show the result. This is my code:
<?php
$connection=mysqli_connect("localhost","root","");
$db=mysqli_select_db($connection,'db_car_identification');
$q="SELECT Plate FROM addplate ORDER BY ID DESC LIMIT 1 ";
$result=mysqli_query($connection,$q);
while($row = mysqli_fetch_array($result)) {
echo $row['Plate'];
$plateID = $row['Plate'];
echo $plateID;
$query="SELECT StuName FROM student_detail WHERE plateID=$plateID";
$query_run=mysqli_query($connection,$query);
while($row1=mysqli_fetch_array($query_run)) {
echo $row1['StuName'];
}
}
?>
You should really be using parameterized prepared statements.
However, in your example you could simply use an SQL subquery instead. It would make your code much simpler.
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$connection = new mysqli("localhost", "root", "", 'db_car_identification');
$connection->set_charset('utf8mb4');
$result = $connection->query('SELECT StuName, Plate
FROM student_detail
WHERE plateID = (SELECT Plate FROM addplate ORDER BY ID DESC LIMIT 1)');
foreach($result as $row1){
echo $row1['StuName'];
}
Please check some step
For 1st Query Result which datatype is it?
If it is Integer or any numeric data type then echo the $query variable and check whatever query is right or wrong by simply copy and paste it into database query builder (phpmyadmin).
If it is String data type then simply add '' to your $plateID variable in query. eg:"SELECT StuName FROM student_detail WHERE plateID='$plateID'" (If not working then echo again and check your query problem)
Let me know if this help.
My question is.... i have a table with 2 columns and 24 rows.
first column is only id and it goes from 1 to 24 and second column is some text in each row and follows id.
i don't know how to SELECT text from each row separated by id in first column and echo it.
I am using xampp - mysql and working in php. TY
i tried something like this and than i used variable $row to call row 0. but i think thats not right way.
$nap_query = "SELECT napomena FROM napomena";
$nap_result = mysql_query($nap_query) or die (mysql_error());
$row = mysql_fetch_row($nap_result);
You need to look at the php mysql_fetch_array documentation: http://php.net/manual/en/function.mysql-fetch-array.php
As the comments suggest mysql_fetch_row doesn't work for multiple rows.
So you can do that
while($row = mysql_fetch_array($nap_result))
echo $row['napomena'];
Aditions
It's best not to use mysql anymore and use mysqli or prefebly PDO in order to handle database connections. Read PDO and mysqli documentation here.
If you want to get only one row result
$rownum=0; //zero or something
echo mysql_result($nap_result, $rownum, "napomena");
Multi values use mysql_fetch_array
while($row=mysql_fetch_array($nap_result)){
echo $row['napomena'];
}
$nap_query = "SELECT field FROM table";
$nap_result = mysql_query($nap_query) or die (mysql_error());
while($row = mysql_fetch_row($nap_result))
echo $row['field'].'<br/>';
This will result like this
resultA
resultB
resultC
etc
Just like #punitha said, it's better to use PDO
The query SELECT * FROM TABLE WHERE id LIKE '%1% is not working properly, it's not select the id 1.
mysql_connect('localhost', 'root' , '');
mysql_select_db('database');
$sql = ("select * from search WHERE id LIKE '%3%'");
mysql_query($sql);
$my_variable = mysql_query($sql);
$display_data = mysql_fetch_row($my_variable);
while ($list = mysql_fetch_assoc($my_variable)) {
$id = $list['id'];
$title = $list['title'];
$keywords = $list['keywords'];
$img = $list['img'];
$link = $list['link'];
}
If you are looking to SELECT id 1 then use = not LIKE. The way LIKE is being used it will match every id that has a 1 in it and you are not guaranteed to get the first one in order, so instead use:
SELECT * FROM search WHERE id = 1
According to the PHP documentation of mysql_fetch_row it
Returns a numerical array that corresponds to the fetched row and moves the internal data pointer ahead.
Which means that the first result won't show up in the next (mysql_fetch_assoc) procedure. You could try removing the $display_data = mysql_fetch_row($my_variable); line and only use the while($list = mysql_fetch_assoc($my_variable)) { ... } procedure. See if that solves your problem.
$sql = ("select * from search WHERE id ='3'");
The id is an integer use = instead of like . Equal is more accurate.
And first echo your query in your program->
echo $sql;die;
copy that query and run it on your phpmyadmin
and then check is your column id is int type if it is then like will not give you the result. You have to use the where clause here .But if you have the column id is of type varchar then definitely give you the result .
Try to use search tab under your database->table in your phpmyadmin and put the condition there.
You will definitely get your answer there.
How would I add up all the integers in the column, _view_count_, on my table, 'videos', then echo it to display on my page?
For example:
if row id 1 has view_count == 328
and
if row id 2 has view_count == 271
How would I make MySQL add those together and echo it out?
You can use MySQL's SUM() and your SQL query would look something similar to:
SELECT SUM(view_count) FROM videos;
To query and echo the value, if you're using mysqli methods in your PHP code, you can use:
$result = $mysqli->query('SELECT SUM(view_count) AS sum FROM videos;');
$row = $result->fetch_assoc($result);
echo $row['sum'];
Assuming you have a $mysqli object defined your code should look like:
$query = "SELECT Sum(view_count) as mySum FROM videos";
$result = $mysqli->query($query);
$row = $result->fetch_assoc($result);
echo $row['mySum'];
SELECT SUM(view_count) FROM videos
Assuming you have a COLUMN id, you can use SUM together with IN, like so:
SELECT SUM(view_count) FROM videos WHERE id in (1,2)
I have this
$sql = "SELECT id FROM table";
$result = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_array($result)){
echo $row['id'];
}
This echo's all id's found in the table.
How can I choose to echo only a selected id.
Say the second id found on the table?
EDIT
I think I have confused people and myself aswell.
Let me try to explain again.
Using the above query I can echo all results found in the table with echo $row['id'];
However I do not want echo all results, just selected ones.
You guys have suggested I use limit or a Where clause.
If I do this I will be limited to just one record. This is not what I want.
I want to echo a selection of records.
Something likes this
echo $row['id'][5], $row['id'][6], $row['id'][6]
But obviously this is incorrect syntax and will not work but hopefully you get what I am trying to do.
Thanks
If you only want the second row then you could change your query to use offset and limit e.g.
SELECT id FROM table LIMIT 1, 1
You could also use a for loop instead of the while loop and then put in a conditional.
UPDATE
Just noticed comments above - you also need to sort the PHP bug by changing mysql_fetch_array to mysql_fetch_assoc.
UPDATE 2
Ok based on your update above you are looking to get all of the rows into an array which you can then iterate over.
You can just use mysql_fetch_array and then use $array[0]. For example:
$sql = "SELECT id FROM table";
$result = mysql_query($sql) or die(mysql_error());
$ids = array();
while($row = mysql_fetch_array($result)) {
$ids[] = $row[0];
}
From what I can gather from your questions you should not be selecting all records in the table if you wish to just use the Nth value, use:
SELECT id FROM table LIMIT N, 1
That will select the Nth value that was returned. Note: The first result is 0 so if you wish to get the second value the Nth value should be 1.
mysql_data_seek() let's you jump to a specific data-set(e.g. the 2.nd)
Example:
$sql = "SELECT id FROM table";
$result = mysql_query($sql) or die(mysql_error());
//get the 2nd id(counting starts at 0)
if(mysql_data_seek($result,1))
{
$row=mysql_fetch_assoc($result);
echo $row['id'];
}
OR:
use mysqli_result::fetch_all
It returns an array instead of a resultset, so you can handle it like an array and select single items directly (requires PHP5.3)