Why does echo fail when an SQL statement is inserted?-php Tutorial

Source: Internet
Author: User
Why does echo fail when an SQL statement is inserted?
       function execute_data($sql){$result = @mysql_query($sql) or die(mysql_error());@mysql_free_result($result) or die(mysql_error());return $result;}


 $sql = "insert into test(name) values('$name')"; if(execute_data($sql)) {     echo 'ok';}


In this way, the insert statement has been executed and inserted into the database table, but echo has not been output to the webpage .. Why?
If you execute a query statement using other methods, echo can be output to the web page.

How to execute a query statement
function get_js_array($sql){$result = @mysql_query($sql) or die(mysql_error());$arr = array();while($row = @mysql_fetch_array($result, MYSQL_ASSOC)){$arr[] = $row;  }$js = json_encode($arr);mysql_free_result($result);return $js;}


Reply to discussion (solution)

The returned value of the execute_data function $ result is released by the function mysql_free_result. if it is returned, it is null.

Print the $ result to see if it is false.

Print the $ result to see if it is false.



It's not false. I have successfully inserted them. Only echo output is unavailable.

Mysql_free_result

Are you still returning after you release them?

Mysql_query ($ SQL) only returns logical values for the insert command
Therefore, mysql_free_result ($ result) returns an error: $ result is not a valid resource.
However, you have blocked the php error message, while die (mysql_error () only terminates the program because the SQL command does not make a mistake (mysql_error () returns an empty string)

Mysql_query ($ SQL) only returns logical values for the insert command
Therefore, mysql_free_result ($ result) returns an error: $ result is not a valid resource.
However, you have blocked the php error message, while die (mysql_error () only terminates the program because the SQL command does not make a mistake (mysql_error () returns an empty string)



So what can I do to let him output it?

Comment out @ mysql_free_result ($ result) or die (mysql_error ();

Comment out @ mysql_free_result ($ result) or die (mysql_error ();


Thank you!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.