duanpa1980 2013-03-20 06:37
浏览 48
已采纳

MySQL随机生成带字母前缀的数字,使用变量时出错?

I keep researching and researching this, but can't find the answer. It's probably simple.

I'd like to update my fields with a randomly generated number using a string prefix. Example: item283940. Preferably I'd like to start at 1000 and just increment up. I seem to have most of the query for the random numbers down, however it looks like I'm missing something.

Why is this causing a syntax error?

$query = "SET @r := 1000; UPDATE discography SET pagename = (@r := @r + 1) ORDER BY RAND()";

mysql_query($query);

Yeilds:

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 'UPDATE discography SET pagename = (@r := @r + 1) ORDER BY RAND()' at line 1

And once the error is sorted out, I'd like to know how to add the prefix "item" at the beginning of the variable. Many thanks in advance.

Edit

It looks like my server uses an older version of PHP so I can't use mysqli. In terms of solving my other issues, it's inefficient but this worked for my needs:

$setit = "SET @r := 1000";
$changenames = "UPDATE discography SET pagename = (@r := @r + 1) ORDER BY RAND()";
$prepend = "UPDATE discography SET pagename=CONCAT('item',pagename)";
mysql_query($setit);
mysql_query($changenames);
mysql_query($prepend);
  • 写回答

1条回答 默认 最新

  • dtbiszu7724 2013-03-20 06:43
    关注

    using mysql_query you can't run multiple queries. The problem arise here.

    You have to use mysqli's multi_query function to run multiple query.

    And as a side not stop using mysql_* because its deprecated. start using mysqli as i mentioned OR PDO.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源