0

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
  • 1
    what is the 123 ? is it your table name ? Commented Mar 21, 2014 at 10:27
  • 2
    you're lost between mysqli and mysql. at place you use one other place you use the other, and you're using mysqli_query syntax for mysql_query. this $get = mysql_query($koppla,SELECT * FROM 123); should be like this: $get = mysql_query("SELECT * FROM 123");` Commented Mar 21, 2014 at 10:30
  • Why do you have this line ?> <?php`? Commented Mar 21, 2014 at 10:46

3 Answers 3

3

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.
Sign up to request clarification or add additional context in comments.

3 Comments

+1 for the long detailed answer... Tell him to use mysqli as mysql_* is deprecated :)
@CodeBird: Thanks for the +1 and fixing my copy-and-paste typo in mysqli_query.
Cheers for the help, going to take a look at those links later as well :)
2
$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); 

https://www.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

https://www.php.net/manual/en/function.mysql-connect.php as well

Comments

0

$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

1 Comment

I removed the my -1, now that you fixed. I tried to comment before but SO wouldn't let me...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.