Mysql Select Multiple Table Get Value with same Column - php

Okay that was harsh,
Anyways, I revised my question and made an example of code here.
What I wanted to do is, I need to get the giveaway table's column title value and competition column title value
$query = "SELECT giveaway_table.title, competition.title FROM giveaway_table, competition WHERE giveaway_table.status=competition.status";
$result = db_query($query)->fetchObject();
How do I retrieve the value?
When I used this
echo $result->title;
it only echo the competition's title value.
How do I retrieve the giveaway table's title column value?
I used this
$result->title[0]
it only shows the First letter of the Title of Competition's title value.
Any help would be appreciated. :)

Use different alias:
$query = "SELECT giveaway_table.title as gtitle, competition.title as ctitle FROM giveaway_table, competition WHERE giveaway_table.status=competition.status";

You could use aliases to clear up the ambiguity:
$query = "SELECT giveaway_table.title AS giveaway_title, competition.title AS competition_title FROM giveaway_table, competition WHERE giveaway_table.status=competition.status";
$result = db_query($query)->fetchObject();
Then echo:
echo $result->competition_title;
echo $result->giveaway_title;

Use aliases
SELECT
giveaway_table.title AS GiveAwayTitle,
competition.title AS CompetitionTitle
FROM giveaway_table,
competition
WHERE giveaway_table.status = competition.status
With php
echo $result->GiveAwayTitle;
echo $result->CompetitionTitle;

Related

PHP MySQLi query not returning value

I've tried to get this to work in several ways, but I can't get this query to return the Cost value from the database to the $cost variable:
$query2 = "SELECT Cost FROM 'item' WHERE Item = '$item'";
$cost= $db->query($query2);
It seems to be empty when I try to echo it.
(The $item variable is selected from a dropdown list generated from the item-table in the mySQL-db. This works fine and if I echo the value from $item, it returns the name of the item as expected.)
Anybody sees what I'm doing wrong?
I could post my complete code if necessary, but I believe this explanation may be sufficient.
You have to declared 'fetch_array' and Make reference this URL : http://php.net/manual/en/mysqli-result.fetch-array.php
For Example:-
$query = "SELECT Name, CountryCode FROM City ORDER by ID LIMIT 3";
$result = $mysqli->query($query);
$row = $result->fetch_array(MYSQLI_NUM);
Change $query2 = "SELECT Cost FROM 'item' WHERE Item = '$item'"; to
$query2 = "SELECT Cost FROM item WHERE item = '".$item."'";
Assuming Cost is your column name, the first item is your table name, the second item is another column name (Please change your naming convention and not use the same name for table and column). To make your question clearer, please provide us with your table name, your column names so that we can give you a more accurate answer.
Also you will need to escape your $item variable by using '".$item."' so that you can query from your database proper using that variable.
You will then need to fetch the results from querying the database.
$results = $db->fetch_all($cost);
To test that the data were successfully fetched, you can test it by printing it using print_r($results); This should return you an array of the results.
This might help you.
$input = "100";
$query = "select sal from sal where sal='$input'";
$result = mysql_query($query);
$row = mysql_fetch_row($result);
//This will print sal
echo $row['sal'];
Please let me know if you've any questions.
$query=mysql_query("SELECT Cost FROM item WHERE Item = '$item'");
while($x=mysql_fetch_array($query))
{
$cost=$x['Cost'];
echo $cost; //here we can return the result
}

PHP: MYSQL select inside another MYSQL select?

I'm trying to place one mysql select inside another one and combine the results to be displayed.
this is my code:
$allattrs = "";
$sql69 = "SELECT * FROM product_details";
$query69 = mysqli_query($db_conx, $sql69);
$login_check69 = mysqli_num_rows($query69);
if($login_check69 > 0){
while($row69 = mysqli_fetch_array($query69, MYSQLI_ASSOC)){
$FID = $row69["id"];
$sql2s = "SELECT * FROM ATTRIBUTES WHERE id='$FID'";
$query2s = mysqli_query($db_conx, $sql2s);
$login_check2s = mysqli_num_rows($query2s);
if($login_check2s > 0){
while($row2s = mysqli_fetch_array($query2s, MYSQLI_ASSOC)){
// Get member ID into a session variable
$Sid = $row2s["id"];
$attr = $row2s["attr"];
$allattrs .= ''.$attr.', ';
}
}
$product_list .= '<tr>
<td>'.$allattrs.'</td>
</tr>';
}
}
The problem i'm having is that the $allattrs returns the values but it will put everthing together.
for example:
if one attr column in mysql database has apples, and another one has oranges, when i see the results of $allattrs on my PHP page i see this:
id 1 - apples
id 2 - apples, oranges
id 3 - apples, oranges, apples, oranges
etc etc
this is in fact wrong as each attribute value needs to stay true to their own id and product_details id field!
I'm not sure what I am doing wrong to cause this.
could someone please advise on this issue?
any help would be appreciated.
The right way to write your query is using JOIN, EXISTS, or IN. I think you would find this most natural:
SELECT a.id, GROUP_CONCAT(a.attr) as attrs
FROM ATTRIBUTES a
WHERE a.id IN (SELECT id FROM product_details)
GROUP BY a.id;
This replaces a bunch of your code.
Looks like you are only interested in the the attributes then try this out instead of the first sql:
SELECT * FROM ATTRIBUTES where id IN (SELECT id FROM product_details)
You need to set $allattrs to an empty string inside your first while loop, instead of only once before.
Apart from that, you should look into the following two topics: Normalization and JOINs.

