EchoSeven Posted May 30, 2014 Share Posted May 30, 2014 Hello. i am writing an API for lua... that sends POST requests to a php script. this php script is malfunctioning. below is the code and error. Code: <?php $host = $_POST['host']; $port = $_POST['port']; $user = $_POST['user']; $password = $_POST['password']; $db = $_POST['db']; $query = $_POST['query']; $query = urldecode($query); $connection = mysql_connect($host,$user,$password) or die("err1"); mysql_select_db($db) or die("err2"); $resource = mysql_query($query); $rows = array(); while($r = mysql_fetch_object($resource)){ $rows[] = $r; } echo json_encode($rows); //echo $rows; mysql_close(); ?> Error: html passed to JSON:decode(): <br /><b>Warning</b>: mysql_fetch_object(): supplied argument is not valid MySQL result resource in [server url] on line 17 Note: it does actually connect to the Database as per previous experiments with the code. JSON:decode() is working and therefore not the problem, since it was the PHP that gave this error to the JSON handler API. The server is working correctly as i use it all the time to make MySQL requests. though, never with this php code. i use a framework to do it for me. but it seems to be working correctly which verifies this php script ás what is malfunctioning. Quote Link to comment https://forums.phpfreaks.com/topic/288886-php-mysql-simple-script-not-working/ Share on other sites More sharing options...
PravinS Posted May 31, 2014 Share Posted May 31, 2014 you need to check the MySQL query $query = urldecode($query); echo the above line and check the query Quote Link to comment https://forums.phpfreaks.com/topic/288886-php-mysql-simple-script-not-working/#findComment-1481476 Share on other sites More sharing options...
EchoSeven Posted May 31, 2014 Author Share Posted May 31, 2014 you need to check the MySQL query $query = urldecode($query); echo the above line and check the query Done. the output is the error, then the query formatted as it should. this is not the problem. Quote Link to comment https://forums.phpfreaks.com/topic/288886-php-mysql-simple-script-not-working/#findComment-1481491 Share on other sites More sharing options...
boompa Posted May 31, 2014 Share Posted May 31, 2014 (edited) $resource = mysql_query($query); if (!$resource) { echo json_encode(array('error' => mysql_error(), 'query' => $query)); exit(); } Edited May 31, 2014 by boompa Quote Link to comment https://forums.phpfreaks.com/topic/288886-php-mysql-simple-script-not-working/#findComment-1481495 Share on other sites More sharing options...
EchoSeven Posted May 31, 2014 Author Share Posted May 31, 2014 i found the error. it wasn't the script. my MySQL program corrupts the database when it starts up. i had to redo the database.. how can i stop this? or would this be something for another forum? Quote Link to comment https://forums.phpfreaks.com/topic/288886-php-mysql-simple-script-not-working/#findComment-1481498 Share on other sites More sharing options...
boompa Posted May 31, 2014 Share Posted May 31, 2014 Try the MySQL forum. Quote Link to comment https://forums.phpfreaks.com/topic/288886-php-mysql-simple-script-not-working/#findComment-1481499 Share on other sites More sharing options...
EchoSeven Posted May 31, 2014 Author Share Posted May 31, 2014 Try the MySQL forum. thanks Quote Link to comment https://forums.phpfreaks.com/topic/288886-php-mysql-simple-script-not-working/#findComment-1481512 Share on other sites More sharing options...
Ansego Posted May 31, 2014 Share Posted May 31, 2014 (edited) Might want to consider moving too 'mysqli' as the 'mysql' extension is deprecated as of PHP 5.5.0, read more... Edited May 31, 2014 by Ansego Quote Link to comment https://forums.phpfreaks.com/topic/288886-php-mysql-simple-script-not-working/#findComment-1481514 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.