Reputation: 23
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
Upvotes: 0
Views: 116
Reputation: 136257
There are at least 3 errors:
mysql_XY
or mysqli_XY
. NOT both. See MySQL: Choosing an API.mysqli_*
, because mysql_*
is deprecated.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 ] )
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");
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.$link_identifier
in mysql_*
if you don't have multiple connections.{
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.Upvotes: 3
Reputation: 11650
$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
Upvotes: 0
Reputation: 44844
$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
Upvotes: 2