Jump to content

Help with MySQL query - inner join?


ramone_johnny

Recommended Posts

Hi guys,

I'm trying to pull a value from the database and assign it to a variable, of which I then need to populate another table.

 

My background is ASP/MSSQL so I'm not overly familiar with how to do this.

 

I have the following query, which is erroring out. I need help here.

$r=query_wrapper("SELECT pcode_area, pcode_postcode, pcode_city FROM tblnew_cities WHERE pcode_area = ?",$share_suburb . " AND pcode_postcode = ?",share_postcode);
$rstDBEdit=mysql_fetch_assoc($r);
$a = array();
$a["pcode_city"] = $pcode_city;

echo $pcode_city;
exit();

Then of course once I have the $pcode_city I need to insert it into the tblshare_adverts table. (see below)

$r=query_wrapper("SELECT * FROM tblshare_adverts");
$rstDBEdit=mysql_fetch_assoc($r);
$a = array();
$a["share_number"] = $share_number;
$a["share_street"] = $share_street;
$a["share_suburb"] = $share_suburb;

....etc etc

$a["share_city"] = $pcode_city;

...etc etc

query_wrapper("INSERT INTO tblshare_adverts SET ?%",$a);

$share_ID = mysql_insert_id();

I'm guessing this could be simplified by perhaps using an inner join and running just one query?

 

Or is this way okay?

 

Link to comment
Share on other sites

I'm not totally sure what you are trying to do.

It looks like you are inserting an array into your table?

 

Is there no other way to do this? I think you can better make separate fields in your table for the values you're inserting.

 

Besides of that, an insert query doesn't work with 'SET', you use this with an update query.

 

the proper syntax is

INSERT INTO table (field1, field2) VALUES (value1, value2)

I don't know if you can use it, but you have to figure out yourself how to use it in your situation then.

You can also use a select query inside the insert query, just google for how to use a subquery.

Edited by Ritual29
Link to comment
Share on other sites

I have the following query, which is erroring out.

 

 

it would sure help if you posted that error. if it concerns using an undefined constant, that's because you left off the $ in front of the share_postcode variable in the query statement.

 


 

all the logic you have after your mysql_fetch_assoc() statements is invalid. the $rstDBEdit variable contains the fetched array and you would reference individual elements of that array like - $rstDBEdit["pcode_city"].

 

also, php assignment statements assign the value on the right-hand side of the = to the variable on the left-hand side.

 


 

you can use an INSERT ... SELECT query to select data from one table and insert it into another table using one query - http://dev.mysql.com/doc/refman/5.6/en/insert-select.html

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.