开发者

SQL and PHP database access

I'm very new to PHP, SQL I've worked with using Coldfusion but only with very simple queries. In coldfusion to access a specific database

<cfquery dbname="blah">

I know in PHP I have to use mysql_query() and mysql_connect(), and here is the code I have, so I 开发者_运维知识库understand how to access a server and a table, but not the database. How can this be done?

<?php
$sql_branch = "SELECT BranchNum
              FROM Branch WHERE
              branchName = '$_POST[branch]'";
$connect = mysql_connect('students','xxxxxxx','xxxxxxx');
if(mysql_query($sql_branch, $connect)) {
  $branch = mysql_query($sql_branch, $connect);
}
else {
  echo "error".mysql_error();
}
$sql_result = "USE henrybooks;
              SELECT AuthorFirst, AuthorLast, OnHand, Title
              FROM Inventory i, Wrote w, Author a, Book b
              WHERE i.BookCode = b.BookCode AND
              i.BookCode = w.BookCode AND a.AuthorNum =
              w.AuthorNum AND i.BranchNum = $branch";
if(mysql_query($sql_result, $connect)) {
  $result = mysql_query($sql_result, $connect);
}
else {
  echo "Error".mysql_error();
}

Also I'm unsure if my Error checking is right, my professor did not really explain how that works exactly.


Find out the database name and select it before making any queries:

$connect = mysql_connect('students','xxxxxxx','xxxxxxx');
mysql_select_db('dbName', $connect);

Documentation for mysql_select_db.


You probably want to use mysql_select_db:

$connect = mysql_connect('students','xxxxxxx','xxxxxxx');
mysql_select_db( "blah", $connect );


Use mysql_select_db to connect to the database. Most of the mysql_ functions should be what you are looking for when working with mysql databases.


Are you looking for mysql_select_db?

You can find all mysql functions here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