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.
Related
I'm new in php programming, please help me.
I've to make two postgres queries from two separate databases, but one of the fist queries condition based on the other query result:
1st query:
<?php
$result = "SELECT ...
WHERE ... ";
$query = $pgszlaConn->prepare($result);
$query->execute();
while($row = $query->fetch(PDO::FETCH_BOTH)) {
$contractid=$row[0];
echo "<tr><td>$contractid</td></tr>";
?>
/In this query I've multiple results./
2nd query:
<?php
$result = "SELECT ...
WHERE contract.contract_id= :contract";
$query = $pgConn->prepare($result);
$query->bindparam(':contract', $contractid);
$query->execute();
while($row = $query->fetch(PDO::FETCH_BOTH)) {
echo "<tr><td>$row[0]</td></tr>";
?>
My problem is that the 2nd query has only one result based on the 1st query multiple results.
I'm not sure is this a good solution or not but my programming skills not so advanced ;-)
Please help me if you can!
Thanks a lot!
D. Attila
If i understand what you mean.
You need to make the second query within the loop of the first query so that for each id of the first query available, the id will be passed to the second query to query the database of the second table.
Not with my system would have shown you some of my code.
while($row = $query->fetch(PDO::FETCH_BOTH)) {
echo "<tr><td>$contractid</td></tr>";
## now you make the second query to the second table with the id of the first table
$result = "SELECT WHERE contract.contract_id= :contract";
while($row = $query->fetch(PDO::FETCH_BOTH)) {
echo "<tr><td>$row[0]</td></tr>";
} ## end of second query loop
} ## end of first query loop
Something like this should work just get the idea and implement it for yourself
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.
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'];
}
I have two tables, both containing a field named "id" that has different data in each table. I need to retrieve both ids to construct an URL. I'm using the following code:
<?php // no direct access
mysql_connect("localhost", "user", "pass") or die(mysql_error());
mysql_select_db("db") or die(mysql_error());
$value = $_POST['password'];
$content = mysql_query("SELECT * FROM jos_content WHERE pass='$value'") or die(mysql_error());
$menu = mysql_query("SELECT * FROM jos_menu WHERE menutype='mymenutype' AND alias='$value'") or die(mysql_error());
$id = mysql_fetch_array( $content );
$itemid = mysql_fetch_array( $menu );
// Print out the contents of each row into a table
if(isset($_POST['password'])){
header('Location: ' . 'index.php?option=com_content&view=article&id=' . $id['id'] . '&Itemid=' . $itemid['id']);
exit;
}
?>
However, I get data only from the first query, while the second returns nothing. Am I doing anything wrong? Perhaps my SELECT query is not spelled correctly? Please, help.
Thanks in advance,
S.
First thing that I see is that you use the password in your second query also, which my intuition tells me is not quite what you want.
Anyway, in order to debug the query, first, have a look in the error log and see if or die(mysql_error()) part is throwing something.
If not, echo the $menu string (query), run that exactly as it will be shown into mysql and see if returns some records. When you make it return something, change the string, and there you go - it should work
You can use var_dump($menu) to see the results for debuging. if $itemid is a multidimensional array u have to use index properly like $itemid[0]['id'] and you can try echo the query and and use same query in mysql as (Tudor Constantin) suggested. try to use mysql joins like this
select jc.id as id, jm.itemid as itemid FROM jos_content as jc left join jos_menu as jm on jm.alias=jc.pass WHERE jm.menutype='mymenutype' and jc.pass='$value'
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)