<code><span>$db_host</span>=<span>'localhost'</span>; <span>//数据库主机</span><span>$db_database</span>=<span>'test'</span>; <span>//数据库名</span><span>$db_username</span>=<span>'root'</span>; <span>//账户名</span><span>$db_password</span>=<span>''</span>; <span>//密码</span><span>$connection</span>=mysql_connect(<span>$db_host</span>,<span>$db_username</span>,<span>$db_password</span>);<span>//连接到数据库</span> mysql_query(<span>"set names 'utf8'"</span>);<span>//编码转化</span><span>if</span>(!<span>$connection</span>){ <span>die</span>(<span>"could not connect to the database:</br>"</span>.mysql_error());<span>//诊断连接错误</span>} <span>$db_selecct</span>=mysql_select_db(<span>$db_database</span>);<span>//选择数据库</span><span>if</span>(!<span>$db_selecct</span>) { <span>die</span>(<span>"could not to the database</br>"</span>.mysql_error()); } <span>$query</span>=<span>"select * from eload_category "</span>;<span>//构建查询语句</span><span>$result</span>=mysql_query(<span>$query</span>);<span>//执行查询</span><span>if</span>(!<span>$result</span>) { <span>die</span>(<span>"could not to the database</br>"</span>.mysql_error()); } print_r(mysql_fetch_array(<span>$result</span>));</code>
用mysql_query查詢的結果並不能直接輸出,會印出Resource id #4。
用mysql_fetch_array轉換成陣列在輸出
以上就介紹了php 與mysql 連接,包括了方面的內容,希望對PHP教程有興趣的朋友有所幫助。