I am working on a php script that will get the required information and display it in an xml. For some reason my brin isn't work and I don't know how to do the query. The database has two tables that I want to pull information from. Both tables have one thing in common which is 'id_member'. Themes table has a bunch of junk in it - so it has 4 coulms - 'id_member', 'id_theme', 'varible', and 'value'. There are two items in 'varible' that I want to filter (show them and not the rest) "cust_armaus" and "cust_armamo" then value will show the value of course. So in the table Themes it will have id_member listed more than once to show the different varibles. This is what I got that isn't working for me:
<?php
header('Content-Type: text/xml');
$username="database_username";
$password="database_password";
$database="database_name";
mysql_connect('localhost',$username,$password);
#mysql_select_db($database) or die( "Unable to select database");
echo "<?xml version=\"1.0\"?>\n".
"<!DOCTYPE squad SYSTEM \"squad.dtd\">\n".
"<?xml-stylesheet href=\"squad.xsl?\" type=\"text/xsl\"?>\n";
?>
<squad nick="Team Tag">
<name>Team Name</name>
<email>contact#team.com</email>
<web>http://www.team.com</web>
<picture>teamlogo.paa</picture>
<title>This is the Team motto</title>
<?php
// smf_themes -> id_member =
// smf_themes -> variable for cust_armaus & cust_usermo
// smf_themes -> value <- get for cust_armaus & cust_usermo
// smf_members -> real_name = profile_name & name
// smf_members -> email_address = email - NO
$memberSQL = mysql_query("SELECT id_member AS id, real_name FROM smf_members");
$member = mysql_fetch_array($memberSQL);
$armausSQL = mysql_query("SELECT id_member, variable, value AS arma_id FROM smf_themes WHERE id_member='$member[id]' AND variable='cust_armaus'");
$armaid = mysql_fetch_array($armausSQL);
$armamoSQL = mysql_query("SELECT id_member, variable, value AS motto FROM smf_themes WHERE id_member='$member[id]' AND variable='cust_usermo'");
$armamo = mysql_fetch_array($armamoSQL);
$num=mysql_numrows($member, $armaid, $armamo);
mysql_close();
$i=0;
while ($i < $num) {
$profile_id = mysql_result($armaid,$i,"value");
$profile_name = mysql_result($member,$i,"real_name");
$profile_remark = mysql_result($armamo,$i,"value");
$profile_username = mysql_result($member,$i,"real_name");
//$profile_email = mysql_result($result,$i,"email");
//$profile_icq = mysql_result($result,$i,"icq");
echo "<member id=\"$profile_id\" nick=\"$profile_name\">" .
"<name>$profile_name</name>".
"<email>$profile_email</email>".
"<icq>$profile_icq</icq>".
"<remark>$profile_remark</remark>".
"</member>\n";
$i++;
}
?>
</squad>
The mysql interface is deprecated. New development should use either mysqli or PDO.
Assuming you only one one value of the cust_armaus and cust_usermo rows for each smf_members, you could use correlated subqueries in the SELECT list, and return just a single query that returns a single row for each row in smf_members. Something like this:
SELECT m.id_member AS id
, m.real_name
, (SELECT a.value
FROM smf_themes a
WHERE a.id_member = m.id_member
AND a.variable = 'cust_armaus'
ORDER BY a.value LIMIT 1
) AS cust_armaus
, (SELECT u.value
FROM smf_themes u
WHERE u.id_member = m.id_member
AND u.variable = 'cust_usermo'
ORDER BY u.value LIMIT 1
) AS cust_usermo
FROM smf_members m
ORDER BY m.id_member
If the (id_member,variable) tuple is guaranteed to be unique in smf_themes, you could use a simpler outer join:
SELECT m.id_member AS id
, m.real_name
, a.value AS cust_armaus
, u.value AS cust_usermo
FROM smf_members m
LEFT
JOIN smf_themes a
ON a.member_id = m.member_id AND a.variable = 'cust_aramus'
LEFT
JOIN smf_themes u
ON u.member_id = m.member_id AND u.variable = 'cust_usermo'
ORDER BY m.id_member
With either of those queries, you could use code something like this:
$sql = "SELECT ...";
$rs = $mysqli->query($sql)
if (!$rs) {
// handle error
echo 'query failed: (' . $mysqli->errno . ') ' . $mysqli->error;
die;
}
// loop through rows returned
while ( $row = $rs->fetch_assoc() ) {
echo '<member id="' . htmlspecialchars($row['id']) . '"'
. ' nick="' . htmlspecialchars($row['real_name']) . '">'
. '<cust_aramus>' . htmlspecialchars($row['cust_aramus']) . '</cust_aramus>'
. '<cust_usermo>' . htmlspecialchars($row['cust_usermo']) . '</cust_usermo>' ;
}
Related
I need to update an entire column with new unique phone numbers that live in a second table. I seem to be on the right track... but my loop logic is faulty.
I'm returning the matches correctly as far as I can tell, but when I try to update the entire column in the table it inserts the last phone number in every single row.
$query = "SELECT matched.duns, matched.new_p1, users_data.temp_duns
FROM matched
INNER JOIN users_data ON temp_duns
WHERE temp_duns = duns LIMIT 10";
$result = mysqli_query($connection, $query);
foreach ($result as $key => $val) {
if($val['duns'] === $val['temp_duns']) {
$final_query = "UPDATE users_data SET phone_number = " . $val['new_p1'];
$final_result = mysqli_query($connection, $final_query);
echo $counter . "DUNS From matched: " . $val['duns'] . " DUNS From users_data: " . $val['temp_duns'] . " NEW PHONE: ". $val['new_p1']. "<br>";
}
}
I'm a total newb but any help would be appreciated.
Simply run an update query in one call without looping and in MySQL INNER JOIN can be used:
UPDATE user_data u
INNER JOIN matched m ON u.temp_duns = m.temp_duns
SET u.phone_number = m.new_p1;
Or for the limit of 10:
UPDATE user_data u
INNER JOIN (SELECT * FROM matched LIMIT 10) m ON u.temp_duns = m.temp_duns
SET u.phone_number = m.new_p1;
The problem is that your query doesn't have a WHERE clause, so it's updating every row.
"UPDATE users_data SET phone_number = " . $val['new_p1'];
You need to limit it to just the row that you want to update.
"UPDATE users_data SET phone_number = " . $val['new_p1'] . " WHERE some_id = " . $some_id;
You could probably do the whole thing in a single query.
UPDATE table_to_be_updated
JOIN table_with_value_we_need ON whatever_joins_them
SET table_to_be_updated.column_we_want_to_fill = table_with_value_we_need.value_we_need;
I am trying to get data from two tables:
$con = getDbConnect();
$edit = $_GET['edit'];
if (mysqli_connect_errno($con)) {
"Failed to connect to MySQL: " . mysqli_connect_error();
} else {
$result = mysqli_query($con, "SELECT * FROM admininfo where email='" . $edit . "'");
$results = mysqli_query($con, "SELECT * FROM adminaccount where email='" . $edit . "'");
while ($admininfo = mysqli_fetch_array($result, $results)) {
You use a single query no matter how many tables you're selecting from, e.g.
SELECT a.*, b.* FROM table1 a, table2 b where a.id = ? AND b.id = ?
Then you fetch what you need from the result, take a look at the docs for that.
-- and, of-course, don't use select *.
I have a php code that use a mySQL query in it.
tables users and conversation and main_profile have true values to select after this query but query return nothing.
$sqlQuer =
"SELECT `conversation`.from,`conversation`.text, `main_profile`.nik_name, `users`.id
FROM `conversation`
INNER JOIN `users` ON(`users`.username = `conversation`.to )
INNER JOIN `main_profile` ON(`users`.id = `main_profile`.user_id)
WHERE `conversation`.to = '".$to."'
AND `conversation`.read = '0'";
$result = mysql_query( $sqlQuer) or die (mysql_error());
while($row = mysql_fetch_array($result))
{
echo $row[0] . "#TEXT#" . $row[1]. "#TEXT#" . $row[2] . "#CON#";
$ids[] = $row['id'];
}
any Idea ?
Query was solved with this change that First INNER JOIN changed to LEFT JOIN and second changed to RIGHT JOIN.
I need to grab a result from one query and pop it into another.
first query
$query = 'SELECT * FROM singleprop.jos_mls WHERE MSTMLSNO = ' . $mlsnum . ';';
$result = mysql_query($query);
$row = mysql_fetch_row($result);
second query
$aquery = 'SELECT * FROM singleprop.jos_agents WHERE AGTBRDIDMM = ' . $row[0] . ';';
$aresult = mysql_query($aquery);
$agent = mysql_fetch_row($aresult);
I know about JOIN, but don't know how to apply it with a 3rd table. Does my model have something to do with $this->?
The code looks good. You could write a query using join, which you are aware of. What is the question?
SELECT *
FROM singleprop.jos_mls as mls JOIN singleprop.jos_agents
ON singleprop.jos_mls.KEY = singleprop.jos_agents.KEY
WHERE mls.MSTMLSNO = $mlsnum
where KEY is the join key
OR
SELECT *
FROM singleprop.jos_agents
WHERE AGTBRDIDMM = (
SELECT COL_NAME
FROM singleprop.jos_mls
WHERE MSTMLSNO = ' . $mlsnum . '
)
where COL_NAME is the column name for AGTBRDIDMM in the first table
I have this query:
$query = "SELECT ads.*,
trafficsource.name AS trafficsource,
placement.name AS placement,
advertiser.name AS advertiser,
country.name AS country
FROM ads
JOIN trafficsource ON ads.trafficsourceId = trafficsource.id
JOIN placement ON ads.placementId = placement.id
JOIN advertiser ON ads.advertiserId = advertiser.id
JOIN country ON ads.countryId = country.id
WHERE advertiserId = '$advertiser_id'";
and ads table
ads Table
ad_id PK
size
price
trafficsourceId FK
placementId FK
advertiserId FK
countryId FK
For getting data I'm using
$result = mysql_query($query) or die('Invalid query: ' . mysql_error());
while ($row = mysql_fetch_assoc($result)) {
}
I cant figure out how I need to print page so that it's not looking like rows but also need id's of for example trafficsource name. I want to make something like that:
EDITED:
<div id="adscontent">
<h1>Advertiser:</h1> Advertiser name
<h2>Traffic Sources:</h2> Company1, Company2, Company 3
<h2>Placements:</h2> Like: Newspaper, radio, website, bla bla
</div>
Thanks
You will need to play around with the printout but I think something like this will work:
$results = array();
while ($row = mysql_fetch_assoc($result)) {
$results[$row['advertiser']]['countries'][] = $row['country'];
$results[$row['advertiser']]['trafficsources'][] = $row['trafficsource'];
$results[$row['advertiser']]['placements'][] = $row['placement'];
}
// And now print the data
foreach ($results as $arvertiser => $data)
{
echo "<h1>{$advertiser}</h1>";
// Print Placements
echo "Placements: " . implode(", ", $data['placements']) . '<br />;
// Print Countries
echo "Countries: " . implode(", ", $data['countries']) . '<br />;
// Print Placements
echo "Traffic Sources: " . implode(", ", $data['trafficsources']) . '<br />;
}
EDIT: If you need to add the IDs you will need to change your select to:
$query = "SELECT ads.*,
trafficsource.name AS trafficsource,
trafficsource.id AS trafficsourse_id,
placement.name AS placement,
placement.id AS placement_id,
advertiser.name AS advertiser,
advertiser.id AS advertiser_id,
country.name AS country
country.id AS country_id
FROM ads
JOIN trafficsource ON ads.trafficsourceId = trafficsource.id
JOIN placement ON ads.placementId = placement.id
JOIN advertiser ON ads.advertiserId = advertiser.id
JOIN country ON ads.countryId = country.id
WHERE advertiserId = '$advertiser_id'";
From then on you can include this information in the $results array like so:
$results[$row['advertiser']['countries'] = array(
'id' => $row['country_id'],
'value' => $row['country')
);
and print out whatever you need from there.