Jump to content

Recommended Posts

Hi guys,

 

I have two tables where users can update their informations

 

tables 1 is user which includes (users)

 

id  username password branch1 branch2

 

and next table is (branch)

 

id branchname  branchpostcode

 

so far i have two branches with postcodes and I have been able to add the branches via drop down list from mysql in user table as below:

 

id  username password  branch1  branch2

1  me          dfdsfds      1            2

 

the branches store the branch id in the field,

 

what i need to do is to join the tables to echo relevant postcode information for entered for users, so if i have branch1 value set to 1 then I need to connect this to my branch table and retrieve the postcode information,

 

I came up with the code below but it gives me error

 

$findpostcode1=mysql_query("SELECT users.branch1, branch.id, FROM users, branch WHERE users.branch1=branch.id");
while($row = mysql_fetch_array($findpostcode1))
{
$findpostcode1 = $row['postcode'];
echo "your postcode: $findpostcode1";
}

}


 

but i recieve  this error "Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource"

 

can you help me with this please? I have googled every possible thing but as im a newbie maybe there are some techniques which i dont know.

 

thank you in advance

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/220297-mysql-error-in-php/
Share on other sites

yup..i notice this

 

$findpostcode1=mysql_query("SELECT users.branch1, branch.id, FROM users, branch WHERE users.branch1=branch.id");

 

if you wish to join two tables then this is the format

 

$query=mysql_query("SELECT table1.* FROM table1 JOIN table2 ON table1.user_id=table2.user_id

WHERE table1.column='blablabla'");

 

here is the explanation

"SELECT table1.*" - If you wish to get the whole records from table1

"table1 JOIN table2" - You join both tables table1 and table2

"ON table1.user_id=table2.user_id" - if both tables have user_id and you wish to connect both tables based on user_id

"WHERE table1.column='blablabla" is the condition you wish to have.

 

Hope that helps :)

 

Link to comment
https://forums.phpfreaks.com/topic/220297-mysql-error-in-php/#findComment-1141631
Share on other sites

There are many formats to join 2 tables together.  There is NATURAL, STRAIGHT, INNER, OUTER, LEFT, RIGHT joins.  Although many work similar, they all have their own uses, and syntax.

 

Your query should read as.

 

$sql = "SELECT branch.branchpostcode FROM users, branch WHERE users.branch1=branch.id"; //<- Select only the postcode from the branch table. 

 

Edit: Left out CROSS join, which is equivalent to INNER and the same as JOIN.  You don't use an ON clause with CROSS JOIN but you do with INNER JOIN.

Link to comment
https://forums.phpfreaks.com/topic/220297-mysql-error-in-php/#findComment-1141635
Share on other sites

hi guys, thanks but none of these worked still getting the same error, also I have checked and there is no coma or anything to interupt the query, the code below is where I have my drop down list from branch table but when I update the table, it updates the id in branch1(users)

 

how can i say to add postcode of that id in my users branch1 table? In drop down it shows the branch name, but when i do insert query for Branch1 it adds the branch id in my branch1 column in user field, instead Id like to insert the postcode of that branch, by doing this then i dont need to join tables.

 

   </select>
    
        Nearest Branch<select name='Branch1'><p />
    
<?php
$branchdropdown=mysql_query("SELECT id ,branchname FROM branch");

while($row = mysql_fetch_array($$branchdropdown))
{

echo "<option value=\"".$row['id']."\">".$row['branchname']."\n  ";
}

?>

 

Link to comment
https://forums.phpfreaks.com/topic/220297-mysql-error-in-php/#findComment-1141799
Share on other sites

That's an entirely different section of code, so of course the replies don't apply to it. The replies were specific to the query and the code you posted at the start of this thread.

 

If you look at the line of code where the error is being reported at, you will see that your code has two $$ in front of the variable name. There should only be one $ on a variable.

Link to comment
https://forums.phpfreaks.com/topic/220297-mysql-error-in-php/#findComment-1141803
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.