JankaZ Posted April 11, 2010 Share Posted April 11, 2010 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']; } } ?> Quote Link to comment Share on other sites More sharing options...
the182guy Posted April 11, 2010 Share Posted April 11, 2010 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); Quote Link to comment Share on other sites More sharing options...
JankaZ Posted April 12, 2010 Author Share Posted April 12, 2010 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 ?> Quote Link to comment Share on other sites More sharing options...
the182guy Posted April 12, 2010 Share Posted April 12, 2010 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. Quote Link to comment Share on other sites More sharing options...
JankaZ Posted April 12, 2010 Author Share Posted April 12, 2010 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! Quote Link to comment Share on other sites More sharing options...
the182guy Posted April 12, 2010 Share Posted April 12, 2010 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... Quote Link to comment Share on other sites More sharing options...
JankaZ Posted April 12, 2010 Author Share Posted April 12, 2010 Jes Table bot Rows id jautajums atbilde id=id jautajums=question atbilde=answer Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.