doushou5761 2014-03-21 10:26
浏览 28
已采纳

获取数组mysql数据库php

I am trying to fetch data from database, but something is not working.

This is my code:

<?php

$koppla = mysql_connect("localhost","admin","","test");



// Check connection


if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }


    $get = mysql_query($koppla,SELECT * FROM 123);

while ($test = mysql_fetch_array($get))
{
    echo $test['tid'];
}

mysql_close($koppla);
?> `<?php

$koppla = mysql_connect("localhost","admin","","test");



// Check connection


if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }


    $get = mysql_query($koppla,SELECT * FROM 123);

while ($test = mysql_fetch_array($get))
{
    echo $test['tid'];
}

mysql_close($koppla);
?>

I am getting the following error while trying to fetch an array from a MySQL database. What is wrong?

Parse error: syntax error, unexpected '123' (T_LNUMBER) in C:\wamp\www\test.php on line 16
  • 写回答

3条回答 默认 最新

  • donglu6303 2014-03-21 10:28
    关注

    What was wrong

    There are at least 3 errors:

    • Use either mysql_XY or mysqli_XY. NOT both. See MySQL: Choosing an API.
      TL;DR: Use mysqli_*, because mysql_* is deprecated.
    • The SELECT statement in line 16 and line 39 has to be in quotes.
    • The syntax of mysql_query is

      mixed mysql_query ( string $query [, resource $link_identifier = NULL ] )

    What is correct

    So line 16 has to be something like

    $get = mysql_query("SELECT * FROM 123", $koppla);
    

    or, when you choose mysqli_query:

    $get = mysqli_query($koppla, "SELECT * FROM 123");
    

    Side notes

    • Table naming: I would not use a table name like 123. I don't know if this is valid SQL, but it feels wrong to not start a table with a character. See SQLite issue with Table Names using numbers? - I know you're using MySQL, but MySQL might have similar problems. And you might want to switch sometimes to another database system.
    • Optional arguments: You don't need to specify the $link_identifier in mysql_* if you don't have multiple connections.
    • Style Guide: In PHP, you usually have the curly brace { in the same line as the if. See List of highly-regarded PHP style guides? and especially the Zend and PEAR section. This is also good for SO, because you could avoid a scrollbar in your code which makes reading your question easier.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