简体   繁体   English

PHP MySQL查询返回“数组”

[英]PHP MySQL query returning 'Array'

I feel like there is a relatively simple answer to this question, and it may have been asked before but I cannot see it. 我觉得这个问题有一个相对简单的答案,也许以前有人问过,但我看不到。

I have the following PHP function: 我有以下PHP函数:

$uniqueId=mysql_query("SELECT MAX(unique_id) FROM my_db.table");
$urlnum=mysql_fetch_row($uniqueId);
echo "http://yourdomain.com/display.php?urlid=".$urlnum;

When I submit the form I am getting it returning " http://yourdomain.com/display.php?urlid=Array " submitting "Array" instead of the actual unique ID from my DB. 当我提交表单时,我得到的是返回“ http://yourdomain.com/display.php?urlid=Array ”提交“ Array”而不是来自数据库的实际唯一ID。

What I would like is to have the most recently added auto-incremental unique ID from the DB be added to the end of the URL string on form submit. 我想将数据库中最新添加的自动增量唯一ID添加到表单提交的URL字符串的末尾。

Any tips as to what I am doing wrong? 关于我在做什么错的任何提示吗?

Thanks guys. 多谢你们。

As per the doc 根据文档

array mysql_fetch_row ( resource $result )

Returns a numerical array that corresponds to the fetched row and moves the internal data pointer ahead. 返回与获取的行相对应的数字数组,并将内部数据指针向前移动。

http://php.net/manual/en/function.mysql-fetch-row.php http://php.net/manual/en/function.mysql-fetch-row.php

So you should do as 所以你应该这样做

$urlnum=mysql_fetch_row($uniqueId);
echo "http://yourdomain.com/display.php?urlid=".$urlnum[0];

BTW you are using deprecated function, you should start using mysqli or PDO with prepared statement 顺便说一句,您正在使用不赞成使用的功能,您应该在准备好的语句中开始使用mysqli或PDO

The return from of mysql_fetch_row is an array (even if there is only one field requested in your query), so the code is doing what you asked it to do. mysql_fetch_row的返回值是一个数组(即使查询中只请求了一个字段),因此代码正在执行您要求的操作。 What you probably want is to reference the array index that contains the value you want. 您可能想要的是引用包含所需值的数组索引。 Something like 就像是

echo "http://yourdomain.com/display.php?urlid=".$urlnum[0];

where 0 is the array index that contains the urlid you want. 其中0是包含所需URLID的数组索引。

$urlnum is an array and you cannot print an array with echo $ urlnum是一个数组,不能用echo打印一个数组

use print_r or var_dump 使用print_r或var_dump

http://php.net/manual/en/function.print-r.php http://php.net/manual/en/function.print-r.php

Try this: 尝试这个:

var_dump($urlnum);

$urlnum is an array and can't be echoed. $urlnum是一个数组,无法回显。

Then you can see where your value is, in what index that is, and extract it like this: 然后,您可以看到您的值在哪里,在哪个索引中,并像这样提取它:

$index = 0;
$result = $urlnum[$index];

echo "http://yourdomain.com/display.php?urlid=".$result;

here you fetch the row so you must be give index number or column row like this.. 在这里,您需要获取行,因此必须给索引号或列行这样的数据。

$uniqueId=mysql_query("SELECT MAX(unique_id) FROM my_db.table");
$urlnum=mysql_fetch_row($uniqueId);
echo "http://yourdomain.com/display.php?urlid=".$urlnum["column_name"];

thank you.... 谢谢....

As you can find on php.net , mysql_fetch_row has array for return type. 正如您在php.net上可以找到的那样,mysql_fetch_row具有用于返回类型的数组。 You can echo the content of an array in a human readable format using this code: 您可以使用以下代码以人类可读的格式回显数组的内容:

echo "<pre>";
print_r($myArray);
echo "</pre>";

Also, a good practice will be to set aliases in your MySQL request, so you can use this alias as a key in the PHP code. 另外,一个好的做法是在MySQL请求中设置别名,因此您可以将此别名用作PHP代码中的键。 In your case, you can use this: 您可以使用以下方法:

$request = mysql_query("SELECT MAX(unique_id) AS maximumId FROM my_db.table");
$dataArray = mysql_fetch_row($request);
echo "http://yourdomain.com/display.php?urlid=" . $dataArray["maximumId"];

Hope this helps understand. 希望这有助于理解。

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

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