I need to translate this MySQL query into a PHP script:
-- Query
CREATE TEMPORARY TABLE t (
ID INT AUTO_INCREMENT PRIMARY KEY
)
SELECT
w.work_id,
w.name wname,
r.sort_name rsortname,
CONCAT(r.seo_url, '.', r.recording_id) as rurl
FROM
WORK AS w
JOIN recording AS r ON w.work_id = r.work_id
JOIN `release` AS rl ON r.release_id = rl.release_id
WHERE
r.is_performer = 1
AND r.is_video = 0
ORDER BY
w.work_id,
- rl.released_year DESC,
- rl.released_month DESC,
- rl.released_day DESC,
rl.release_id;
I am using a prepared statement, even if i don't need them, just in case i'll need to use variables in the feature.
Here's the PHP so far:
<?php
$q = "CREATE TEMPORARY TABLE `t` (`ID` INT AUTO_INCREMENT PRIMARY KEY)";
$q2 = "SELECT
w.work_id,
w.name wname,
r.sort_name rsortname,
CONCAT(r.seo_url, '.', r.recording_id) as rurl
FROM
WORK AS w
JOIN recording AS r ON w.work_id = r.work_id
JOIN `release` AS rl ON r.release_id = rl.release_id
WHERE
r.is_performer = 1
AND r.is_video = 0
ORDER BY
w.work_id,
- rl.released_year DESC,
- rl.released_month DESC,
- rl.released_day DESC,
rl.release_id";
$conn = fn_connect();
$stmt = $conn->stmt_init();
// run q1
$stmt->prepare($q);
$stmt->execute();
// $stmt->store_result();
// echo $stmt->num_rows;
// $stmt->free_result();
$stmt->close();
// run q2
$stmt->prepare($q2);
$stmt->execute();
$stmt->store_result();
echo $stmt->num_rows;
$stmt->free_result();
$stmt->close();
$conn->close();
The function fn_connect()
is the function to connect to the db
function fn_connect()
{
$host = 'xxx.x.x.x';
$db = 'db_dev';
$user = 'db_write';
$pwd = 'pwd123456';
$conn = new mysqli($host, $user, $pwd, $db);
if ($conn->connect_error) {
die('Error msg hoes here...');
}
$conn->set_charset("utf8");
return $conn;
}
For query #2 i'm getting the result and it's executed correctly. But, for query#1, i get an error:
Warning mysqli_stmt::execute(): invalid object or resource mysqli_stmt in /Users/myname/Sites/myproject/songbook/include/data/works.php on line 28
I believe the syntax is correct. I did check that the quotes, double quotes and ` are all ok... Cannot figure this one out. Any ideas?