PHP postgresql: INSERT values from query array result - php

I have some rows from this query statement:
SELECT * from jkd where idno='$id';
And I want to "copy" the values from those rows to other table:
INSERT INTO tmpjkd(pop, name, address) VALUES($pop, $name, $address);
I tried with this:
$jkd = SELECT * from jkd where idno='$id';
$sql = pg_query("SELECT * from jkd where idno='$id'");
while ($row = pg_fetch_object($sql)){
$pop = $jkd['pop'];
$name = $jkd['name'];
$address = $jkd['name'];
pg_query("INSERT INTO tmpjkd(pop, name, address) VALUES($pop, $name, $address)");}
But there's no luck. Please help me...
UPDATE:
I wrote in drupals:
$sql = db_query("SELECT * from jkd where idno='$id'");
while ($row = db_fetch_object($sql)){
$pop = $jkd['pop'];
$name = $jkd['name'];
$address = $jkd['name'];
db_query("INSERT INTO tmpjkd(pop, name, address) VALUES($pop, $name, $address)");}

Your code needs some changes
$sql = pg_query("SELECT * from jkd where idno=$id");
while ($row = pg_fetch_object($sql)) {
$pop = $row->pop;
$name = $row->name;
$address = $row->address;
pg_query("INSERT INTO tmpjkd(pop, name, address) VALUES ('$pop', '$name', '$address')");
}
pg_fetch_object is returning an object not an array, beside you you were using $jkd which doesn't do anything with actual values.
Also, the first line $jkd = SELECT * from jkd where idno='$id';, I don't know what it is used for, mostly you don't really need it.

The solution proposed above does N + 1 queries. This will lead to performance problems. Why not using a CTE for that ?
$query = <<<SQL
WITH
to_be_inserted AS (SELECT pop, name, address FROM jkd WHERE idno = $1)
INSERT INTO tmpjkd (pop, name, address)
SELECT pop, name, address FROM to_be_inserted;
SQL;
pg_query_params($sql, [$id]);
Furthermore, this makes an atomic transaction.

Related

Get applicants from Table 1 and compare id and get user details from Table 2 Using PHP

