read record from mysql db

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cokelly
Forum Newbie
Posts: 2
Joined: Tue Jul 02, 2013 3:39 pm

read record from mysql db

Post by cokelly »

hello all- forgive the rookie question.
The script below is intended to read a database record from "MySQL-db" from a webquery http://mydomain.com/article.php?ArticleID=?? and echo the result to the screen.
I'm not fully certain that the script is connecting to MySQL_db and it is not returning any result.
MySQL _db is set up correctly with data in it. I can access it via a MySQL workbench so i'm pretty sure my script syntax below is wrong.

Code: Select all

<?php
// this is articles.php
// reads an database record from MySQL_db
//and echos the result to the screen
mysql_connect("localhost", "mysql_userid", "mysql_password");
// open the mysql db where the articles are stored
mysql_select_db("mysql_db");

$ArticleID = $_GET['ArticleID'] ;
        $result = mysql_query("SELECT Articles.ArticleID, Articles.Article FROM Articles WHERE ArticleID='$ArticleID' ");
             echo "<p align=justify>";
             printf(" %s<br>\n", mysql_result($result,0,"Article"));
             echo "</p>";
?>
any ideas?

thanks
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: read record from mysql db

Post by Christopher »

Add error checking:

Code: Select all

$errorMsg = '';
$db = mysql_connect("localhost", "mysql_userid", "mysql_password");
if (mysql_errno($db) == 0) {
  mysql_select_db("mysql_db", $db);
  if (mysql_errno($db) == 0) {
    $ArticleID = $_GET['ArticleID'] ;
    $result = mysql_query("SELECT Articles.ArticleID, Articles.Article FROM Articles WHERE ArticleID='$ArticleID' ", $db);
    if (mysql_errno($db) == 0) {
      echo "<p align=justify>";
      printf(" %s<br>\n", mysql_result($result,0,"Article"));
      echo "</p>";
    } else {
      $errorMsg = "Query error: " . mysql_error($db);
    }
  } else {
    $errorMsg = "Connect error: " . mysql_error($db);
  }
} else {
    $errorMsg = "Select DB error: " . mysql_error($db);
}
if (errorMsg) {
  echo "Error: errorMsg<br/>";
}
Remember that mysql is no longer supported. Use mysqli or PDO.
(#10850)
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: read record from mysql db

Post by Celauran »

PDO is far less cumbersome to use IMO. Don't forget to escape your inputs or use prepared statements!

Code: Select all

$ArticleID = mysql_real_escape_string($_GET['ArticleID']);
PDO example:

Code: Select all

$sql = new PDO('mysql:host=localhost;dbname=mydb', 'dbuser', 'dbpass');

$query = "SELECT ArticleID, Article FROM Articles WHERE ArticleID = :id";
$stmt = $sql->prepare($query);
$stmt->execute(array(':id' => $_GET['ArticleID']));
$result = $stmt->fetchAll();
Post Reply