使用php查询mysql数据库(querying mysql database using php)

编程入门 行业动态 更新时间:2024-10-27 22:22:29
使用php查询mysql数据库(querying mysql database using php)

我搜索谷歌和在这里和一些信息有用,但大多数没有那么有用,仍然无法设法使用PHP查询我的数据库。 我对编码很新,所以最好根据我的代码找到答案,而不是其他人稍微不同的问题。 我有一个小型数据库,人们填写一份希望雇用公共汽车的表格。 我已经创建了一个日历,并希望根据人们输入的已发送到我的数据库的数据,每天打印出什么时间以及有多长时间的租用(如果有的话)。 整个页面显示但日历在其表中包含一个错误:

“您的SQL语法有错误;请查看与您的MySQL服务器版本对应的手册,以便在第1行''2013-03-01'附近使用正确的语法”

即使我删除查询的“where date = ..”部分,它仍会显示此信息。 香港专业教育学院尝试了几种不同的编写代码的方法,包括循环,不包括一个,但我不确定我的意思是做什么。

编辑:我的代码现在:

<?php $con = mysql_connect("localhost","user","password"); $result = mysql_query("SELECT time, length FROM hire WHERE date='01-03-13'"); $row = mysql_fetch_assoc($result); $Time = $row['TIME']; $Length = $row['LENGTH']; echo "Time: " . $TIME . "<br/>"; echo "Length: " . $LENGTH . "<br/>"; ?>

我现在得到错误“警告:mysql_fetch_assoc():提供的参数不是有效的MySQL结果资源在C:\ Users \ Laura \ Documents \ COMPY \ server \ www \ calendar.php第123行”它显示页面,日历和“时间”和“长度”但有错误,没有数据

编辑

<?php $con = mysql_connect("localhost","user","password"); mysql_select_db("busassociation", $con); $result = mysql_query("SELECT time, length FROM hire WHERE date='01-03-13'"); while ($row = mysql_fetch_assoc($result)){ $time = $row['time']; $length = $row['length']; echo "time: " . $time . "<br/>"; echo "length: " . $length; } ?>

I've searched google and on here and some info has been useful but mostly not so useful and still can't manage to query my database using php. I'm very new to coding so thought it best to find an answer based on my code rather than other peoples slightly different problems. I have a small database, people fill in a form who wish to hire a bus. i have created a calendar and wish to print out for each day what time and for how long there is a hire, if there is one, based on the data people have entered which has been sent to my database. The whole page shows but the calendar contains an error within its table saying:

"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''2013-03-01' at line 1"

even if i delete the "where date=.." part of the query it continues to show this. ive tried several different methods of writing the code out, including a loop and not including one but im not really sure what im meant to do.

EDIT: MY CODE IS NOW:

<?php $con = mysql_connect("localhost","user","password"); $result = mysql_query("SELECT time, length FROM hire WHERE date='01-03-13'"); $row = mysql_fetch_assoc($result); $Time = $row['TIME']; $Length = $row['LENGTH']; echo "Time: " . $TIME . "<br/>"; echo "Length: " . $LENGTH . "<br/>"; ?>

I now get the error "Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in C:\Users\Laura\Documents\COMPY\server\www\calendar.php on line 123" It shows the page, calendar and "time" and "length" but with the error and no data

EDIT

<?php $con = mysql_connect("localhost","user","password"); mysql_select_db("busassociation", $con); $result = mysql_query("SELECT time, length FROM hire WHERE date='01-03-13'"); while ($row = mysql_fetch_assoc($result)){ $time = $row['time']; $length = $row['length']; echo "time: " . $time . "<br/>"; echo "length: " . $length; } ?>

最满意答案

使用反引号

$result = mysql_query("SELECT `time`, `length` FROM `hire` WHERE `date`= '2013-03-01' ");

编辑。

你正在两次获取你的查询。

$row = mysql_fetch_row($result); <------- delete this line while( $row = mysql_fetch_row($result) ){

EDIT.2

我想你不会重新解决两者之间的差异

mysql_fetch_assoc()和mysql_fetch_row()

如果您使用mysql_fetch_assoc() ,它将在您的代码中正确。

但如果您使用mysql_fetch_row()就像你在代码中所做的那样,你必须回复这样的值

$Time = $row['1']; // suppose that `TIME` is the second column $Length = $row['2']; // suppose that `LENGTH` is the third column

EDIT.3

<?php $con = mysql_connect("localhost","user","password"); mysql_select_db("your_db", $con); <---------replace by your database $result = mysql_query("SELECT time, length FROM hire WHERE date='01-03-13'"); while ($row = mysql_fetch_assoc($result)){ $Time = $row['time']; $Length = $row['length']; echo "Time: " . $Time . "<br/>"; echo "Length: " . $Length . "<br/>"; } ?>

use backticks

$result = mysql_query("SELECT `time`, `length` FROM `hire` WHERE `date`= '2013-03-01' ");

EDIT.

your are fetching your query two times.

$row = mysql_fetch_row($result); <------- delete this line while( $row = mysql_fetch_row($result) ){

EDIT.2

i think u dont reconize the diference between

mysql_fetch_assoc() and mysql_fetch_row()

if you use mysql_fetch_assoc() it will be correct in your code.

but if u use mysql_fetch_row() as u have done in your code u must echo values like that

$Time = $row['1']; // suppose that `TIME` is the second column $Length = $row['2']; // suppose that `LENGTH` is the third column

EDIT.3

<?php $con = mysql_connect("localhost","user","password"); mysql_select_db("your_db", $con); <---------replace by your database $result = mysql_query("SELECT time, length FROM hire WHERE date='01-03-13'"); while ($row = mysql_fetch_assoc($result)){ $Time = $row['time']; $Length = $row['length']; echo "Time: " . $Time . "<br/>"; echo "Length: " . $Length . "<br/>"; } ?>

更多推荐

本文发布于:2023-07-17 10:13:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1142852.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据库   mysql   php   querying   database

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!