I seem to be confusing myself, so I’ll detail a bit of the background and what I’m trying to do.
I have an archive of what are referred to on the site as “thoughts” in a MySQL database. There are multiple fields here detailing all kinds of things, e.g. author, content, go live time, etc. I previously called these through obtaining the day, month and year of the thought through the query string, e.g. ?d=31&&m12&&y=2007 (to give one example). To help identify each thought in the database I’ve set the id field in a similar format, e.g. “31-12-2007”. Due to this, the id field is set as VARCHAR.
I’ve just moved hosts, partially because I’ve felt the need to move from a host running PHP v5.* to v 7.*. And part of this is to force myself to force myself to get away from old code and call things from the MySQL database the right way.
However, I seem to be confusing myself when trying to call this. I’ve ensured that the query string is being captured and is in the expected format. I’ve gone back and built the page incrementally, and it seems to fall over when I’m getting to the query. Little bit odd as well when I’ve run the query in PHPMyAdmin and it works successfully (admittedly here I pass on what would be the query string as a value such as “31-12-2007” instead of $shortthoughtid).
Queries I’ve attempted are as follows:
$sql = "SELECT * FROM thoughts WHERE id=?"; // SQL with parameters
$stmt = $conn->prepare($sql);
$stmt->bind_param("s", $shortthoughtid);
$stmt->execute();
$result = $stmt->get_result(); // get the mysqli result
$fullcontent = $result->fetch_assoc(); // fetch data
// Test line to see if query is successful
echo $fullcontent;
$selectThought = $mysqli->prepare("SELECT * FROM thoughts WHERE id = '$shortthoughtid' LIMIT 1");
$selectThought->execute();
$selectThoughtResult = $selectThought->get_result();
/* now you can fetch the results into an array - NICE */
while ($myrow = $selectThoughtResult->fetch_assoc()) {
// use your $myrow array as you would with any other fetch
printf("This is the title - $myrow['title']);
}
Also, I could potentially call an article using the go live field, which is an integer, if that would be easier.
Overall, it is a bit baffling when I’ve got other queries that I have been able to translate over and get working. Struggling to get my head around this all and understand it if I’m being honest. Any help appreciated, thanks.