Home Backend Development PHP Tutorial PHP無法寫进mySQL

PHP無法寫进mySQL

Jun 13, 2016 am 11:20 AM
mysql nbsp php query select

PHP無法寫入mySQL
小妹是php新手
最近想要試著寫一個搜尋程式
主要是藉由php作為媒介連結android和mySQL
不過卻卡在我的php一直無法成功判讀SQL語法
以下是我主要問題程式片段:
(in java)
try {
String result = DBConnector.executeQuery("SELECT*FROM user where zip = 'newzip' ");
System.out.println(result);

(php語法)
$db = mysql_pconnect("localhost","root","1234");

mysql_query("SET CHARACTER SET 'UTF8';");
mysql_query("SET NAMES UTF8;");
mysql_query("SET CHARACTER_SET_CLIENT=UTF8;");
mysql_query("SET CHARACTER_SET_RESULTS=UTF8;");
mysql_select_db("food");

$sql = $_POST["query_string"];

$res = mysql_query($sql);
while($r = mysql_fetch_assoc($res))
$output[] = $r;

print(json_encode($output));
mysql_close();
?>

這是我的錯誤訊息:
mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in C:\AppServ\www\AndroidConnectDB\android_connect_db.php on line 13

我有將java中的語法直接在phpmyadmin中執行
(即SELECT*FROM user where zip = 'newzip')
是可以正確找到該筆符合條件的資料
但是不知道為什麼 透過java寫入至php中去呼叫mySQL執行時卻總是傳回錯誤
上網爬過很多文
有試著去修正過編碼的問題(SET NAMES UTF8;)
也有去php.ini修改過 magic_quotes_gpc = On
但仍舊回傳null以及錯誤訊息
但是我將它改成
SELECT*FROM user where id=1 時
是可以正確執行的00"
所以再猜想會不會是單引號引起的問題
也有將它改成
SELECT*FROM user where zip = /'newzip/'
卻仍舊不見好轉ˊˋ

想要請問各位大大是否有解決的方法?ˊˋ


------解决方案--------------------
猜测是没有用的,需要看到出错时的错误信息
$res = mysql_query($sql) or die(mysql_error());
因为你给出的错误信息是
mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource 
即传递给 mysql_fetch_assoc 的不是有效的 MySQL 资源,所以要逐步向上查找
------解决方案--------------------
你不觉得应该是这样吗?
SELECT * FROM user where zip = 'newzip'
Copy after login

------解决方案--------------------
关闭魔术引号,修改php.ini magic_quotes_gpc = Off
或者临时对传入字符串反转义
$sql = stripslashes($_POST["query_string"]);
Copy after login
 也可以应付过来
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP's Impact: Web Development and Beyond PHP's Impact: Web Development and Beyond Apr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

Solve database connection problem: a practical case of using minii/db library Solve database connection problem: a practical case of using minii/db library Apr 18, 2025 am 07:09 AM

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

Laravel framework installation method Laravel framework installation method Apr 18, 2025 pm 12:54 PM

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

MySQL and phpMyAdmin: Core Features and Functions MySQL and phpMyAdmin: Core Features and Functions Apr 22, 2025 am 12:12 AM

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

See all articles