Fatal Error on loading specific MySQL Records - php

:)
I'm scripting a little web app with a database connection.
If the URL ends with ?tag=whatever it should only select the records with the categorie (rubrik) whatever in this case. It worked but then I rewrote the code a little and now I can't find the mistake. :D
Here's my php code:
if(isset($_GET['tag'])) {
$rubrik = $_GET['tag'];
$rubrik = mysqli_real_escape_string($conn,$rubrik);
$sql = "SELECT * FROM `projects` WHERE `rubrik`=`". $rubrik ."` ORDER BY `datum` DESC";
$result = $conn->query($sql);
}
else {
$sql = "SELECT * FROM `projects` ORDER BY `datum` DESC";
$result = $conn->query($sql);
}
while($row = $result->fetch_assoc()) {
echo "<a href='project.php?id=".$row["id"]."'><div class='card ". $row["rubrik"] ."'><h4>".$row["name"]."</h4><p>".$row["funktion"]."</p></div></a>";
}
If I just open index.php it works as usual. But if I type index.php?tag=whatever
This error pops up:
Fatal error: Call to a member function fetch_assoc() on a non-object
Does anyone know what I did wrong? Thanks for your help in advance! :)

Your query uses backtick while it should use single quote. Backtick is for database, table or column identifier while single quote is for string or date literal value.
Change from
"SELECT * FROM `projects` WHERE `rubrik`=`". $rubrik ."` ORDER BY `datum` DESC";
to
"SELECT * FROM `projects` WHERE `rubrik`='". $rubrik ."' ORDER BY `datum` DESC";

try updating your code as below
if(isset($_GET['tag'])) {
$rubrik = $_GET['tag'];
$rubrik = mysqli_real_escape_string($conn,$rubrik);
$sql = "SELECT * FROM `projects` WHERE `rubrik`=`". $rubrik ."` ORDER BY `datum` DESC";
$result = $conn->query($sql);
}
else {
$sql = "SELECT * FROM `projects` ORDER BY `datum` DESC";
$result = $conn->query($sql);
}
if( $result){
while($row = $result->fetch_assoc()) {
echo "<a href='project.php?id=".$row["id"]."'><div class='card ". $row["rubrik"] ."'><h4>".$row["name"]."</h4><p>".$row["funktion"]."</p></div></a>";
}
}

Mistake in your select query
"SELECT * FROM `projects` WHERE `rubrik`=`". $rubrik ."` ORDER BY `datum` DESC";// remove ` symbol in php variable $rubrik

Related

Array to string conversion in mysql while loop

Following is my code,
$result1 = "SELECT emp_id FROM employee where manager_id=".$userID;
$array = mysql_query($result1);
$cnt = 0;
while ($row = mysql_fetch_array($array)) {
"emp_id: " . $row[0];
$myArrayOfemp_id[$cnt] = $row[0];
$cnt++;
}
var_dump($myArrayOfemp_id);
$sql = "SELECT emp_id FROM emp_leaves WHERE emp_id='$myArrayOfemp_id' ORDER BY apply_date DESC";
$result = mysql_query($sql);
$total_results = mysql_num_rows($result);
When I'am trying to use $myArrayOfemp_id variable in $sql query, It shows that error:
Array to string conversion in..
How can I fix it?
You are trying to convert an array into a string in the following line:
$sql = "SELECT emp_id FROM emp_leaves
WHERE emp_id='$myArrayOfemp_id' ORDER BY apply_date DESC";
$myArrayOfemp_id is an array. That previous line of code should be changed to:
$sql = "SELECT emp_id FROM emp_leaves
WHERE emp_id={$myArrayOfemp_id[0]} ORDER BY apply_date DESC";
I placed 0 inside {$myArrayOfemp_id[0]} because I'm not sure what value want to use that is inside the array.
Edited:
After discussing what the user wanted in the question, it seems the user wanted to use all the values inside the array in the sql statement, so here is a solution for that specific case:
$sql = "SELECT emp_id FROM emp_leaves
WHERE ";
foreach ($myArrayOfemp_id as $value)
{
$sql .= " emp_id={$value) || ";
}
$sql .= "1=2";
$result = mysql_query($sql);
$total_results = mysql_num_rows($result);
$sql = "SELECT emp_id FROM emp_leaves WHERE emp_id in
(SELECT GROUP_CONCAT(emp_id) FROM employee where manager_id=".$userID.")
ORDER BY apply_date DESC";
$result = mysql_query($sql);
$total_results = mysql_num_rows($result);
just change your query like above might solve your problem.
you can remove following code now. :)
$result1 = "SELECT emp_id FROM employee where manager_id=".$userID;
$array = mysql_query($result1);
$cnt = 0;
while ($row = mysql_fetch_array($array)) {
"emp_id: " . $row[0];
$myArrayOfemp_id[$cnt] = $row[0];
$cnt++;
}
var_dump($myArrayOfemp_id);

