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
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);

Link to comment
Share on other sites

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 ?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.