简体   繁体   中英

Connecting to mySQL db using php error checking

When I run the script below on my browser all that prints out is 0: If I erase MySQL_errno I get nothing. How would I get a more detailed error?

 <?php
  $link = mysql_connect("192...", "root", "");
  mysql_select_db("visitors_tables");

  echo mysql_errno($link) . ": " . mysql_error($link). "\n";
  echo mysql_error();
   $query = "SELECT * FROM visitors";
  $result = mysql_query($query);
  while ($line = mysql_fetch_array($result))
  {
     foreach ($line as $value)
       {
     print "$value\n";
  }
  }
mysql_close($link);
?>

You aren't getting an error. But since mysql_errno() and mysql_error() is in no condition, it gets echoed anyway.

You can do it like this:

function showError($link) {
    return mysql_errno($link).': '.mysql_error($link);
}

$link = mysql_connect("192...", "root", "") or die(showError($link));
mysql_select_db("visitors_tables") or die(showError($link));
//etc.
$result = mysql_query($query) or die(showError($link));

or ...; only gets called when there actually is an error.

try using the below code. & insert the column_name you want to fetch at echo $line['column_name']

<?php
  $link = mysql_connect("192...", "root", "");
  if($link){
    mysql_select_db("visitors_tables");
  }
  $query = "SELECT * FROM visitors";
  $result = mysql_query($query);
  while ($line = mysql_fetch_array($result)){
    echo $line['column_name']
  }
mysql_close($link);
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM