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.
Related
I'm just trying to get something basic to appear for now and it's not working. It should display 1 on the screen. Is my logic wrong? I paste my statement in console and get 1.
<?php
$sql = mysqli_query("Select moist_measure_avail from sigh_in_account where moist_measure_avail = '1'");
$result = mysqli_fetch_array($sql);
echo $result['moist_measure_avail'];
First of all you should have to create a connection
<?php
$conn = mysqli_connect("localhost "," root","","dbname");
?>
And then you have to include the conn variable in your query
$sql = mysqli_query( $conn, " SELECT moist_measure_avail from sigh_in_account WHERE moist_measure_avail = '1'");
The sql statements must be either all caps or all small
$sql = mysqli_query("Select moist_measure_avail from sigh_in_account where moist_measure_avail = '1'");
pass connection as first param in above method. something like this
$sql = mysqli_query($conn,"Select moist_measure_avail from sigh_in_account where moist_measure_avail = '1'");
where $conn is mysql connection
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')
I have one problem.I am fetching some data from MYSQL table.But there are some duplicate datas. I need to skip those duplicate data.I am explaining my code below.
session_start();
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
$colg_id=1;
$dept_id = $_SESSION["admin_dept_id"];
$user_id=$_SESSION["admin_id"];
$connect = mysqli_connect("localhost", "root", "******", "go_fasto");
$result = mysqli_query($connect, "select plan_id,unit_name from db_unit_plan where dept_id='".$dept_id."' and user_id = '".$user_id."' ");
while ($row =mysqli_fetch_assoc($result)) {
$data[] = $row;
}
print json_encode($data);
Here i need if any unit_name column has same type data then how to skip those rows.Please help me.
Change like this with DISTINCT
$result = mysqli_query($connect, "select DISTINCT unit_name,plan_id from db_unit_plan where dept_id='".$dept_id."' and user_id = '".$user_id."' ");
you have to specify the 'DISTINCT' keyword to get unique results from SQL.
so just try changing your select statement to $result = mysqli_query($connect, "select DISTINCT plan_id .. ");
good luck.
I have a MySQL database and I need a PHP to pull a random row. I have successfully created
$query = "SELECT * FROM $usertable
WHERE region='UK'
ORDER BY RAND() LIMIT 1";
This successfully randomly pulls a row; however, it is not limited to where region=2.
I need to be able to:
pull randomly when region=UK
pull randomly when region=UK or ##
(where ## is actually another region, for example, YK = Yorkshire)
Basically I need it to select rows randomly but ONLY when region=UK.
region is a label for one of my fields/collumns, and UK is the content of the VARCHAR in that for a number of rows.
I have the rest of the code sorted.
I have a simple database and the php as follows:
<?php
//Sample Database Connection Syntax for PHP and MySQL.
//Connect To Database
$hostname="carbonmarketing.db.9606426.hostedresource.com";
$username="MarketReadOnly";
$password="Read0nly1";
$dbname="carbonmarketing";
$usertable="ClientList";
$advertfooter = "advertfooter";
mysql_connect($hostname,$username, $password) or die ("<html>%MINIFYHTML4333ddb1f6ba50276851b9f9854a5c817%</html>");
mysql_select_db($dbname);
# Check If Record Exists
$query = "SELECT * FROM $usertable
WHERE region='UK'
ORDER BY RAND() LIMIT 1";
$result = mysql_query($query);
if($result)
{
while($row = mysql_fetch_array($result))
{
$advertfooter = $row["$advertfooter"];
echo "$advertfooter";
}
}
?>
But, it's just pulling randomly for all values of the region column
Let me know if it would help for you to see the database.
Make and array with your regions and implode them:
$region = array('UK', 'YK');
$implode = implode("', '", $region);
$query = "SELECT * FROM `".$usertable."` WHERE `region` IN ('".$implode."') ORDER BY RAND() LIMIT 1";
$query = "SELECT * FROM $usertable
WHERE region IN ('UK','YK')
ORDER BY RAND() LIMIT 1";
I was trying to get some details from MySql database, but i was working so long and tried so many ways that i am completely lost now :s
What i need to GET is two details which depend on information from 3 tables. I need to get $title and $siteurl (from table_sites) where current user did not click it before, AND siteurl owner still have remaining credits...
Here is my database:
USER:
id
username
password
credits
active
clicks
SITES:
id
userid
title
siteurl
clicks
active
weight
CLICKS:
id
siteid
byuserid
i tried with this MySql query:
include('db_con.php');
mysql_connect("$dbhost", "$dbusername", "$dbpassword")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$qrym = mysql_query("SELECT * FROM `users` WHERE username='$username' LIMIT 1") or die (mysql_error());
while($mem = mysql_fetch_object($qrym)){
$uid = $row->id;
}
$qrys = mysql_query("SELECT * FROM sites, clicks WHERE clicks.byuserid != '$uid' and sites.userid != '$uid' and sites.active = '1' ORDER BY sites.weight DESC, sites.id DESC LIMIT 1") or die (mysql_error());
if(mysql_num_rows($qrys)!=0){
while($row = mysql_fetch_object($qrys)){
$title = $row->title;
$siteurl = $row->siteurl;
echo "$title $siteurl";
}
} else {
echo "No more sites";
}
No errors, but whatever i try result is No more sites! How to JOIN this query correctly?
Maybe do
while($row = mysql_fetch_object($qrym)){
$uid = $row->id;
instead of
while($mem = mysql_fetch_object($qrym)){
$uid = $row->id;
You probably want a query like this:
SELECT [the columns you need]
FROM sites
LEFT JOIN clicks ON clicks.siteid = sites.id
AND clicks.byuserid = [current user id]
WHERE sites.active = 1 AND clicks.id IS NULL
ORDER BY sites.weight DESC, sites.id DESC
LIMIT 1
As gpojd noted above, you must must MUST sanitize your inputs before using them in a query. Fix your first query's code:
$qrym = mysql_query("SELECT * FROM `users`
WHERE username='" . mysql_real_escape_string($username) . "' LIMIT 1");
When fetching only a single row, as your first query does, there is absolutely NO need for a while() loop to retrieve the data.
That, and observe the comments in the code block:
$qrym = mysql_query("SELECT * FROM `users` WHERE username='$username' LIMIT 1") or die (mysql_error());
while($mem = mysql_fetch_object($qrym)){
^^^--- you fetch into $mem
$uid = $row->id;
^^^--- but try to retrieve from $row?
}
Try this instead:
$sql = "SELECT ...";
$qrym = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($qrym);
$uid = $row['uid'];