0

I am trying to return data from two tables. I am getting a No database selected error. I am only learning so go easy on me your common sense. I thought I had the database selected up top, am I missing something some where else or have i just made another mess!? The first 6 td's are from the first table (custrec), the rest from table (contidr) Here's the code

<?php
//connect to database
$mysqli = new mysqli('localhost', 'name', 'pass', 'Org_db');

// check connection
    if (mysqli_connect_errno()) {
      echo "Connect failed: " . mysqli_connect_errno(); exit();
    }

$result = mysql_query("SELECT * FROM `custrec` 
          FULL OUTER JOIN `contidr` ON contidr.cid = custrec.`cid` WHERE custrec.`cid` = `1`");
if($result === FALSE) {
    die(mysql_error()); //  error handling
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8">
<title>Get queries - multiple Customer and contact details</title>

</head>

<body>
    <table width="80%" border="1px">
      <tr>
        <th scope="col">Customer ID</th>
        <th scope="col">Customer type</th>
        <th scope="col">Number of Children</th>
        <th scope="col">Animal Preference</th>
        <th scope="col">Vet ID</th>
        <th scope="col">Individual Contact ID Number</th>
        <th scope="col">First Name</th>
        <th scope="col">Last Name</th>
        <th scope="col">Address L1</th>
        <th scope="col">Address L2</th>
        <th scope="col">Town/City</th>
        <th scope="col">State</th>
        <th scope="col">Postcode</th>
        <th scope="col">Email</th>
        <th scope="col">Postcode</th>
      </tr>
<?php

while ($row = mysql_fetch_array($result)) {
?>
  <tr>
    <td><? echo $row["$cid"]; ?></td>
    <td><? echo $row["$cust_type"]; ?></td>
    <td><? echo $row["$no_chd"]; ?></td>
    <td><? echo $row["$aPref"]; ?></td>
    <td><? echo $row["$vetId"]; ?></td>
    <td><? echo $row["$icin"]; ?></td>
    <td><? echo $row["$id_type"]; ?></td>
    <td><? echo $row["$first_name"]; ?></td>
    <td><? echo $row["$last_name"]; ?></td>
    <td><? echo $row["$add_li1"]; ?></td>
    <td><? echo $row["$add_li2"]; ?></td>
    <td><? echo $row["$town_city"]; ?></td>
    <td><? echo $row["$state"]; ?></td>
    <td><? echo $row["$postcode"]; ?></td>
    <td><? echo $row["$email"]; ?></td>
    <td><? echo $row["$ph_area_code"]; ?></td>
    <td><? echo $row["$phone"]; ?></td>
    <td><? echo $row["$mobile"]; ?></td>   
  </tr>
<?php } 

/* close connection */
 $mysqli->close();
?>
</table>
</body>
</html>
6
  • 1
    You need to select a db after connection. Use mysql_select_db("<dbname>"); before calling any queries. Commented Oct 24, 2013 at 12:02
  • @Akshay you need to learn mysqli Commented Oct 24, 2013 at 12:10
  • I put in $mysql_select_db("Org_db");before query and got Undefined variable: mysql_select_db and Function name must be a string in, so took of $ and got "no db selected". Did I do it wrong? Commented Oct 24, 2013 at 12:14
  • 1
    You are mixing MySQL and MySQLi functions here. Commented Oct 24, 2013 at 12:15
  • @YourCommonSense - I was pointing to the fact that db wasn't selected. Thanks for the correction. user2042111 - I've posted answer with the relevant link. Commented Oct 24, 2013 at 12:16

1 Answer 1

1

As it was noted in comments, you are mixing mysql and mysqli. And this is your problem.

For mysqli you already selected database but later you are using mysql functions, which causes the error. You have to use only mysqli functions.

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

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.