获取数组mysql数据库php

获取数组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

我正在尝试从数据库中获取数据,但有些东西无效。 p> 这是我的代码: p>

 &lt;?php 
 
 $ koppla = mysql_connect(“localhost”,“admin”,“”,“test”);  
 
 
 
 //检查连接
 
 
if(mysqli_connect_errno())
 {
 echo“无法连接到MySQL:”。  mysqli_connect_error(); 
} 
 
 
 $ get = mysql_query($ koppla,SELECT * FROM 123); 
 
而($ test = mysql_fetch_array($ get))
 {
 echo $ test [  'TID']; \ N} 
 
mysql_close($ koppla); 
&GT?;  `&lt;?php 
 
 $ koppla = mysql_connect(“localhost”,“admin”,“”,“test”); 
 
 
 
 //检查连接
 
 
if(mysqli_connect_errno  ())
 {
 echo“无法连接到MySQL:”。  mysqli_connect_error(); 
} 
 
 
 $ get = mysql_query($ koppla,SELECT * FROM 123); 
 
而($ test = mysql_fetch_array($ get))
 {
 echo $ test [  'tid']; 
} 
 
mysql_close($ koppla); 
?&gt; 
  code>  pre> 
 
 

我在尝试获取时遇到以下错误 来自MySQL数据库的数组。 有什么问题? p>

 解析错误:语法错误,第16行的C:\ wamp \ www \ test.php中的意外'123'(T_LNUMBER)
  code  >  pre> 
  div>

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.

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

should be

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

You have this in 2 places correct them.

OH well hold on you are using mysql_query()

so it should be

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

http://in1.php.net/mysql_query

Now going more in the code you are using if (mysqli_connect_errno()) this is for mysqli_connect() you may need to see

http://in1.php.net/manual/en/function.mysql-connect.php as well

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

Shell look like this: $get = mysql_query("SELECT * FROM 123", $koppla);

A query is used to be String; and $koppla shell be the second parameter