PHP代码。 怎么了? [关闭]

PHP代码。 怎么了?  [关闭]

问题描述:

I'm new to PHP and MySQL. I try to reach out MySQL table (3 column) and list rows. However following code does not show anything (blank page). Does any one has an idea?

<?php

$db = mysql_connect('localhost', 'root', 'root', 'database_name');

if(!$db){
die('Connect Error (' . mysqli_connect_errno() . ') '
        . mysqli_connect_error());
}

$result = "SELECT * FROM table_name";

$query = mysqli_query($result);

$row = mysqli_fetch_array($query, MYSQLI_NUM);

echo $row[0];

mysqli_free_result($result);

mysqli_close($db);

?>

我是PHP和MySQL的新手。 我尝试伸出MySQL表(3列)并列出行。 但是下面的代码没有显示任何内容(空白页)。 有没有人有想法? p>

 &lt;?php 
 
 $ db = mysql_connect('localhost','root','root','database_name')  ; 
 
if(!$ db){
die('Connect Error('。mysqli_connect_errno()。')'
.mysqli_connect_error()); 
} 
 
 $ result =“SELECT * FROM table_name  “; 
 
 $ query = mysqli_query($ result); 
 
 $ row = mysqli_fetch_array($ query,MYSQLI_NUM); 
 
echo $ row [0]; 
 
mysqli_free_result($ result); \  n 
mysqli_close($ db); 
 
?&gt; 
  code>  pre> 
  div>

You're switching between mysql and mysqli.

You should be connecting to the database like this:

$db = new mysqli('host', 'username', 'password', 'database');

Run queries like:

$query = "SELECT * FROM my_table";
$result = $db->query($query);

Also, loop through the results like so:

while($row = mysqli_fetch_array($result)) {
     echo $row['column_name'];
}

There are more advanced concepts like prepared statements, but this should get you in the right direction.

I think firstly, check your data to make sure there is at least one row. If there is no row, echo $row[0] will result in error. But as you did not see anything on screen, better to check at php.ini for error_reporting settings. Change it to E_NOTICE and all errors/notice will be displayed to you.

You are mixing your database layer API.

MySQL_* and MySQLI_* Functions are different, contain different usages and such.

You should move away from MySQL_* Functions. Read the manual here:

http://www.php.net/manual/en/book.mysqli.php

You have the choice between object oriented or procedural style of MySQL.

Connect to the Database by reading about the __construct.