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']; } } ?> Link to comment https://forums.phpfreaks.com/topic/198246-error-mysql_fetch_assoc/ 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); Link to comment https://forums.phpfreaks.com/topic/198246-error-mysql_fetch_assoc/#findComment-1040156 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 ?> Link to comment https://forums.phpfreaks.com/topic/198246-error-mysql_fetch_assoc/#findComment-1040297 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. Link to comment https://forums.phpfreaks.com/topic/198246-error-mysql_fetch_assoc/#findComment-1040454 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! Link to comment https://forums.phpfreaks.com/topic/198246-error-mysql_fetch_assoc/#findComment-1040494 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... Link to comment https://forums.phpfreaks.com/topic/198246-error-mysql_fetch_assoc/#findComment-1040500 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 Link to comment https://forums.phpfreaks.com/topic/198246-error-mysql_fetch_assoc/#findComment-1040506 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.