0

Hello i am trying to get this into a function getTableResults. I am unfamiliar with php.

SELECT *
FROM members
INNER JOIN member_details
ON members.member_id=member_details.member_id;

I have a similar function i am trying to base it by but i am really unsure as the sql is longer.

function getResults($tablename) {
   $sql = "SELECT * FROM " . $tablename;
   $mysqlConnection = getSQLConnection();

   $ResultSet = $mysqlConnection->query($sql);
 return $ResultSet;
}

Would this be it?

function getResults($tablename) {
   $sql = "SELECT *
           FROM members
           INNER JOIN member_details
           ON members.member_id=member_details.member_id; " . $tablename;
   $mysqlConnection = getSQLConnection();

   $ResultSet = $mysqlConnection->query($sql);
 return $ResultSet;
}
4
  • 1
    This is actually very basic. Are you sure you haven't tried everything to figure this out? Commented Nov 20, 2013 at 18:42
  • PHP knows nothing about sql. You write in SQL in php exactly the same as you would write any other plain-text string. Commented Nov 20, 2013 at 18:42
  • I have tried however i am unsure with the first line in the function Commented Nov 20, 2013 at 18:42
  • Then you should learn basic PHP syntax: php.net/manual/en/language.basic-syntax.php we're here to help, not be your teachers. Commented Nov 20, 2013 at 18:44

5 Answers 5

4
function getTableResults() {
   $sql = "SELECT * FROM members INNER JOIN member_details ON Members.member_id=member_details.member_id";
   $mysqlConnection = getSQLConnection();

   $ResultSet = $mysqlConnection->query($sql);
 return $ResultSet;
}

Basically the same, you change your SQL query in the $sql variable.

Note: This function does not accept a tablename now, as you had a very specific SQL query.

Sign up to request clarification or add additional context in comments.

Comments

3

Check this

function getTableResults() {
   $sql = "SELECT *
           FROM members
           INNER JOIN member_details
           ON members.member_id=member_details.member_id";
   $mysqlConnection = getSQLConnection();

   $ResultSet = $mysqlConnection->query($sql);
   return $ResultSet;
}

EDIT:

After your Edit in the question Would this be it

No because what you are trying is to concatenating the table name in the query..

details.member_id; " . $tablename; <- can't do this ..

This will make your query look like ON members.member_id=member_details.member_id; members (if you send members as the $tablename in the parameter)

3 Comments

So what this is doing is selecting the table members and joining to the members details where they both match with member_id?
Might want to fix your edit now; it's definitely incorrect ;)
I was not specific enough in my comment (my apologies). I meant that your code is correct, but the "My Answer is Yes" part isn't.
2
function getTableResults(){
$sql = "SELECT * FROM members 
        INNER JOIN member_details
        ON members.member_id=member_details.member_id";

$mysqlConnection = getSQLConnection();
$ResultSet = $mysqlConnection->query($sql);

return $ResultSet;
}

Please note that the functions of mysql have been deprecated. mysqli should be used instead. PHP.net says that mysql's functions will be removed in the future, so you should switch now. In practice, there is very little difference between the code of mysql and the code of mysqli. Please see this site for examples on how to use mysqli: http://www.php.net/manual/en/mysqli.query.php

2 Comments

Just as a heads up, $mysqlConnection looks to be a PDO wrapper, not related to the family of mysql_* functions (which HAVE been deprecated). Also, a minor typo: assigning to $mysqlConnection but using $mysqliConnection
Good catches. Edited.
1

use this code

 <?php

$con=mysqli_connect("example.com","peter","abc123");

// Check connection


 if (mysqli_connect_errno())

      {


 echo "Failed to connect to MySQL: " . mysqli_connect_error();

  }
// Create database
$sql="SELECT *
FROM members
INNER JOIN member_details

ON members.member_id=member_details.member_id";

if (mysqli_query($con,$sql))
{

echo "Database my_db created successfully";

  }else{

echo "Error creating database: " . mysqli_error($con);

 }?>

Comments

1

It would end up looking something like this:

function getResults($tablename) {
    $sql = "SELECT * FROM members"
           . " INNER JOIN member_details"
           . " ON members.member_id=member_details.member_id";
    $mysqlConnection = getSQLConnection();

    $ResultSet = $mysqlConnection->query($sql);
    return $ResultSet;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.