0

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?

Dharman
  • 30,962
  • 25
  • 85
  • 135
Marco
  • 2,687
  • 7
  • 45
  • 61
  • If you are only starting to learn PHP then you should learn PDO instead of mysqli. PDO is much easier and more suitable for beginners. Start here https://phpdelusions.net/pdo & https://websitebeaver.com/php-pdo-prepared-statements-to-prevent-sql-injection – Dharman Feb 26 '21 at 22:41
  • 1
    `utf8` charset has been deprecated 10 years ago. Use `utf8mb4` instead – Dharman Feb 26 '21 at 22:42
  • This is an usual error message I agree, but the reason is the same. You haven't enable mysqli error reporting. Read [How to get the error message in MySQLi?](https://stackoverflow.com/a/22662582/1839439) – Dharman Feb 26 '21 at 22:43
  • 1
    On unrelated note: the function `fn_connect()` seems completely redundant. Your credentials shouldn't be hardcoded and they should come from config file. You will only ever create a single instance of mysqli, which only takes 3 lines of code: enable error reporting, create new instance, set correct charset. No need for a function. – Dharman Feb 26 '21 at 22:46
  • @Dharman Thanks for the tip. I've been doing this as a part time hobby for almost 10 years. I started with mysqli coz PDO was not very well supported. I agree that PDO is much easier, especially when dealing with binding dynamic variables. It's a pain in the @#$ in mysqli when binding dynamic variables. Anyways, after going through my code, line by line, i realize that my user does not have the access ''CREATE TEMPORARY TABLE', only the basic rights `delete`, `insert`, etc... I've never use temporary tables before...So, bottom line, check your user rights when dealing with TEMPORARY TABLES. – Marco Feb 26 '21 at 23:37
  • Cool, then I am going to close this with the answer [How to get the error message in MySQLi?](https://stackoverflow.com/a/22662582/1839439) as this is what would let you find this mistake. – Dharman Feb 26 '21 at 23:40
  • @Dharman By the way, im reading al the links and i'm applying the technique `How to connect properly using mysqli` Did u wrote those amazing articles? If yes, congrats! Very helpfull ( https://phpdelusions.net/mysqli/mysqli_connect ) – Marco Feb 27 '21 at 00:41
  • No, I am not the author of these articles. – Dharman Feb 27 '21 at 00:49

0 Answers0