Using PHP $_GET in oci_parse - php

I need to use an echo $_GET['id']; in oci_parse but cannot seem to get it working. I have tried escaping it in ', " etc. but neither have worked.
Code:
$stid = oci_parse($conn, "
SELECT COLA,
(SELECT COLB FROM T2 WHERE T2.COLB = echo $_GET['id'];)
FROM T1
WHERE T1.COLA = echo $_GET['id'];
");

Method 1
$stid = oci_parse($conn, "SELECT COLA,
(SELECT COLB FROM T2 WHERE T2.COLB = {$_GET['id']})
FROM T1
WHERE T1.COLA = {$_GET['id']}");
Method - 2
$stid = oci_parse($conn, "SELECT COLA,
(SELECT COLB FROM T2 WHERE T2.COLB = ".$_GET['id'].")
FROM T1
WHERE T1.COLA = ".$_GET['id']."");

you have to do some validation before using it inside the query ..
if (ctype_digit($_GET['id'])){
$dIdUser = $_GET['id'];
$stid = oci_parse($conn, "
SELECT COLA,
(SELECT COLB FROM T2 WHERE T2.COLB =$dIdUser ;)
FROM T1
WHERE T1.COLA = $dIdUser;
");
}

Maybe you need after query first execute the query?
<?php
$conn = oci_connect('hr', 'welcome', 'localhost/XE');
// Parse the statement. Note there is no final semi-colon in the SQL statement
$stid = oci_parse($conn, 'SELECT * FROM employees');
oci_execute($stid);
echo "<table border='1'>\n";
while ($row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS)) {
echo "<tr>\n";
foreach ($row as $item) {
echo " <td>" . ($item !== null ? htmlentities($item, ENT_QUOTES) : " ") . "</td>\n";
}
echo "</tr>\n";
}
echo "</table>\n";
?>

Related

Show MySQL row content on PHP from specific table column (same .row name on different tables)

In my app, I have two tables that contain two columns with the same column name
Table TITLES
COLUMN = name - CONTENT example: Jurassic World Trailer
Table VIDEOS
COLUMN = name - CONTENT example:<img src='/abc.png'>
I need to display rows from both columns "name" in my PHP file, but the row="name" from TITLES table always comes first, and I cant dispaly the image from the VIDEOS row="name".
// output data of each row
while($row = $result->fetch_assoc()) {
$written_fichas = $written_fichas .' target="_parent">'.$row['name'].'</a></b></div></td></tr><tr><td align="right"><div class="44"> '.$row["name"];
It is clear that I must change the way in which the second query is extracted for the table "videos" and add besides $row['name'] and the name of the table, but how should I do this?
<div class="44"> '$VIDEOS-TABLE->.$row["name"];
Expected result for the code above
target="_parent">Jurassic World Trailer</a> //name come from titles table and is OK
</b></div></td></tr><tr><td align="right">
<div class="44"><img src='/abc.png'> //name NEED to come from VIDEOS table
Thanks for the help.
UPDATE - more code.
// First query
$sql = "SELECT MAX(l.`id`) FROM `videos` as l WHERE l.approved = 1" . $type2 ." GROUP BY l.`title_id`, l.`name`, l.`season`, l.`episode` ORDER BY l.`id` DESC LIMIT 1000";
$result=mysqli_query($conn,$sql);
if ($result->num_rows > 0){
$ids_max=[];
while($row = $result->fetch_row())
$ids_max[]=$row[0];
$where_in = implode(',', $ids_max);
// Second query:
$sql = "SELECT l.id, l.name, l.title_id, t.name, t.poster, l.season, l.episode, l.approved FROM `videos` as l, `titles` as t WHERE l.title_id = t.id AND l.`id` IN (" .$where_in .")" .$type ." ORDER BY l.`id` DESC LIMIT " .$offset .", " .$limit;
//print $sql;
$result = $last_id = $conn->query($sql);
//write head
$file = fopen($filename, "w");
$written_head = "
<head>
<link type=\"text/css\" rel=\"stylesheet\" href=\"/styles.css\">
</head>
<body>
fwrite($file, $written_head . PHP_EOL);
if ($result->num_rows > 0){
// output data of each row
while($row = $result->fetch_assoc()) {
if (empty($row['episode'])) {
$written_fichas = "<div><table><tr><td><a href=/".$row['title_id'].'-'.$row['name'];
$written_fichas = $written_fichas .' target="_parent"><img src='.$row['poster'].' /></a><div> </div></td>';
$written_fichas = $written_fichas ."</tr><tr><td><table><tr><td></td></tr><tr><td><div class='55'><b><a href=/".$row['title_id'];
$written_fichas = $written_fichas .' target="_parent">'.$row['name'].'</a></b></div></td></tr><tr><td><div class="44"> '.$row["name"];
$written_fichas = $written_fichas .'</div></td></tr></table></td></tr></table></div>';
fwrite($file, $written_fichas . PHP_EOL);
}
}else {
fwrite($file, "0 results". PHP_EOL);}
Use this syntax in MySql:
SELECT column_name AS alias_name FROM table_name;
SELECT column_name(s) FROM table_name AS alias_name;
Why would you use "AS" when aliasing a SQL table?
The following code is a solution for you:
Update second query to this one:
$sql = "SELECT l.id, l.name as video_name, l.title_id, t.name, t.poster, l.season, l.episode, l.approved FROM `videos` as l, `titles` as t WHERE l.title_id = t.id AND l.`id` IN (" .$where_in .")" .$type ." ORDER BY l.`id` DESC LIMIT " .$offset .", " .$limit;
Update last line to this one:
<div class="44"> '.$row["video_name"];

Getting information from multiple using MySQLI and be able to echo results

for the past few hours, I have been trying to find a simple method of while loop echoing information from multiple tables at once. I'd like to say I've not pulled all my hair out looking, but I have.
Here is one mysqli query to get the following fields from CUSTOMER
$tid = $_SESSION['user_id']; // "id" is 1 for example
$query = "SELECT * FROM `CUSTOMER` WHERE user_id = {$tid}";
$results = mysqli_query($dbconnection, $query);
while ($row = mysqli_fetch_array($results)) {
echo $row['user_id'] . "<br><br>";
echo $row['c_fname'] . "<br>";
echo $row['c_sname'] . "<br>";
};
Here is another mysqli query to get the following fields from SALE
$query = "SELECT * FROM `SALE` WHERE user_id = {$tid}";
$results = mysqli_query($dbconnection, $query);
while ($row = mysqli_fetch_array($results)) {
echo $row['s_date'] . "<br>";
echo $row['s_total'] . "<br>";
};
Could someone possibly show me how I can get both of these tables in one query so that echoing both tables information is possible at the same time instead of separately. I am not fussed how it is done, As long as it gets all from both tables for echoing purpose, that is good.
You can do it by using LEFT JOIN like this.
SELECT column_name(s)
FROM table1
LEFT JOIN table2 ON table1.column_name = table2.column_name;
And this is your code.
$query = "SELECT * FROM `CUSTOMER` LEFT JOIN `SALE` ON `SALE`.user_id=`CUSTOMER`.user_id WHERE `SALE`.user_id={$tid}";
$results = mysqli_query($dbconnection, $query);
while ($row = mysqli_fetch_array($results)) {
echo $row['user_id'] . "<br><br>";
echo $row['c_fname'] . "<br>";
echo $row['c_sname'] . "<br>";
echo $row['s_date'] . "<br>";
echo $row['s_total'] . "<br>";
}
For more info read this,
https://www.w3schools.com/sql/sql_join_left.asp
I hope this helps.
EDITED
This is for joining 3 tables,
SELECT column_name(s)
FROM table1
LEFT JOIN table2 ON table1.column_name = table2.column_name;
LEFT JOIN table3 ON table1.column_name = table3.column_name;
In your code.
SELECT * FROM `CUSTOMER`
LEFT JOIN `SALE` ON `CUSTOMER`.user_id = `SALE`.user_id
LEFT JOIN `PRODUCTS` ON `CUSTOMER`.user_id = `PRODUCTS`.user_id
WHERE `SALE`.user_id={$tid};
As variable.
$query = "SELECT * FROM `CUSTOMER` LEFT JOIN `SALE` ON `CUSTOMER`.user_id = `SALE`.user_id LEFT JOIN `PRODUCTS` ON `CUSTOMER`.user_id = `PRODUCTS`.user_id WHERE `SALE`.user_id={$tid}";
You can use the following code and will help u solve Ur problem
$query = "SELECT C.*,S.* FROM CUSTOMER C,SALES S
WHERE C.user_id={$tid}
and C.user_id=S.user_id;
while ($row = mysqli_fetch_array($results)) {
echo $row['C.user_id'] . "<br><br>";
echo $row['C.c_fname'] . "<br>";
echo $row['C.c_sname'] . "<br>";
echo $row['S.s_date'] . "<br>";
echo $row['S.s_total'] . "<br>";
};
You can simply join the tables to get your expected result as shown below.
$query = "SELECT c.user_id, c.c_fname, c.c_sname, s.s_date, s.s_total FROM `CUSTOMER` AS c INNER JOIN `SALE` AS s ON c.user_id = s.user_id WHERE c.user_id = {$tid}";
Joining 3 tables example
$query = "SELECT *
FROM `CUSTOMER` AS c
INNER JOIN `SALE` AS s ON c.user_id = s.user_id
INNER JOIN `PRODUCTS` AS p ON p.product_id = s.product_id
WHERE c.user_id = {$tid}";

Separate records with comma

I'm new to php and I want to separate the records with a comma.
Database:
the table in sql
I use this code to get the data:
<?php
$id = get_the_ID();
$sql = "SELECT *
FROM tablename
WHERE parent_id=$id";
$result = $conn->query($sql);
while($row = mysqli_fetch_array($result))
{
echo "Test: " . $row["value"]. "<br>";
}
mysqli_close($con);
?>
It return twice as:
Test: Test
Test: Test1
I want to separate the records with a ',' like this:
Test: Test, Test1
Store your values in an array and than implode with ",". You will get the result:
$id = get_the_ID();
$sql = "SELECT *
FROM tablename
WHERE parent_id=$id";
$result = $conn->query($sql);
$yourArr = array();
while($row = mysqli_fetch_array($result))
{
$yourArr[] = $row["value"];
//echo "Test: " . $row["value"]. "<br>";
}
echo "Test: ". implode(",",$yourArr);
You can do this entirely in MySQL using GROUP_CONCAT:
<?php
$id = get_the_ID();
$sql = "SELECT `parent_id`, GROUP_CONCAT(`value` SEPARATOR ', ') AS comma_separated_values
FROM tablename
WHERE `parent_id` = '$id'
GROUP BY `parent_id`";
$result = $conn->query($sql);
while ($row = mysqli_fetch_array($result))
{
echo 'Test: ' . $row["comma_separated_values"]; // Test: test, test2
}
mysqli_close($con);
?>
You should change the "comma_separated_values" name to something more appropriate for your data.
In your particular case, you would not need the while() loop either as we're limiting the MySQL to a single row.
If you were to remove the WHEREparent_id= '$id' from the SQL query, then you could return the results of multiple parent_id values. For example:
while ($row = mysqli_fetch_array($result))
{
echo 'Parent ' . $row['parent_id'] .': ' . $row["comma_separated_values"] . '<br>';
}
Would return:
Parent 292: Test1, Test2
Parent 293: Test3, Test4
Parent 294: Test5, Test10, Test50
etc...
you can use update query for this
$query="Update tablename set value=CONCAT(value,',test1') where parentid='295'";
$result=mysqli_query($conn,$query);

inner join query don't return any things

I have a php code that use a mySQL query in it.
tables users and conversation and main_profile have true values to select after this query but query return nothing.
$sqlQuer =
"SELECT `conversation`.from,`conversation`.text, `main_profile`.nik_name, `users`.id
FROM `conversation`
INNER JOIN `users` ON(`users`.username = `conversation`.to )
INNER JOIN `main_profile` ON(`users`.id = `main_profile`.user_id)
WHERE `conversation`.to = '".$to."'
AND `conversation`.read = '0'";
$result = mysql_query( $sqlQuer) or die (mysql_error());
while($row = mysql_fetch_array($result))
{
echo $row[0] . "#TEXT#" . $row[1]. "#TEXT#" . $row[2] . "#CON#";
$ids[] = $row['id'];
}
any Idea ?
Query was solved with this change that First INNER JOIN changed to LEFT JOIN and second changed to RIGHT JOIN.

Show Table Name From Query

I have a SELECT staement where I JOIN 2 separate tables.
$result = mysql_query('SELECT * FROM (SELECT * FROM gslil0009) as table1 UNION SELECT * FROM (SELECT * FROM gslil0028) as table2' . ' ORDER BY lname');
while($row = mysql_fetch_array($result)) {
echo $row['fname'];
}
How do I print out which table the row came from?
$result = mysql_query('SELECT *,'tbl1' FROM (SELECT * FROM gslil0009) as table1
UNION SELECT *,'tbl2' FROM (SELECT * FROM gslil0028) as table2'
. ' ORDER BY lname');
while($row = mysql_fetch_array($result)) {
echo $row['fname'];
echo $row['tbl1'];
}
I used the same solution as sel just add backslash to avoid error and added a name to the table names.
$result = mysql_query('SELECT *,\'table1\' AS tablename FROM (SELECT * FROM gslil0009) as table1
UNION SELECT *,\'table2\' AS tablename FROM (SELECT * FROM gslil0028) as table2'
. ' ORDER BY lname');
while($row = mysql_fetch_array($result)) {
echo $row['fname'];
echo $row['tablename'];
}
i couldn't add comment on his post.

Categories