php MySQL error for retrieving data - php

I have a PHP code for selecting data from a database, but it produces NULL result, although the same query performed manually works well. Here's relevant part of code:
$conn = mysqli_connect($host,$user,$password);
if (!$conn) {
die('Could not connect: ' . mysql_error());
}
mysql_db_name($dbName,$conn);
$query = "SELECT Images.Path,p.NameAr,p.DescriptionAr FROM
(SELECT * FROM project where TypeID = 1) as p
JOIN Images where Images.ProjectID = p.ID";
$result = mysql_query($query,$conn);
var_dump($result);
What can be wrong about it?

Not sure about PHP related error but your query formation is not corect.
Change your query like below. Moreover, you are not fetching any column from images table then why do a join with images table?
SELECT Path,NameAr,DescriptionAr
FROM
(
SELECT p.* FROM project p
JOIN
Images i on i.ProjectID = p.ID and p.TypeID = 1
) tab

You use mysqli_connect but then you wrote mysql_* If you fix all mysql_* to mysqli_* it should work.

Related

How to implement a mysql query that performs inner join from two different database in PHP using mysqli_query?

I've been using PHP mysqli functions since long but never came accross such a senario where I need to join two tables from different database.
Suppose I've table t1 from database d1 & table t2 from database d2 with a common column say id.
I am stuck here:
mysqli_select_db($conn1,"d1") or die("Error selecting database");
$conn2 = mysqli_connect("localhost","root","") or die("Error connecting Database");
mysqli_select_db($conn2,"d2") or die("Error selecting database");
$sql = "SELECT * from d1.t1 tab1 inner join d2.t2 tab2 on tab1.id = tab2.id";
$result = mysqli_query(**don't know**,$sql);
$row = mysqli_fetch_array($result);
Please tell me what should I write in place of "don't know". Also suggest any better way, if any to get this done.
Thanks in advance.:)

PHP and SQL INSERT JOIN 2 tables with criteria error

$rentingsquery = "SELECT * FROM TBL_rentings WHERE personId='$personId'
JOIN TBL_rentings ON TBL_properties.propertyId = TBL_rentings.propertyId
JOIN TBL_people ON TBL_rentings.personId = TBL_people.personId";
$rentingsResult=mysql_query($rentingsquery) or die ("Query to get data from TBL_properties failed: ".mysql_error());
This is the error I get when I execute the code in my web page:
What I'm trying to do is select only the 'rentings' where the personId = $personId, (as the current page is a page for each individual person), and display only those 'rentings'. Also in the code which I haven't posted I'm displaying data about the property which is related to that rent, hence why I'm trying to join the renting and properties table with propertyId so that I call the correct property's details off the database.
$rentingsquery = "SELECT * FROM TBL_rentings
JOIN TBL_rentings ON TBL_properties.propertyId = TBL_rentings.propertyId
JOIN TBL_people ON TBL_rentings.personId = TBL_people.personId
WHERE personId='$personId'";
$rentingsResult=mysql_query($rentingsquery) or die ("Query to get data from TBL_properties failed: ".mysql_error());
First of all try this, the where condition has to be after the joins.
If there are other problems I will edit the answer
The next problem is that you are joining the same table, but you are joining it in an external field. A quick fix, if I am supposing correct, is that you wrongly joined with an unwanted table:
SELECT * FROM TBL_rentings
JOIN TBL_properties ON TBL_properties.propertyId = TBL_rentings.propertyId
JOIN TBL_people ON TBL_rentings.personId = TBL_people.personId
WHERE TBL_rentings.personId='$personId'

Sqlite3 synthax error while joining tables using PDO

I have a problem with PDO and sqlite3. While trying to join tables I am getting an error:
object(PDO)#1 (0) { } {"error":true,"message":"SQLSTATE[HY000]: General error: 1 near \u0022FROM\u0022: syntax error"
my connection and sql query looks like this. The sublime-text 2 also does not color JOIN like others.
$objDb = new PDO('sqlite:../dbase/shopping-list');
var_dump($objDb);
$objDb -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT `i`.*,
`t`.`name` AS `type_name`
FROM `items` `i`
JOIN `types` `t`
ON `t`.`id` = `i`.`type`
ORDER BY `i`.`date` ASC";
$result = $objDb->query($sql);
if(!$result) {
throw new PDOException("The result returned no object");
}
I follow a tutorial and he can use this statement with mac. I use arch linux but I dont think so it is related.
When I check sqlite database with cli, I can get the types table :
sqlite> SELECT * FROM `types`;
1|Qty
2|Kg
Is there anyway to accomplish this query ?
edit : I have changed db and removed backticks ... Now I can get the items but still I have an error and they dont work inside my angularjs frontend.
object(PDOStatement)#3 (1) { ["queryString"]=> string(37) "SELECT * FROM types ORDER BY id" } {"error":false,"items":[{"id":"1","item":"Butter","qty":"1","type":"1","done":"0","date":"2014-10-06 02:45:51","type_name":"Qty"}],"types":[{"id":"1","name":"Qty"},{"id":"2","name":"Kg"}]}
here is correct sql query with JOIN
$sql = "SELECT item.* ,
t.name AS type_name
FROM items item
INNER JOIN types t
ON t.id = item.type
ORDER BY item.date ASC";
this is my select.php link

