I would like to show the results of one database table by the use of a variable fetched from another database table like this:
mysql_select_db($database_Connection, $Connection);
$query_Recordset_bids = "SELECT * FROM bids WHERE bidder = '$userName'";
$Recordset_bids = mysql_query($query_Recordset_bids, $Connection) or die(mysql_error());
while ($row_Recordset_bids = mysql_fetch_array($Recordset_bids)) {
$totalRows_Recordset_bids = mysql_num_rows($Recordset_bids);
mysql_select_db($database_Connection, $Connection);
$query_Recordset_jobs = "SELECT * FROM jobs WHERE userID = '".$row_Recordset_bids['jobID']."'";
$Recordset_jobs = mysql_query($query_Recordset_jobs, $Connection) or die(mysql_error());
$row_Recordset_jobs = mysql_fetch_assoc($Recordset_jobs);
$totalRows_Recordset_jobs = mysql_num_rows($Recordset_jobs);
}
And then I want the output showed in the following table:
<?php if($totalRows_Recordset_jobs == 0)
echo "You have never submitted a job offer!";
else {
?>
<table width="440" border="0" cellpadding="1" cellspacing="1" id="tablejobs">
<tr>
<th width="40" bgcolor="#779BDC" scope="col">ID</th>
<th width="90" bgcolor="#779BDC" scope="col">Destination</th>
<th width="85" bgcolor="#779BDC" scope="col">Cargo</th>
<th width="85" bgcolor="#779BDC" scope="col">Due Date</th>
<th width="75" bgcolor="#779BDC" scope="col">Bid</th>
<th width="65" bgcolor="#779BDC" scope="col">Status</th>
</tr>
<?php do { ?>
<tr>
<td height="22" bgcolor="#798890" scope="col"> <?php echo $row_Recordset_jobs['userID']; ?></td>
<td bgcolor="#798890" scope="col"> <?php echo $row_Recordset_jobs['destination']; ?></td>
<td bgcolor="#798890" scope="col"> <?php echo $row_Recordset_jobs['cargo']; ?></td>
<td bgcolor="#798890" scope="col"> <?php echo $row_Recordset_jobs['due_date']; ?></td>
<td bgcolor="#798890" scope="col"> <?php echo $row_Recordset_jobs['bids']; ?> kr.</td>
<td bgcolor="#798890" scope="col"> <?php echo $row_Recordset_jobs['status']; ?></td>
</tr>
<?php } while ($row_Recordset_jobs = mysql_fetch_assoc($Recordset_jobs)); ?>
</table>
<?php
}
?>
But there is only one row shown in the table even though there are 2 or more results that match the select query.
So how do I loop through the first database table to get multiple matching variables (jobID) that I can use to my select statement to the second database table, which should show multiple results?
I suggest you to simply learn about joins. :)
You can also use a join on different databases aslong as both databases are available with the same connection/credentials.
I am not sure if I got you right that you have two databases for your tables.
If you have only one database its simple:
$query = 'SELECT *
FROM bids b
LEFT JOIN jobs j ON b.jobID = j.UserID
WHERE b.bidder = "$userName"';
Incase you have two databases use this and insert names of your two databases for <namedb1> and <namedb2>.
However, note that this is not the smartest thing to do because you cannot use any indizes, transactions, constraints or table-locks over different databases.
(As mentioned by Jay Blanchard in the comments)
$query = 'SELECT *
FROM <namedb1>.bids b
LEFT JOIN <namedb2>.jobs j ON b.jobID = j.UserID
WHERE b.bidder = "$userName"';
http://dev.mysql.com/doc/refman/5.1/en/join.html
Related
The table I am looking to pull from my database is com/bzkItsK. It currently pulls the first row in the database but I am unsure about how to set up a script that will pull all the rows (currently 4) by their id to the webpage.
Here is the html as I have set it:
<table class="table table-striped">
<thead>
<tr>
<th>user id</th>
<th>First Name</th>
<th>Last Name</th>
<th>Department</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row"><?php echo $rows[$user_id];?></th>
<td><?php echo $rows[$first_name];?></td>
<td><?php echo $rows[$last_name];?></td>
<td><?php echo $rows[$dept];?></td>
</tr>
</tbody>
</table>
mysql_query is as such.
mysql_connect("localhost","?","?");
mysql_select_db("?");
$sql = mysql_query("SELECT * FROM users ORDER BY id ASC");
$id = 'id';
$user_id = 'user_id';
$first_name = 'first_name';
$last_name = 'last_name';
$dept = 'dept';
$rows = mysql_fetch_assoc($sql);
?>
I am trying to pull all 4 rows by id to be auto generated by a single table script.
you must iterate over your results.
$rows = mysql_fetch_assoc($sql);
will only fetch the first entry.
You have do something like this:
<?php while($rows = mysql_fetch_assoc($sql)): ?>
<tr>
<th scope="row"><?php echo $rows[$user_id];?></th>
<td><?php echo $rows[$first_name];?></td>
<td><?php echo $rows[$last_name];?></td>
<td><?php echo $rows[$dept];?></td>
</tr>
<?php endwhile; ?>
EDIT:
And please do not use mysql, use mysqli instead.
I have a table (MySQL) where items might be duplicated (room_number and menu_probelm of row x are the same as room_number and menu_probelm of row y). I am trying to only show row y if it is a duplicate of row x. The table has 4 rows with the same room_number and menu_probelm values but col3 (notes) is different. For some reason it is showing me the 1st duplicate row(y) but not the other two.
Here's my code:
$query_Recordset1 = "SELECT *, count(*) FROM damage GROUP BY room_number, menu_probelm HAVING count(*) > 1 ";
$Recordset1 = mysql_query($query_Recordset1, $hk) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
and here is the while statement inside my table:
<table width="80%" border="0" cellspacing="0" cellpadding="0">
<tr>
<th align="left" valign="top" scope="col">Record Number</th>
<th align="left" valign="top" scope="col">Room Number</th>
<th align="left" valign="top" scope="col">Room Number</th>
<th align="left" valign="top" scope="col">Error Found </th>
<th align="left" valign="top" scope="col">Delete</th>
</tr>
<?php do { ?>
<tr>
<td align="left" valign="top"><img src="../images/error2.png" width="25" height="25" alt="<?php echo $row_Recordset1['rid']; ?>"></td>
<td align="left" valign="top"><?php echo $row_Recordset1['room_number']; ?></td>
<td align="left" valign="top"><?php echo $row_Recordset1['menu_probelm']; ?></td>
<td align="left" valign="top"><?php echo $row_Recordset1['notes']; ?></td>
<td align="left" valign="top"></a><img src="../images/Remove.png" width="25" height="25" alt="Delete Record"></td>
</tr>
<?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
<tr>
<td colspan="2" align="left" valign="top"> </td>
<td colspan="3" align="left" valign="top"> </td>
</tr>
</table>
Here is my SQL Tabel
|rid (ai) | room_number |menu_problem| notes |
there are 5 records total. Three of them have the room 135 with problem carpet they all have different notes and it is only showing me the first 1 record note the other 2 and what I want is to not show the 1st record but only the 2nd and 3rd (the duplicates ).
I tried to put the whole table in here in ACSSI but it looked a mess and I tried a HTML tabel, but it would not show it.
There may be a problem with your SQL query. You are trying to do a SELECT * in your query, which is asking for all columns in the table. But since you are doing a GROUP BY you can only SELECT the following columns: room_number, menu_probelm, and the COUNT of each group.
Try this query instead:
SELECT room_number, menu_probelm, count(*) AS 'frequency'
FROM damage
GROUP BY room_number, menu_probelm
HAVING frequency > 1;
The reason you cannot SELECT all columns in GROUP BY is that the MySQL engine has no way of knowing which value of those other columns it should choose for that group.
I am using the following code to generate tables from data from my database. it groups the data by the employee. What I need is to display the totals for the last four columns at the end of each employees table. The Hours, OT, Travel and TOT columns.
Code:
$current_user_name = false;
while($row = mysql_fetch_array($result)) {
// listing a new employee? Output the heading, start the table
if ($row['user_name'] != $current_user_name) {
if ($current_user_name !== false)
echo '</table>'; echo '[divider_padding]';// if we're changing employee, close the table
echo '
<h5>'.$row['last_name'].', '.$row['first_name'].'</h5>[minimal_table]
<table>
<tr>
<th>Date</th>
<th class="tableleft">Description</th>
<th class="tableleft">Job</th>
<th class="tableleft">Activity</th>
<th class="tableleft">Comments</th>
<th class="tableright">Hours</th>
<th class="tableright">OT</th>
<th class="tableright">Travel</th>
<th class="tableright">TOT</th>
</tr>';
$current_user_name = $row['user_name'];
}
// output the row of data
echo '<tr>
<td style="width:75px">'.$row['labor_date'].'</td>
<td class="tableleft">'.$row['description'].'</td>
<td class="tableleft">'.strtoupper($row['job']).'</td>
<td class="tableleft">'.$row['activity'].'</td>
<td class="tableleft">'.$row['comments'].'</td>
<td class="tableright">'.$row['rthours'].'</td>
<td class="tableright">'.$row['othours'].'</td>
<td class="tableright">'.$row['trthours'].'</td>
<td class="tableright">'.$row['tothours'].'</td>
</tr>
';}
echo '</table>[/minimal_table]'; // close the final table
}
?>
I am stuck after trying some tests and can not figure this out.
This is the query gathering the data:
$result = mysql_query("SELECT * FROM data WHERE labor_date BETWEEN '$start' AND '$end' Order by last_name, labor_date ASC");
Any help would be appreciated.
This line is missing an open bracket:
if ($current_user_name !== false)
Should be:
if ($current_user_name !== false) {
I'm writing a code for my little admin panel, and since I'm not that advanced of a coder, I'm experiencing some troubles with getting a name using two different tables.
Here's my code so far:
<?php
session_start();
if(!session_is_registered(myusername)){
header("location:main_login.php");
}
include 'db_connect.php';
$sql = "SELECT * FROM $tbl_name WHERE is_dead='0'";
$result=mysql_query($sql);
?>
<title>Title</title>
<center><img src="header.png"></center>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td>
<table width="400" border="1" cellspacing="0" cellpadding="3">
<tr>
<? include 'menu.php';?>
</tr>
<tr>
<td align="center"><strong>ID</strong></td>
<td align="center"><strong>Unique ID</strong></td>
<td align="center"><strong>Model</strong></td>
<td align="center"><strong>Last Online</strong></td>
<td align="center"><strong>Options</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td><? echo $rows['id']; ?></td>
<td><? if ($rows['unique_id'] == 7815684) { echo '<font color="blue"><b><u>7815684</u></b></font>'; }
elseif ($rows['unique_id'] == 2312964) { echo '<font color="blue"><b><u>2312964</u></b></font>'; }
else { echo $rows['unique_id']; } ?></td>
<td><? echo $rows['model']; ?></td>
<td align='center'><font color="green"><b><? echo $rows['last_updated']; ?></b></font></td>
<td align="center">update
</tr>
<?php
}
?>
</table>
</td>
</tr>
</table>
So what I'm trying to do is to get user name, using two tables $tbl_name and $prtalbe using their unique_id. So, if unique_id from $tbl_name equals unique_id from $prtable, I want to show user's name from $prtalbe.
I've been trying another sql query:
$sql = "SELECT * FROM $tbl_name, $prtable WHERE $tbl_name.unique_id = $prtable.unique_id;
$result=mysql_query($sql);
Then doing while loop to get it working
while($rows=mysql_fetch_array($result)){
$rows['name'];
}
and it did work actually, but it didn't want to put it right into my code, since ID from $prtable and $tbl_name are different.
Try this:
$sql = "SELECT $prtable.username FROM $tbl_name INNER JOIN $prtable ON ($tbl_name.unique_id = $prtable.unique_id)";
When you call INNER JOIN you are fetching all rows from each table and combining them given the ON condition. For more information, see this: http://www.w3schools.com/sql/sql_join_inner.asp
I am building a social networking site that is dedicated to nightclubs and night events.
Among other tables, I have a users table, events table and establishments table.
I am really intrigued with how Facebook in particular is able to query and return matches of not just users but also pages, ads etc row after row. Im sure most who are reading this have tried the facebook search
My question is in my case, should I:
Perform 3 separate LIKE %search% on each of the tables on search.php.
Draw up 3 separate tables to show the results of what matches in the relevant queries which are collapsed when empty(on the same search.php) ie
In search.php
//query users table
$query_user = "SELECT user_first_name, user_last_name, username, picture_thumb_url, avatar FROM users JOIN picture ON users.user_id = picture.user_id
AND picture.avatar=1 ORDER BY users.user_id";
$result_users = mysql_query($query_user, $connections) or die(mysql_error());
$row_result_users = mysql_fetch_assoc($wid_updates);
//query events table
$query_event = "SELECT event_thumb_url, event_name, event_venue, event_date FROM event WHERE event_name LIKE '%".$search_value."%'";
$event = mysql_query($query_event, $connections) or die(mysql_error());
$row_event = mysql_fetch_assoc($event);
$totalRows_event = mysql_num_rows($event);
//query establishments table
$query_establishment = "SELECT establishment_thumb_url, establishment_name, location_id, establishment_pricing FROM establishment
WHERE establishment_name LIKE '%".$search_value."%'";
$establishment = mysql_query($query_establishment, $connections) or die(mysql_error());
$row_establishment = mysql_fetch_assoc($establishment);
$totalRows_establishment = mysql_num_rows($establishment);
My html:
<table max-width="500" name="users" border="0">
<tr>
<td width="50" height="50"></td>
<td width="150"></td>
<td width="150"></td>
<td width="150"></td>
</tr>
</table>
<table width="500" name="events" border="0">
<tr>
<td width="50" height="50"><a href="#profile.php"><img src="Images/<?php echo $row_event['event_thumb_url']; ?>"
border="0" height="50" width="50"/></a></td>
<td width="150"><?php echo $row_event['event_name']; ?></td>
<td width="150"><?php echo $row_event['event_venue']; ?></td>
<td width="150"><?php echo $row_event['event_date']; ?></td>
</tr>
</table>
<table width="500" name="establishments" border="0">
<tr>
<td width="50" height="50"><a href="#profile.php"><img src="Establishment_Images/<?php echo $row_establishment['establishment_thumb_url']; ?>"
border="0" height="50" width="50"/></a></td>
<td width="150"><?php echo $row_establishment['establishment_name']; ?></td>
<td width="150"><?php echo $row_establishment['location_id']; ?></td>
<td width="150"><?php echo $row_establishment['establishment_pricing']; ?></td>
</tr>
</table>
I haven't populated the PHP echo's for the user table.
This is just to give you an idea of what I am trying to do. Any assistance?
Id say there would be many different ways to go about this, however I would search all tables at once, and display the results in order of relevance. To be honest I have never needed to do this for more than two tables, but perhaps the following link will help you with your MySQL queries link text
Although this link is for full-text searching rather than the LIKE function