简体   繁体   中英

Fetch array mysql database 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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