i have the following code.
i already try'd some sulotions from the search option, but nothing helped.
all tables have a row called: user_id with data in it, but still the query gives false
The $user variable has the value 29 and in my tables look like this:
+---------+---------+-------+-----------+
+ id | user_id | jaar + kenmerk |
+---------+---------+-------+-----------+
+ 1 | 29 | 2015 + standaard |
+---------+---------+-------+-----------+
$query = "SELECT buitenklimaat.user_id, kasklimaat.user_id, watermangement.user_id, energie.user_id FROM buitenklimaat, kasklimaat, watermangement, energie WHERE user_id = ".$user." AND kenmerk = 'standaard' AND jaar = ".date("Y")."";
$result = mysqli_query($conn, $query);
if($result === false) {
echo 'Query failed';
die();
}
else {
// do something
}
If the field user_id exists in more than one table, you need to prefix the field with the table name in your where clause, too. Otherwise your database will not know in which table it should look for the user_id field. But even after you fixed this error the query will probably not return what you want it to. Have a look at some SQL tutorials that cover the join syntax.
$query = "SELECT buitenklimaat.user_id, kasklimaat.user_id, watermangement.user_id, energie.user_id FROM buitenklimaat, kasklimaat, watermangement, energie WHERE <TABLENAME MISSING>.user_id = ".$user." AND kenmerk = 'standaard' AND jaar = ".date("Y")."";
You may need to join all those tables together to get the user_id condition to work properly:
$query = "SELECT buitenklimaat.user_id, kasklimaat.user_id,
watermangement.user_id, energie.user_id FROM buitenklimaat, kasklimaat,
watermangement, energie
WHERE buitenklimaat.user_id = kasklimaat.user_id
AND buitenklimaat.user_id = watermangement.user_id
AND buitenklimaat.user_id = energie .user_id
AND buitenklimaat.user_id = ".$user." AND kenmerk = 'standaard'
AND jaar = ".date("Y")."";
Related
I want to get data from another table by just calling if the ID and data from another table is the same.
Table_1
Id | name | lastname
1 | Fred | Moore
Table 2
Id | table1_id
1 | 1
I can already get and store the table 1 id to my table 2, but i want to echo the name and lastname from the table 1.
e.g. if table 2 table1_id is equal to Table_1 Id it will print the name and lastname .
Try this query
select table_1.name, table_1.lastname
from table_1
left join on table_2 on table_1.id = table_2.table1_id
<?php
$a = "Table_1";
$b = "Table_2 ";
$allItem = $cxn->query("SELECT *
FROM $a
INNER JOIN $b
ON $a.id = $b.id
WHERE $a.id = 1);
$allItem = $allItem->fetchAll();
$lastName = $allItem["lastname"];
$name = $allItem["name"];
?>
Maybe it could work!
<?php
$conn = mysqli_connect("localhost","username","password","database_name");
if(!$conn){
echo "Database Connection Failed!";
}
$query1 = mysqli_query($conn,"SELECT table1_id,first_name,last_name FROM table_1")or trigger_error(mysqli_error($conn));
if(mysqli_num_rows($query1)){
while($row1 = mysqli_fetch_array($query1)){
$table1_id = $row1['table1_id'];
$table1_first_name = $row1['first_name'];
$table1_last_name = $row1['last_name'];
$query2 = mysqli_query($conn,"SELECT table2_id,table1_id,first_name,last_name FROM table_2 WHERE table1_id='$table1_id' ORDER BY first_name,last_name ASC")or trigger_error(mysqli_error($conn)); //check table1_id from table2 AND table1_id from table1 if matched
$row2 = mysqli_fetch_array($query2);
$table2_first_name = $row2['first_name'];
$table2_last_name = $row2['last_name'];
echo "First Name: ".$table2_first_name."<br>"; //print first name
echo "Last Name: ".$table2_last_name."<br>"; //print last name
}
}
else{
echo "No Result Found.";
}
?>
I have a table of which I must select information using a MySQLi Query and push it to the end of a PHP array.
The table is in this format:
table = friends
id user1 user1_id user2 user2_id datemade accepted
1 name1 1 name2 2 2015-05-27 03:24:32 1
2 name3 3 name2 2 2015-05-27 03:24:32 1
3 name3 3 name1 1 2015-05-27 03:24:32 1
4 name4 4 name2 2 2015-05-27 03:24:32 1
id = an auto_incrementing number to keep track of everything
user1 = the person's name that asks for friendship
user1_id = that person's special unique id
user2 = name of the person that accepts/decline's friendship
user2_id = that person's special unique id
datemade = the date it was made :P
accepted = did he accept? (don't worry about this)
I want to select all users that are friends with $u.
In this example, $u's id is 1 (name is name1).
After running the query it would push it to the end of friend_array.
So if I printed this array the output would be:
2, 3
Since, id=1 is friends with id=2 and id=3
What query should I do and how would I push that to an array (I know about about array_push but I do not know how to implement it)?
Please try this code. It will return the array for all user's friends.
$sql = "SELECT user1 AS user FROM friends UNION SELECT user2 AS user FROM friends";
$data = mysqli_query($sql);
while ($v = mysqli_fetch_assoc($data)) {
$sql = mysqli_query("SELECT * FROM `friends` where (user1 = '" . $v['user'] . "' or user2 = '" . $v['user'] . "')");
$arr = array();
while ($array = mysqli_fetch_assoc($sql)) {
if ($array['user1'] == $v['user']) {
$arr[$v['user']][] = $array['user2_id'];
} else {
$arr[$v['user']][] = $array['user1_id'];
}
}
}
I finally figured it out!
$sql = "SELECT COUNT(id) FROM friends WHERE user1_id='$log_id' AND accepted='1' OR user2_id='$log_id' AND accepted='1'";
$query = mysqli_query($db_conx, $sql);
$query_count = mysqli_fetch_row($query);
$friend_count = $query_count[0];
//echo($friend_count); //how many friends
if($friend_count < 1){
echo($u." has no friends yet");
} else {
$all_friends = array();
$sql = "SELECT user1_id FROM friends WHERE user2_id='$log_id' AND accepted='1' ORDER BY RAND()";
$query = mysqli_query($db_conx, $sql);
while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) {
array_push($all_friends, $row["user1_id"]);
}
$sql = "SELECT user2_id FROM friends WHERE user1_id='$log_id' AND accepted='1' ORDER BY RAND()";
$query = mysqli_query($db_conx, $sql);
while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) {
array_push($all_friends, $row["user2_id"]);
}
$friendArrayCount = count($all_friends);
}
So it first counts how many friends you have to check if you have any, if you do not a, temp. message appearing saying no friends. If you have some friends it will run a query to check what friends you have is both columns - user1_id and user2_id. It will then finally add everything to the $all_friends array and it then counts how many friends you have.
Thank you to everyone that helped!
I have two table, table2 has nameColumn that include name of table1 column like
table 1
id|check|status|...|
1 |abc |1 |...|
table2
|nameColumn|...|
|check |...|
|status |...|
I want get value in table1 with column name is value in table2 like
$sql="SELECT * FROM `table1` WHERE `id` = 1 ";
$result = mysqli_query($conn,$sql) or die (mysql_error ());
$rowTable = mysqli_fetch_array($result);
$s = "SELECT * FROM table2";
$r = mysqli_query($conn,$s);
while($row1 = mysqli_fetch_array($r)){
$columnName = $row1['nameColumn'];
$value = $rowTable["'".$columnName."'"]; // fail why?
//$value = $rowTable['check']; // working?
}
I don't know why $value = $rowTable["'".$columnName."'"]; fail the error like
Notice: Undefined index: 'check' ...
But if i using direct like $value = $rowTable['check']; // working why?
How to fix that thanks
its failing because your adding unnecessary ' char to it. try the code below
$value = $rowTable[$columnName];
$value = $rowTable['".$columnName."'];
I have a sql database with table "profile_values" (yes, it's a Drupal db), and I would like to pull the user's name, title and company id with one query. (I have done this in the past with three queries, but I'd like to do it in one.)
| fid | uid | value
| 4 | 100 | john
| 8 | 100 | doe
| 9 | 100 | web developer
| 22 | 100 | D3239GE77
I used different field id's (fid) to show that there can be field id's between the rows that I want to pull, but the field id will not change, so 4 will always be first name, 8 would be last name, 9 would be title, and 22 would be company id.
MY FIRST ATTEMPT:
$result = mysql_query("SELECT value FROM profile_values WHERE uid = '$uid' ORDER BY fid ASC");
while($row = mysql_fetch_array($result)) {
$userinfo[] = $row['value'];
$firstname = $userinfo[0];
$lastname = $userinfo[1];
$title = $userinfo[10];
$companyid = $userinfo[12];
}
But this method won't work, because, depending on the optional fields that users will have filled out, there may be more or less fields between.
I need a way to say, "In this array, if fid = 4, $firstname = value
$result = mysql_query("SELECT fid, uid, value FROM profile_values WHERE uid = '$uid' ORDER BY fid ASC");
$user = array();
while ($row = mysql_fetch_array($result)) {
switch ($row['fid']) {
case 4:
$key = 'firstname';
break;
case 8:
$key = 'lastname';
break;
case 9:
$key = 'title';
break;
case 22:
$key = 'company_id';
break;
default:
continue 2; // 2 required when using switch inside loop
}
$user[$key] = $row['value'];
}
I would try the following:
$result = mysql_query("SELECT fid, value FROM profile_values WHERE uid = '$uid' ORDER BY fid ASC");
while($row = mysql_fetch_assoc($result)) {
switch ((int)$row['fid']) {
case 4:
$firstname = $row['value'];
break;
case 8:
$lastname = $row['value'];
break;
case 9:
$title = $row['value'];
break;
case 22:
$companyid = $row['value'];
break;
}
}
Then, I would be using isset() to check whether the variables exists, e.g. isset($lastname)
Hope this helps
Here is an approach that do not require a switch statements or ifs. It's based on an array where you previously map the table's fids to the keys on your destination array:
$fieldMap = array(
4 => 'firstname',
8 => 'lastname',
9 => 'title',
22 => 'companyid'
);
$userinfo = array();
$result = mysql_query("
SELECT fid, value
FROM profile_values
WHERE uid = $uid
ORDER BY fid ASC
");
while($row = mysql_fetch_array($result)) {
$fieldKey = $fieldMap[$row['fid']];
$userinfo[$fieldKey] = $row['value'];
}
Below is my original suggestion, where we pivot the results to get a "regular" table format, instead of a property table format:
SELECT
p1.value AS first_name,
p2.value AS last_name,
p3.value AS title,
p4.value AS company_id
FROM profile_values p1
LEFT OUTER JOIN profile_values p2
ON p2.uid = p1.uid
AND p2.fid = 8
LEFT OUTER JOIN profile_values p3
ON p3.uid = p1.uid
AND p3.fid = 9
LEFT OUTER JOIN profile_values p4
ON p4.uid = p1.uid
AND p4.fid = 22
WHERE p1.uid = $uid AND p1.fid = 4
This is more expensive for SQL then the way you're currently doing it, but it will allow you to fetch data from multiple users more easily (just change p1.uid = $uid to, e.g., p1.uid IN (2, 6, 29).
This one's probably easy:
I have two variables:
$sender_id
$receiver_id
Those ID's are stored and assigned to a user in tblusers. I have no problem selecting one-at-a-time:
$data = mysql_query("SELECT * FROM tblusers WHERE usrID='$receiverID'") or die(mysql_error());
while($row = mysql_fetch_array( $data ))
{
echo $row['usrFirstName'];
echo $row['usrLastName'];
}
However, how would I select both rows (one for senderID and receiverID) so that I can gain access to further information on both those users. Sort of like a "SELECT within a SELECT".
Thanks!
SELECT * FROM tblusers WHERE usrID='$receiverID' or usrID='$sender_id'
EDIT: clarification
while($row = mysql_fetch_array( $data ))
{
if($row['usrID'] == $receiverID) {
echo "Receiver: " . $row['usrFirstName'] . " " . $row['usrLastName'];
} else {
echo "Sender: " . $row['usrFirstName'] . " " . $row['usrLastName'];
}
}
If you need to differentiate which is the receiver and which is the sender:
select
'Receiver' as UserType,
*
from
users
where
usrid = $receiver_id
union all
select
'Sender' as UserType,
*
from
users
where
usrid = $sender_id
This will return:
UserType | UsrID | Name
Receiver | 23 | John Smith
Sender | 42 | Adam Douglas
Of course, with two rows, you can always just compare the ID's to figure that out, too. This is mainly to make scaling easier if you have a larger result set than just two rows.
SELECT * FROM tblusers WHERE usrID IN ($receiverID, sender_id)