-3

I am trying to get result from as table in mySQL in the following code. It does not work. I am newbie in PHP.

Maybe someone can help whatws wrong in my code? I think that my SQL sentence is not written well. Is it?

<?php 

header("Content-type: text/html; charset=utf8");

// header('Content-Type:text/html;charset=utf-8');

//1. create connection

$connection=mysql_connect("localhost","user","pass");

if(!$connection){
    die("database connection failed:" . mysql_error());
}

//2. select database 

$db = mysql_select_db("database",$connection);

if(!db) {
    die("database connection failed:" . mysql_error());
}

//if i want to work with hebrew databases

mysql_query("SET NAMES 'utf8'",$connection); // reading heberer from phpadmin database - only for hebrew sites 

$MessageNo = $_POST['MsgNo']

//$MessageN0=(int)$MessageNo

$query = mysql_query("SELECT * FROM MessageContent WHERE MsgNo='$MessageNo'");;;

$messages = array();

while ($row = mysql_fetch_array($query)) {
    $messages[] = array('MsgContId' => $row['MsgContId'], 'MsgNo' => $row['MsgNo'],'MsgContent' => $row['MsgContent'], 'AddedBy' => $row['AddedBy'],'AddedAt'=>$row['AddedAt']);
}

// echo json_encode(array('users' => $users));

echo json_encode($messages);

mysql_close($connection);

?>
animuson
  • 53,861
  • 28
  • 137
  • 147
  • you've many syntax errors which is off-topic. This http://php.net/manual/en/function.error-reporting.php will help you to figure out what they are. – Funk Forty Niner Apr 27 '15 at 13:56
  • 1
    Please, [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Apr 27 '15 at 14:18

1 Answers1

1

I can see on this line you have formatting errors:

$query = mysql_query("SELECT * FROM MessageContent WHERE MsgNo='$MessageNo'");;;

Remove the extra two semi-colons:

$query = mysql_query("SELECT * FROM MessageContent WHERE MsgNo='$MessageNo'");

Also this:

$MessageNo = $_POST['MsgNo']

Needs a semi-colon:

$MessageNo = $_POST['MsgNo'];

Additionally you're using deprecated functions. I would suggest you look into MySQLi functions or PDO.

Consult: mysqli with prepared statements, or PDO with prepared statements.

EDIT: Also in your second query, you're not using the connection string:

$query = mysql_query("SELECT * FROM MessageContent WHERE MsgNo='$MessageNo'",$connection);

Add error reporting to the top of your file(s) which will help find errors.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

Sidenote: Error reporting should only be done in staging, and never production.

  • Also add or die(mysql_error()) to mysql_query().

Sidenote:

Community
  • 1
  • 1
John Bell
  • 2,350
  • 1
  • 14
  • 23