学习如何从MYSQL中读取数据

时间:2015-07-08 06:19:11

标签: php mysql

我正在尝试学习php和mysql,我试图从我的数据库中读取数据,我在线跟踪并遇到错误,这里是我的代码

<?php
  include 'includes/conn.php';

   $query =sprintf("Select * from customer");
   $result = mysql_query($query);

   if (!$result) 
   {
   $message  ='from you see this then it's not connecting!'.mysql_error() ."\n";
      $message .= 'everything' .$query;
      die($message);
   }   

    while ($customer = mysql_fetch_assoc($result))
    {
       echo $row['cust_id'];
       echo $row['fname'];
       echo $row['lname'];
       echo $row['gender'];
       echo $row['dob'];
    }
mysql_free_result($result);
?>

//这是我尝试与数据库建立连接的地方

<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';

$db ='telmar_php';
$con = mysql_connect($dbhost,$dbuser,$dbpass);
mysql_select_db($db);
?>

3 个答案:

答案 0 :(得分:0)

替换

/**
 * CashRegister
 *
 * @ORM\Table(name="cash_registers")
 * @ORM\Entity
 * @ORM\HasLifecycleCallbacks
 */
class CashRegister
{

    ...

    /**
     * @var BinLocation
     *
     * @ORM\ManyToOne(targetEntity="BinLocation", inversedBy="cashRegisters")
     * @ORM\JoinColumn(name="bin_location_id", referencedColumnName="id")
     */
    private $binLocation;

    /**
     * @var BinLocation
     *
     * @ORM\ManyToOne(targetEntity="BinLocation", inversedBy="cashRegisters")
     * @ORM\JoinColumn(name="return_bin_location_id", referencedColumnName="id")
     */
    private $returnBinLocation;


    /**
     * @return BinLocation
     */
    public function getBinLocation()
    {
        return $this->binLocation;
    }

    /**
     * @return BinLocation
     */
    public function getReturnBinLocation()
    {
        return $this->returnBinLocation;
    }

    ...

}

/**
 * BinLocation
 *
 * @ORM\Table(name="bin_locations")
 * @ORM\Entity
 * @ORM\HasLifecycleCallbacks
 */
class BinLocation
{

    ...

    /**
     * @var CashRegister[]
     *
     * @ORM\OneToMany(targetEntity="CashRegister", mappedBy="binLocation") <= Here is the problem, in this case mappedBy need to be an array [binLocation, returnBinLocation]
     */
    private $cashRegisters;


    /**
     * @return CashRegister[]
     */
    public function getCashRegisters()
    {
        return $this->cashRegisters;
    }

    ...

}

此处您的字符串是$message ='from you see this then it's not connecting!'.mysql_error() ."\n"; 中的单引号导致问题

<强>与

it's

答案 1 :(得分:0)

...并显示您的搜索结果:

function statistics() 
{ 
/* Some other queries */ 
$b = $this->get_balances(); 
} 
function get_balances() 
{ 
$this->db->select('amount AS SavingAmount'); 
$query = $this->db->get_where('cashman_trial_balance',array('category_id'=>'176','clientI‌​d'=>$user['UserID'],'year'=>$year)); 
return $query->result(); 
}

到此:

while ($customer = mysql_fetch_assoc($result))

答案 2 :(得分:0)

Please review the below example for reading data my mysql using php.    

$sql = "SELECT id, firstname, lastname FROM MyGuests";
    $result = mysqli_query($conn, $sql);

    if (mysqli_num_rows($result) > 0) {
        // output data of each row
        while($row = mysqli_fetch_assoc($result)) {
            echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
        }
    } else {
        echo "0 results";

}