Ok so I have two Tables
Applicant list - this shows all applicants
User Table
Now I'm Providing news_id by Post method and I want to list details of all users(email,mobile,username) where the value for user_authToken and user_authtoken is same. Can Someone help me out with this logic using PHP.
$job_id = $_POST['job_id'];
$resultSet = $con->query("SELECT appliers_list.news_id AS jobid ,
appliers_list.user_authToken AS user,
user.user_name AS username,
user.mobile AS mobile,
FROM appliers_list,user
WHERE appliers_list.news_id = '$job_id'
ORDER BY appliers_list.id DESC
");
$rows = $resultSet->fetch_assoc();
First of all, your naming is very inconsistent, it's hard to read and understand.
Second, please use prepare statement, otherwise you open your system to SQL injection.
$news_id = $_POST['job_id'];
$stmt = $con->prepare("SELECT email, mobile, user_name
FROM users
WHERE user_authtoken in (select user_authToken from appliers_list where news_id = ?)");
$stmt->bind_param("i", $news_id);
$stmt->execute();
$resultSet = $stmt->get_result();
while($row = $resultSet->fetch_assoc()) {
// data manipulation here
}
you can use left join to get record from both table :
$job_id = !empty($_POST['job_id']) ? intval($_POST['job_id']) : 0;
$resultSet = $con->query("SELECT appliers_list.*,users.email
FROM appliers_list
left join users on appliers_list.user_authToken = users.user_authToken
WHERE news.news_id = '$job_id'
ORDER BY news.id DESC
");
$rows = $resultSet->fetch_assoc();
You didn't specify a relationship between the user and appliers_list tables, so you're getting all rows in user. You also have an extra comma at the end of the SELECT list.
$job_id = $_POST['job_id'];
$resultSet = $con->query("SELECT appliers_list.news_id AS jobid ,
appliers_list.user_authToken AS user,
user.user_name AS username,
user.mobile AS mobile
FROM appliers_list
JOIN user ON appliers_list.user_authToken = user.user_authToken
WHERE appliers_list.news_id = '$job_id'
ORDER BY appliers_list.id DESC
");
$rows = $resultSet->fetch_assoc();

How to do Looping to Check Duplicate in MySQL with PHP

I want to make for each data exist on databases then do loop up to get unique data.
Here my code:
$id = rand(10000000,99999999);
$check_id = $db->prepare("SELECT * FROM sh_url WHERE sh_id='$id'");
$check_id->execute();
$count_id = $check_id->rowCount();
for ($count_id != 0) {
$lid = $id+1;
}
$shorturl = htmlentities(base_convert($lid,20,36));
$query = $db->prepare("INSERT INTO `sh_url`(`sh_id`) VALUES (:id)");
$query->bindParam(":id", $lid);
$query->execute();
Why don't you use distinct keyword to do this.
SELECT DISTINCT column1, column2, ...
FROM table_name;

PHP reverse stored procedures function?

lets say I have the following query in a variable:
$sql = "select id, salary, fname, lname from users where id = ? and salary > ?";
and an array like this:
$params = array (
0 => '38765',
1 => '4000');
I was wondering if there is a simple of built in PHP to do this to replace the "?" in the query to get the following result:
"select id, salary, fname, lname from users where id = '38765' and salary > '4000' ";
the query won't be executed by the way, this more of a sting manipulation question than a sql one.
this is as close as I got but it looks like PHP might have something built it for it:
foreach ($params as $param){
$pos = strpos($sql, '?');
$sql = substr_replace($sql, "'".$param."'", $pos, 1);
}
Yes, there is.
See this code example from php.net
$stmt = $dbh->prepare("SELECT * FROM REGISTRY where name = ?");
if ($stmt->execute(array($_GET['name']))) {
while ($row = $stmt->fetch()) {
print_r($row);
}
}
http://php.net/manual/en/pdo.prepared-statements.php#example-1016
But this variant requires that you work with PDO.
See this link for more details:
http://php.net/manual/en/book.pdo.php

PHP Fetch All Data Only 1 No

I want to fetch all users but not this user (id = 12) how to make this ?
$MyID = '12';
$sql_query = mysqli_query($Conn, "SELECT * FROM users WHERE country='$MyCountry'");
while ($fetch_data = mysqli_fetch_array($sql_query)) {
$firstname = $fetch_data['firstname'];
echo $firstname;
}
Well in it's simplest form:
$sql_query = mysqli_query($Conn, "SELECT * FROM users WHERE country='$MyCountry' and userid <> $MyID");
But this is inadvisable because your values are not being escaped properly. Better to use prepared statements or mysqli_real_escape_string
$stmt = $mysqli->prepare("SELECT * FROM users WHERE country = ? and userId <> ?")
$stmt->bind_param("sd",$MyCountry, $MyId);
$stmt->execute();
select * from user WHERE ID!=$mycountry;
You can do it easily like that
lets say your field is an id = 12
then
SELECT * FROM your_table WHERE id <> 12

My code returning incorrect values

In my PHP code I run the following statement:
$stmt = $pdo->prepare("SELECT * FROM `tb_services` WHERE tb_services.user_id = :var1 AND
(tb_services.user_id = :var1 OR tb_services.user_id = (SELECT subuser_owner_id FROM tb_users WHERE user_id = :var1))
AND tc_services.sub_id = :var2");
$stmt->bindParam(":var1", $_GET['var1']);
$stmt->bindParam(":var2", $_GET['var2']);
However doing $stmt->rowCount() returns 0, but when I run this exact statement(filled with the same values that I'm inputting instead of the :var1, :var2) it finds the row perfectly.
while($info = $stmt->fetch(PDO::FETCH_ASSOC)) {
The above is the code that is not running. As the return of the rowCount() is 0 and the while loop is not running, that just shows to me that it is for some reason not finding the row. If I input static values into the SQL statement, it works perfectly.
I have found the error. I was overlooking my code and saw the stupid mistake I made. For anyone out there that might experience a similar problem, the following MySQL statement:
$stmt = $pdo->prepare("SELECT * FROM `tb_services` WHERE tb_services.user_id = :var1 AND
(tb_services.user_id = :var1 OR tb_services.user_id = (SELECT subuser_owner_id FROM tb_users WHERE user_id = :var1))
AND tc_services.sub_id = :var2");
should be
$stmt = $pdo->prepare("SELECT * FROM `tb_services` WHERE(tb_services.user_id = :var1 OR tb_services.user_id = (SELECT subuser_owner_id FROM tb_users WHERE user_id = :var1))
AND tc_services.sub_id = :var2");
Removing the first tb_services.user_id = :var1 as that caused the error.

Categories