<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:"</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"</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"</span>.mysql_error()); } print_r(mysql_fetch_array(<span>$result</span>));</code>
用mysql_query查询的结果并不能直接输出,会打印出Resource id #4。
用mysql_fetch_array转换成数组在输出
以上就介绍了php 与mysql 连接,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。