简体   繁体   English

从mysql查询回显字符串-PHP

[英]Echoing string from a mysql query - PHP

How do I echo a simple string from a MySQL Query? 如何从MySQL查询中回显简单的字符串?

I'm trying trying to accomplish this with the following code but it is not working...The data I am pulling is fine so I know that my mysql_query is working (I've checked that via a different URL GET method. 我正在尝试使用以下代码来完成此操作,但它不起作用...我提取的数据很好,所以我知道我的mysql_query正在工作(我已经通过其他URL GET方法进行了检查。

<?php
$myQuery = mysql_query("fetch some stuff....");
$myResult = mysql_fetch_object($myQuery);
echo $myResult;

First of all use var_dump($myResult) to see the data and it's structure. 首先使用var_dump($myResult)查看数据及其结构。

Since it's an object it will have properties named as the columns returned by the SELECT statement you used. 由于它是一个对象,因此将具有名为您所使用的SELECT语句返回的列的属性。

echo $myResult->column_name; // Should work fine

Usually if echo $variable; 通常,如果echo $variable; doesn't work it means that the variable is either en empty string '' or a null value NULL or a false value FALSE which all show "nothing" when echoed. 不起作用,表示变量为空字符串''或空值NULL或假值FALSE ,它们在回显时均显示“ nothing”。

But when using var_dump() on them you get a report of the type of data and size of it. 但是,在它们上使用var_dump()时,您会得到有关数据类型和大小的报告。

you need to know what is returned type. 您需要知道返回的类型。 in what your doing you assume that it printable but most of what db queries return are either in object form or an array 在您做的事情中,您假定它可打印,但是大多数数据库查询返回的内容都是对象形式或数组

try doing a 尝试做一个

echo "<pre>" ,print_r($myResult, TRUE),"</pre>";

Providing your query is correct, it looks like your php tags are incorrect: 只要您的查询是正确的,就好像您的php标签不正确:

<?php  ?>

PS It might help if you post the actual query so it can be troubleshooted here. PS如果您发布实际查询,可能会有所帮助,因此可以在此处进行故障排除。 It's hard to ask why something is not working and get an answer if you don't show any of it. 很难问为什么有些东西不起作用,如果您什么都没显示出来,就可以得到答案。

First, var_dump($myResult); 首先, var_dump($myResult); . If you see NULL , your query is failing. 如果看到NULL ,则查询失败。 If you see a big block o' jumbled text, the query is, in fact, working. 如果看到混乱的文本大块,则查询实际上是有效的。 Since you are echoing $myResult , it is no surprise that nothing is being output, as you are trying to echo the object directly rather than the property you want. 由于您正在回显$myResult ,因此什么也不输出就不足为奇了,因为您试图直接回显对象而不是您想要的属性。 Try echoing $myResult->myColumn; 尝试回显$myResult->myColumn;

Also, please use MySQLi or PDO, as php_mysql is deprecated. 另外,请不要使用MySQLi或PDO,因为不建议使用php_mysql。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM