Tryning to get the right information from mysql - php

I'm trying to call the correct mysql information.
In my first query, I check if the order is available and take the post_id so I can make a SELECT that includes all post_id's for my while.
In the first query is there many post_id's so I need to connecte tto many id's
An example that does not work but explain what I mean:
$query103 = mysqli_query($conn, "SELECT * FROM `$v1`.`wpd2_posts`
where ID='order_id1' and ID='order_id2' and ID='order_id3'
") or die(mysqli_error($conn));
The $orderidtilsoog can be more IDs and not just 1
Hope you understand me
The is the completaly code:
$query = mysqli_query($conn, "SELECT * FROM `$v1`.`wpd2_postmeta` where meta_value='$emailconvert' ORDER BY meta_id ") or die(mysqli_error($conn));
echo "Kunden er fundet og ligger på https://v1.com";
while($row55 = mysqli_fetch_array($query)) {
$orderidtilsoog = $row55['post_id']. ",";
echo $orderidtilsoog;
if(mysqli_num_rows($query) == '0') {
}else{
$query103 = mysqli_query($conn, "SELECT * FROM `$v1`.`wpd2_posts` where ID
IN ('$orderidtilsoog') ") or die(mysqli_error($conn));
}

Just use SELECT * FROM $v1.wpd2_posts where ID IN('$orderidtilsoog')

Related

Simple and Efficient Code to Count Data in PHP with MySQL

So I think there's will be a simple and efficient code to count data with PHP and get the data output from different MySQL tables. This implementation is used to display how much data in my dashboard page.
But I prefer to use PHP procedural style because I'm still learning.
So here is my dirty code:
$count_admin = mysqli_query($conn, "SELECT COUNT(*) AS count_admin FROM employee WHERE level_id = 1");
$count_officer = mysqli_query($conn, "SELECT COUNT(*) AS count_officer FROM employee WHERE level_id = 2");
$count_goods = mysqli_query($conn, "SELECT COUNT(*) AS count_goods FROM goods");
$count_customer = mysqli_query($conn, "SELECT COUNT(*) AS count_customer FROM customer");
$row_admin = mysqli_fetch_assoc($count_admin);
$row_officer = mysqli_fetch_assoc($count_officer);
$row_goods = mysqli_fetch_assoc($count_goods);
$row_customer = mysqli_fetch_assoc($count_customer);
echo $row_admin['count_admin'];
echo $row_officer['count_officer'];
echo $row_goods['count_goods'];
echo $row_customer['count_customer'];

Get results and use them to another query mysql php

How can I use the fetched result to use the values to make another query? I tried to find info on php.net, but can`t figure out.
$sql = "SELECT id FROM orders WHERE customer_id=$customer_id";
$query = mysqli_query($conn, $sql);
while($row= mysqli_fetch_array($query)) {
$ordersid=$row['id'];
}
$ordersid returns: 13 - order number 1 and 3.
Here is my difficulty. How can I make $orderid(1,3)?
After that I want to use 1 and 3 like that in another query:
SELECT * FROM orderdetails WHERE order_id IN ($orderid)
In that way without direct relation will have all answers from the first query to second.
Where is my mistake?
You could put the results into an array and use like follows:
$a = array();
$sql = "SELECT id FROM orders WHERE customer_id=$customer_id";
$query = mysqli_query($conn, $sql);
while($row= mysqli_fetch_array($query)) {
array_push($a, $row['id']);
}
$data = implode("', '", $a);
And in your next query like so:
SELECT * FROM orderdetails WHERE order_id IN ('$data')

PHP - Like limit for posts not working

So far, I am trying to limit the user from liking a certain post more than once. The problem is that when I click "like" on a certain post, the like count will go up every time I click the link, even though it should be limited to just one.
This is the user.php file:
echo "<a href='likes.php?id=$row[0]'>$row[6]</a>";
The "id=$row[0]" indicates to the id_post column in the database. "$row[6]" is the column in the database which shows the like count.
Here is the likes.php file:
<?php
include 'db.php';
connect();
$id = $_GET['id'];
$sql1 = "SELECT * FROM posts";
$result1 = mysqli_query($link, $sql1) or die(mysqli_error($link));
$row = mysqli_fetch_row($result);
if ($row[6] == 0) {
$sql2 = "UPDATE posts SET likes = likes + 1 WHERE id_post = '$id'";
$result2 = mysqli_query($link, $sql2) or die(mysqli_error($link));
}
if ($row[6] == 1) {
exit(header("Location: user.php"));
}
header("Location: user.php");
?>
What's the problem with my code?
SELECT * FROM posts
Your query is selecting all rows from the posts table, and not filtering based on the ID in your $id variable.
Try changing your query to:
SELECT * FROM posts WHERE COLUMN_NAME = '$id'
This way $row[6] will refer to the correct ID in your posts table.

Displaying a SQL table data for a specific user

I want to display the table participantes with the columns sorteo, nombre and fecha.
The user info is on another table sellify_users (usern column).
I want to display only that user data using:
SELECT * FROM participantes WHERE nombre = 'usern'
But usern is not in the same table, so if possible I want to call the sellify_users to get the usern data.
<?php
$user = 'database_user';
$password = 'database_pass';
$database="database_name";
mysql_connect(localhost,$user, $password);
#mysql_select_db($database) or die( "Unable to select database");
echo $query = "SELECT * FROM participantes WHERE nombre='usern'";
$result = mysql_query($query);
mysql_close();
?>
If you meant that a user has a record in the table sellify_users and you need to find the usern from it in order to use it in the next query:
$query = "SELECT * FROM participantes WHERE nombre='usern'";
Then all you need to do is to run a query for the table sellify_users first and get the value usern from it, store it in a variable and then use that in your second query. Something like:
$query1 = "SELECT * FROM sellify_users";
$result1 = mysqli_query($con, $query1);
$row1 = mysqli_fetch_assoc($result1);
$usern = $row1['usern'];
$query2 = "SELECT * FROM participantes WHERE nombre='usern'";
$result2 = mysqli_query($con, $query2);
while($row2 = mysqli_fetch_assoc($result2)){
echo $row2['ColumnNameHere1'];
echo $row2['ColumnNameHere2'];
}
Notice: I've passed $con which denotes a connection variable, i.e.
you should read about mysqli or PDO and if there's anything you do not understand, feel free to shoot a query.
EDIT:
If by any chance you are trying to use the last inserted record, you should look for mysqli_insert_id($con); and use that instead.

PHP count from database WHERE

I am trying to show a count of all applications stored in a database with the status of 1.
Here is my UPDATED code:
$result=mysql_query("SELECT * FROM member ")or die('You need to add an administrator ' );
$counter = mysql_query("SELECT COUNT(*) as personID FROM member where state='1' ");
$row = mysql_fetch_array($result);
$personID = $row['personID'];
$num = mysql_fetch_array($counter);
$countadmin = $num["personID"];
However this isn't showing anything when I echo `$countadmin'
Can anyone help
You may try this
$query = mysql_query("select count(*) from member where state='1'");
if ($query) {
$count = mysql_result($query, 0, 0);
echo $count;
}
Check mysql_result and also notice the Warning at top. Also make sure that, the field is state not status, it's confusing, you mentioned status in your question but used state in the query.
Also, following line is not required to get the count of your second query
$result=mysql_query("SELECT * FROM member ")or die('You need to add an administrator ' );
Also, make sure that, you have connected to a database and selected it.
You try to readout "ID", but you select COUNT as "personID"
You have two options here;
$result = mysql_query("SELECT * FROM `member` WHERE `Status`='1'");
$num_rows = mysql_num_rows($result);
or
$result = mysql_query("SELECT COUNT(`Status`) FROM `member` WHERE `Status`='1'");
while($row = mysql_fetch_array($result)){
$Count = $row['count(Status)'];
}

Categories