sql statement select from DB limit didn't work

i have a question which is my limit statement didn't work i want the content select from database and limit the show content in 40 only but it didn't work
here is my SQL statement with php code
$chatroomID=$_GET['chatroomID'];
$userID = $_SESSION['id'];
$sql="SELECT * FROM chatroom_chat WHERE chatroom_id ='$chatroomID'";
$result1 = mysqli_query($connection, $sql) or die(mysqli_error($connection));
while ($row = mysqli_fetch_array($result1)) {
$chat = $row['chat_id'];
$sql3 ="SELECT * FROM (
SELECT * FROM chat WHERE id = '$chat' ORDER BY id DESC LIMIT 0,40
) sub
ORDER BY id ASC ";
$getChatData = mysqli_query($connection,$sql3) or die(mysqli_error($connection));
/*here have statement to get username*/
while($row3 = mysqli_fetch_array($getChatData)) {
echo "<div>all content</div>";
}
}
does my code have any syntax error? i no sure why it didn't work
SELECT * FROM (
SELECT * FROM chat WHERE id = '$chat' ORDER BY id DESC LIMIT 40
) sub
ORDER BY id ASC

MySQL/PHP - Display Recent Date

I was able to apply this line onto phpMyAdmin and it worked just fine.
SELECT id, date_format(`date`, '%m.%d.%Y') as `date` FROM TABLE ORDER BY date DESC LIMIT 1
The problem is that when I added the rest of the code, the recent date shows up blank on the webpage. Am I missing something in this code?
<?php
$query = "SELECT id, date_format(`date`, '%m.%d.%Y') as `date` FROM TABLE ORDER BY date DESC LIMIT 1";
$result = mysql_query($query);
echo "$date";
?>
Any help is appreciated. Thank you.
. Try this
$query = "SELECT id, date_format(`date`, '%m.%d.%Y') as `date` FROM TABLE ORDER BY. date DESC LIMIT 1";
$result = mysql_query($query);
$r = mysql_fetch_assoc($result);
$date = $r['date'];
echo "$date";
You didn't set $date variable. You need to use mysql_fetch_array function for your $result variable.
Ex:
`
$query = "SELECT id, date_format('date', '%m.%d.%Y') as 'date' FROM TABLE ORDER BY date DESC LIMIT 1";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
print_r($row); }
`

loops mixed up with MySQL

I'm writing a simple message board where you can reply to any thread and then reply to any reply and so on.... everything works well but is there a simple method to loop the query as this could potentiality go on and on and on
$rsql = "SELECT * FROM rotd_mb WHERE reply='N' ORDER BY dateTime DESC";
$result = runSQL($rsql);
while ($row = mysql_fetch_array($result)) {
echo "".$row[title]."";
$rsql2 = "SELECT * FROM rotd_mb WHERE reply='Y' AND replyID='$row[messageID]' ORDER BY dateTime DESC";
$result2 = runSQL($rsql2);
while ($row2 = mysql_fetch_array($result2)) {
echo "".$row2[title]."";
$rsql3 = "SELECT * FROM rotd_mb WHERE reply='Y' AND replyID='$row2[messageID]' ORDER BY dateTime DESC";
$result3 = runSQL($rsql2);
while ($row3 = mysql_fetch_array($result3)) {
echo "".$row3[title]."";
}
mysql_free_result($result3);
}
mysql_free_result($result2);
}
mysql_free_result($result);
You could try UNION, probably not the best solution but it will work
$sql = "SELECT * FROM rotd_mb WHERE reply='N' ORDER BY dateTime DESC
UNION
SELECT * FROM rotd_mb WHERE reply='Y' AND replyID='$row[messageID] ORDER BY dateTime DESC
UNION
SELECT * FROM rotd_mb WHERE reply='Y' AND replyID='$row2[messageID]' ORDER BY dateTime DESC";
A better idea would be to re-design your database table structure.

PHP/MYSQL Different Trouble Selecting By Primary Key:

Due to some help from a recent post, I'm selecting a row by primary key, as follows:
$query ="SELECT * FROM Bowlers WHERE 'key' = '1'";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_assoc($result)or die(mysql_error());
For some reason, the third line of code dies every time, without error. It works fine using other keys, ie WHERE name = 'djs22'.
Any ideas?
You are using single quotes on the field name, you must use backticks.
not ', but `
try
$query ="SELECT * FROM Bowlers WHERE key = '1'";
or
$query ="SELECT * FROM `Bowlers` WHERE `key` = '1'";
instead of
$query ="SELECT * FROM Bowlers WHERE 'key' = '1'";
try using this
$query ="SELECT * FROM Bowlers WHERE `key` = '1'";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_assoc($result)or die(mysql_error());
I just replaced ' ' by .

Categories