1. 使用mysql_connect()
1 error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED); 2 $con = mysql_connect("127.0.0.1","root","root"); 3 if (!$con) 4 { 5 die('Could not connect: ' . mysql_error()); 6 } 7 mysql_select_db("test", $con); 8 $result = mysql_query("SELECT * FROM test_oo"); 9 10 while($row = mysql_fetch_array($result)) 11 { 12 // echo gettype($row['id']) . "--" . gettype($row['smallint']); 13 echo "<br />"; 14 var_dump($row['id']); 15 if(is_int($row['id'])) 16 { 17 echo 'int'; 18 }elseif(is_string($row['id'])) 19 { 20 echo 'string'; 21 } 22 }
2. 使用mysqli
$con=mysqli_connect("localhost","root","root","test"); // 检查连接 if (!$con) { die("连接错误: " . mysqli_connect_error()); } $result = mysqli_query($con,'select * from test_oo'); $arr = mysqli_fetch_array($result,MYSQLI_ASSOC); var_dump($arr);
3. 使用pdo
1 error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED); 2 $user = 'root'; 3 $pass = 'root'; 4 try { 5 $dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass); 6 foreach($dbh->query('SELECT * from test_oo') as $row) { 7 print_r($row); 8 echo '<br>'; 9 settype($row['id'],'int'); 10 echo gettype($row['id']); 11 echo '<br>'; 12 } 13 $dbh = null; 14 } catch (PDOException $e) { 15 print "Error!: " . $e->getMessage() . "<br/>"; 16 die(); 17 }