PHP SQL Select From Where

I am having some difficulty running some SQL code.
What I am trying to do is, find a row that contains the correct username, and then get a value from that correct row.
This is my SQL in the php:
mysql_query("SELECT * FROM users WHERE joined='$username' GET name")
As you can see, it looks for a username in users and then once found, it must GET a value from the correct row.
How do I do that?
You need some additional PHP code (a call to mysql_fetch_array) to process the result resource returned by MySQL.
$result = mysql_query("SELECT name FROM users WHERE joined='$username'");
$row = mysql_fetch_array($result);
echo $row['name'];
mysql_query("SELECT `name` FROM users WHERE joined='$username' ")
Just select the right column in your 'select clause' like above.
Edit: If you are just starting out though, you might want to follow a tutorial like this one which should take you through a nice step by step (and more importantly up to date functions) that will get you started.
mysql_query("SELECT name FROM users WHERE joined='$username'")
$q = mysql_query("SELECT * FROM users WHERE joined='$username'");
$r = mysql_fetch_array($q);
$name = $r['user_name']; // replace user_name with the column name of your table
mysql_query("SELECT name FROM users WHERE joined='$username' ")
Read documentation : http://dev.mysql.com/doc/refman/5.0/en/select.html

MYSQL get the name from another table that is associated with the first table

I can't figure out why this statement is not working
$sql2 = mysql_query("
SELECT myChurches.id AS id, myChurches.church_name AS church_name
FROM myChurches
INNER JOIN church_staff
ON church_staff.church_id=myChurches.id
WHERE church_staff.mem_id='$logOptions_id'
ORDER BY myChurches.church_name
ASC
")
if(mysql_num_rows($sql2) > 0){
while($row2 = mysql_fetch_array($sql)){
$church_id = $row2['id'];
$church_name = $row2['church_name'];
$options .= '<option value="'.$church_id.'">'.$church_name.'</option>';
}
}
Basically I need to find the person's that are staff members of a church from one table and want to get the 'name' of that church FROM the 'myChurches' table. Hopefully that makes sense. Thanks in advance
EDIT:
Table 1 has a unique id and church_name
Table 2 has unique id, church_id, and mem_id
SELECT id, church_name
FROM myChurches
WHERE id = (SELECT church_id FROM church_staff WHERE mem_id = '$logOptions_id')
EDIT: this assumes that mem_id is unique as you said, but church_id should not be unique, unless you are doing something wrong (as it is a foreign key)
The problem was in the fetch array... The variable was different to the actual query variable. Thanks for the help

MySQL PHP Query placed within another query's while loop failing

I have a column of pony names, where the breeder's Prefix is included with the pony's name (eg. Ashbrook Boy, where Ashbrook is the Breeder's Prefix, and Boy is the name of the pony). I have another table where I have a list of all the Prefixes used. I want to cycle through that list, and for each record search through my ponies and fetch those whose names begin with each prefix in turn. When they are fetched, I want to remove the said prefix from their name, and pop it into a column for that purpose on their own table.
In the end, I want -rather than one column with both Prefix and Name mixed in - two columns: one for Prefix, one for Name.
I thought the code below would do it for me, but it's not working. I get a 'not a valid resource' error for $res. Any help you could give me would be hugely appreciated - I really don't want to do this by hand! :P
I'm using a PHP script off a MySQL db, which I can access via PHPMyAdmin.
include '../conn.php';
$q=mysql_query("SELECT DISTINCT Pre FROM prefixes");
while($r=mysql_fetch_array($q)) {
$pre=$r['Pre'];
$sql="SELECT ID, Name FROM profiles WHERE (Name REGEXP '^$pre') ORDER BY ID ASC";
mysql_query($sql);
echo $sql;
while($res=mysql_fetch_array($sql)){
$name=$res['Name'];
$name=trim(str_replace("$pre","", $name));
$id=$res['ID'];
mysql_query("UPDATE profiles SET Prefix = '$pre', Name = '$name' WHERE ID = '$id' ");
}
}
mysql_close($con);
Your mistake come from the fact that $sql is the query string, not the mysql result of the query
$sql="SELECT ID, Name FROM profiles WHERE (Name REGEXP '^$pre') ORDER BY ID ASC";
mysql_query($sql);
echo $sql;
while($res=mysql_fetch_array($sql)){
with this, it will look better :
$sql="SELECT ID, Name FROM profiles WHERE (Name REGEXP '^$pre') ORDER BY ID ASC";
$result = mysql_query($sql);
while($res=mysql_fetch_array($result)){

Categories