MYSQL connect and query 2 dbs on 2 different server

MYSQL connect and query 2 dbs on 2 different server
THANKS in advance for any help,
I've been trying for a week to get this to work,
and have googled various examples on this site and on others.
i'm trying to connect 2 db, on the same server.
i can get data from each seperatly, but i don't know how to wrire code to combine / join the data
this is my incorrect code based on numerous help and
how to post i've read online. ANY help or clues are welcome.
If i can just get anything to output / print i would be happy
// connect to db1 contact info
$host1='db1hostname';
$user1='db1user';
$password1='db1pw';
$database1='db1user';
// connect to db2, phone call info
$host2='db2hostname';
$user2='db2user';
$password2='db2pw';
$database2='db2user';
$connect1 = mysql_connect($host1, $user1, $password1) OR DIE
('Unable to connect to database! Please try again later.');
$connect2 = mysql_connect($host2, $user2, $password2) OR DIE
('Unable to connect to database! Please try again later.');
// i think this is where things start to go wrong
mysql_select_db($database1, $connect1);
mysql_select_db($database2, $connect2);
// i want to join these 2 tables from diff db by the caller info
which is a cell number
// the database 2 info comes from a phone call records table
and can't be altered, so i can't connect the 2 tbls by the table primary id
because i never know what the id will be for each call in db2,
i wish to connect the 2 db tables by caller cell number
$data = mysql_query("SELECT `m`.`id`, `m`.`created`, `m`.`caller`, `m`.`content_text`,
`c`.`iname`, `c`.`caller`
FROM database1.contacts c, database2.messages m
WHERE `c`.`caller` = `m`.`caller` ") or die(mysql_error());
// i then want to print the results to a table
while($info = mysql_fetch_array( $data ))
Connect to your first database and when you want to run a query in the second database just use the simple way like in below code.
$query = "SELECT t1.*, t2.*
FROM tableOfDB1 t1
LEFT JOIN database2.tableOfDB2 t2 ON t1.ID = ts2.TableOne_ID
";
Or if you want data only from db2
$query = "SELECT t2.*
FROM database2.tableOfDB2 t2";
In your example try this
$data = mysql_query("SELECT `m`.`id` AS mID, `m`.`created` AS mCreated, `m`.`caller` AS mCaller, `m`.`content_text`,
`c`.`iname` AS cIname, `c`.`caller` AS cCaller
FROM database1.contacts c, database2.messages m
WHERE `c`.`caller` = `m`.`caller` ") or die(mysql_error());
while( $row = mysql_fetch_array($data) )
{
echo "<br>mCaller: ".$row['mCaller'];
echo "<br>cCaller: ".$row['cCaller'];
}
If, as you say, the databases are on the same server, then you can do everything with one connection. You have to be explicit about your database names in your queries, but then you can do everything else in a very straightforward way.
See https://stackoverflow.com/questions/19039718/reference-column-in-seperate-database/19040228#19040228

mulitiple database select php mysql

I have 2 databases on the same server with 2 identical tables.
What I want to do is select all records from both tables and join them in one array.
I've been messing around with the script below. For some reason it returns the records of db2.tbl twice and doesn't return the db1.tbl records at all. When I try to select the data from a single database is works fine for both of them. Does any one see the problem?
<?PHP
require_once("config.php");
$conn = #mysql_connect($dbhost, $dbuser, $dbpass)or die ('Error connecting to mysql server'.mysql_error());
$q = mysql_query("SELECT * FROM db1.tbl JOIN db2.tbl");
var_dump(mysql_num_rows($q));
while($arr = mysql_fetch_assoc($q)){
var_dump($arr);
}
?>
Is this what you want ? All records from database1 followed by all records from database2:
$q = mysql_query("SELECT * FROM db1.tbl UNION SELECT * FROM db2.tbl");
I assume the user you are connecting with has access to both databases.
Your query should work. However add tilt(`) to your database name table name. Execute the query first in mysql see whether it is ok than execute with php.

Categories