Jump to content

error mysql_fetch_assoc


JankaZ

Recommended Posts

Error

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in D:\web\www\bot\bot.php on line 11

 

Script

<form method="post"> 
Jautajums: <input name="jautajums" type="text" /> <input type="submit" value="Jautat" />

<?php
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("db") or die(mysql_error());
$Q = mysql_query("SELECT * FROM bot") or die(mysql_error());  
if($_POST['jautajums']) // vai formas name
{
$Q = mysql_real_escape_string($_POST['jautajums']); // drosiba pirmajaa vietaa
$atbilde = mysql_fetch_assoc("SELECT count(atbilde) AS c, atbilde FROM bot WHERE jautajums = '".$Q."' GROUP BY atbilde");
if($atbilde['c'] < 1)
{
echo "Nesapratu jautājumu";
}
else
{
echo $atbilde['atbilde'];
}
}
?>

Link to comment
https://forums.phpfreaks.com/topic/198246-error-mysql_fetch_assoc/
Share on other sites

You can't put an SQL query as the argument of mysql_fetch_assoc(). You need to use mysql_query to execute it, then pass the result of that into mysql_fetch_assoc(), try

 

$result= mysql_query("SELECT count(atbilde) AS c, atbilde FROM bot WHERE jautajums = '".$Q."' GROUP BY atbilde");

if($result && mysql_num_rows($result) > 0) { // if the query did not fail, and there was at least 1 row returned
$atbilde = mysql_fetch_assoc($result);

I corectli place that code?

 

<form method="post"> 
Jautajums: <input name="jautajums" type="text" /> <input type="submit" value="Jautat" />

<?php
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("db") or die(mysql_error());
$Q = mysql_query("SELECT * FROM bot") or die(mysql_error());  
if($_POST['jautajums']) // vai formas name
{
$Q = mysql_real_escape_string($_POST['jautajums']); // drosiba pirmajaa vietaa
$result= mysql_query("SELECT count(atbilde) AS c, atbilde FROM bot WHERE jautajums = '".$Q."' GROUP BY atbilde");

if($result && mysql_num_rows($result) > 0) { // if the query did not fail, and there was at least 1 row returned
$atbilde = mysql_fetch_assoc($result);
{
echo "Nesapratu jautajumu";
}
{
echo $atbilde['atbilde'];
}
?>

 

I hawe error with unspected $end! In thats line is only ?>

It's because you're missing a closing brace } character. Try

 

<?php
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("db") or die(mysql_error());
$Q = mysql_query("SELECT * FROM bot") or die(mysql_error());  
if($_POST['jautajums']) // vai formas name
{
$Q = mysql_real_escape_string($_POST['jautajums']); // drosiba pirmajaa vietaa
$result= mysql_query("SELECT count(atbilde) AS c, atbilde FROM bot WHERE jautajums = '".$Q."' GROUP BY atbilde");

if($result && mysql_num_rows($result) > 0) // if the query did not fail, and there was at least 1 row returned
{ 
	$atbilde = mysql_fetch_assoc($result);
	echo $atbilde['atbilde']
}
else
{
	echo 'no rows returned';
}
}
?>

 

Not sure what you're trying to do. The select query looks like it will return multiple rows so you'd need to loop mysql_fetch_assoc, not just call it once..... unless you know there will only ever be 1 row returned.

Tnxx Cookie for you! +1 But I am beginer and finde your whine smal mistake

echo $atbilde['atbilde']
Behaind you nide to put synatx ; But tnx alot.

Ken you tel me how to make this

 

Me: Versija

Bot: V.1

Me: Hello

Bot: Hi

 

To save a histori and show for example 5~10 posts!

What's in the bot table, is it like this?

 


ID | input (user inputs) | output (send back to user)
=====================================================
1  | Hi                  | Hello
---+---------------------+---------------------------
2  | How are you?        | I'm fine thanks, you?
---+---------------------+---------------------------
3  | What are you doing? | Not much...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.