HollanderNL HollanderNL - 3 months ago 8
PHP Question

Select info from mysql-database and show on page

I am trying to show some info from a mysql database on a page, but I get an error. Used code:

<?php
if (isset($_GET["id"]));
$newsarticle = ($_GET["id"]);
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$dbname = 'XXXX';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);
$searchroute ="SELECT NewsText FROM News WHERE id=$newsarticle";
$handle = mysql_query($searchroute);
$NewsText = mysql_fetch_row($handle);
?>


And later in my code:

<?php echo "$NewsText" ?>


What am I doing wrong?

Answer

mysql_fetch_row() will return array so in your case it should be, echo $NewsText[0];

If you use mysql_fetch_array() then you can use echo $NewsText['NewsText'];