本文整理汇总了PHP中CS50::query方法的典型用法代码示例。如果您正苦于以下问题:PHP CS50::query方法的具体用法?PHP CS50::query怎么用?PHP CS50::query使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CS50
的用法示例。
在下文中一共展示了CS50::query方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: render_next
/**
* Renders the next profile to view.
*/
function render_next()
{
//find current viewnum
$viewNum = CS50::query("SELECT viewNum FROM users WHERE id = ?", $_SESSION["id"]);
$viewnum = $viewNum[0]["viewNum"];
//find id of last viable profile
$last = CS50::query("SELECT MAX(id) FROM users");
//increment viewnum to next viable profile
do {
CS50::query("UPDATE users SET viewNum = viewNum + 1 WHERE id = ?", $_SESSION["id"]);
$viewnum++;
if ($viewnum > $last[0]["MAX(id)"]) {
apologize("You've seen e'rybody!");
break;
}
} while (count(CS50::query("SELECT * FROM users WHERE id = ?", $viewnum)) == 0 || $viewnum == $_SESSION["id"]);
// render homepage; show new profile.
render("home.php", ["title" => "home", "profile" => prof_lookup($viewnum)]);
}
示例2: apologize
<?php
// you've been given the user's username from a form
$username = $_POST["username"];
// step 1. get the user's id given their username
$result = CS50::query("SELECT id FROM users WHERE username = ?", $username);
if ($result === false) {
apologize("Error accessing user data!");
}
// note the need for [0]
$user_id = $result[0]["id"];
// step 2. get all zaps sent to user
$zaps = CS50::query("SELECT * FROM zaps WHERE sent_to = ?", $user_id);
if ($zaps === false) {
apologize("Error getting zaps!");
}
// step 3. print out all zaps using PHP (hint: use foreach)
?>
<ul>
<?php
foreach ($zaps as $zap) {
?>
<li>
<?php
echo $zap["length"];
?>
seconds
<?php
if ($zap["seen"]) {
?>
(seen)
示例3: user_events
<?php
// Mostly original code
// configuration
require "../startbootstrap-business-casual-1.0.4/config.php";
//query username from users and stock from recruiter_userss
$rows = CS50::query("SELECT company, event_date, event_time FROM events WHERE school = ? AND company= ?", $_POST["school"], $_POST["company"]);
if ($rows != false) {
$new_insertion = CS50::query("INSERT INTO user_events (company, event_time, event_date, user_id) \n VALUES (?, ?, ?, ?)", $_POST["company"], $rows[0]["event_time"], $rows[0]["event_date"], $_SESSION["id"]);
if ($new_insertion !== false) {
redirect("students.php");
}
} else {
apologize("sorry, there is no event for this company");
}
示例4: apologize
// validate inputs
if (empty($_POST["username"])) {
apologize("You must provide a username.");
} else {
if (empty($_POST["password"])) {
apologize("You must provide a password.");
} else {
if (empty($_POST["confirmation"]) || $_POST["password"] != $_POST["confirmation"]) {
apologize("Those passwords did not match.");
}
}
}
// try to register user
$rows = CS50::query("INSERT IGNORE INTO users (username, hash) VALUES(?, ?)", $_POST["username"], password_hash($_POST["password"], PASSWORD_DEFAULT));
if ($rows !== 1) {
apologize("That username appears to be taken.");
}
// get new user's ID
$rows = CS50::query("SELECT LAST_INSERT_ID() AS id");
if (count($rows) !== 1) {
apologize("Can't find your ID.");
}
$id = $rows[0]["id"];
// log user in
$_SESSION["id"] = $id;
// redirect to portfolio
redirect("/");
} else {
// else render form
render("register_form.php", ["title" => "Register"]);
}
示例5: apologize
// configuration
require "../includes/config.php";
// if user reached page via GET (as by clicking a link or via redirect)
if (empty($_SESSION["id"])) {
apologize("You don't have the proper authorization to access this page...");
}
if ($_SERVER["REQUEST_METHOD"] == "GET" && empty($_GET["match_id"])) {
$matches = CS50::query("SELECT * FROM matches");
render("remove_scores_view.php", ["title" => "Remove Scores", "matches" => $matches, "sport_map" => $sport_map]);
} else {
if ($_SERVER["REQUEST_METHOD"] == "GET") {
$remove = CS50::query("SELECT * FROM matches WHERE id = ?", $_GET["match_id"]);
if ($remove == false) {
apologize("No match found.");
}
$college_names = "BK, BR, CC, DC, ES, JE, MC, PC, SM, SY, TC, TD";
foreach ($remove[0] as $key => $value) {
if (strpos($college_names, $key) !== false) {
CS50::query("UPDATE stats SET total = total - ?," . $remove[0]["sport"] . " = " . $remove[0]["sport"] . " - ? WHERE college = ?", $value, $value, $key);
}
}
if (CS50::query("DELETE FROM matches WHERE id = ?", $_GET["match_id"]) == false) {
apologize("Error deleting match from database");
}
render("score_removed.php", ["title" => "Success"]);
}
}
?>
示例6: render
<!-- original code -->
<?php
// configuration
require "../startbootstrap-business-casual-1.0.4/config.php";
// if user reached page via GET (as by clicking a link or via redirect)
if ($_SERVER["REQUEST_METHOD"] == "GET") {
// eer form
render("register_company_company.php");
}
// else if user reached page via POST (as by submitting a form via POST)
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// validate submission
if (empty($_POST["company"]) or empty($_POST["company_code"])) {
apologize("Sorry");
}
//add the user to the the database
$new_insertion = CS50::query("INSERT INTO companies (company, company_code) \n VALUES (?, ?)", $_POST["company"], $_POST["company_code"]);
// insert new user into database
if ($new_insertion === false) {
apologize("Cannot log you in at this time");
} else {
redirect("register_company.php");
}
}
示例7: render
<?php
// configuration
require "../includes/config.php";
// if user reached page via GET (as by clicking a link or via redirect)
if ($_SERVER["REQUEST_METHOD"] == "GET") {
render("prof_form.php", ["title" => "Edit"]);
} else {
if ($_SERVER["REQUEST_METHOD"] == "POST") {
//insert new interest into interests database
CS50::query("INSERT INTO interests (user_id, interest) VALUES(?, ?)", $_SESSION["id"], $_POST["interest"]);
// render updated profile
render("prof_page.php", ["title" => "Profile", "profile" => prof_lookup($_SESSION["id"])]);
}
}
示例8: matches
<?php
// configuration
require "../includes/config.php";
// if user reached page via GET (as by clicking a link or via redirect)
if ($_SERVER["REQUEST_METHOD"] == "GET") {
//logic to deal with this new proposal
$viewNum = CS50::query("SELECT viewNum FROM users WHERE id = ?", $_SESSION["id"]);
$viewnum = $viewNum[0]["viewNum"];
//try deleting the complement of this proposal. if deletion works, we have a match.
//deletion is optional, but an optimization to keep proposals table small.
if (CS50::query("DELETE FROM proposals WHERE user_id = ? AND proposee_id = ?", $viewnum, $_SESSION["id"])) {
//we have a match; insert into match table.
CS50::query("INSERT INTO matches (user1_id, user2_id) VALUES(?, ?)", $_SESSION["id"], $viewnum);
} else {
// no match yet; insert proposal into proposals table
CS50::query("INSERT INTO proposals (user_id, proposee_id) VALUES(?, ?)", $_SESSION["id"], $viewnum);
}
//show the next profile. This function is defined in helpers.php
render_next();
}
示例9: VALUES
<?php
// configuration
require "../includes/config.php";
// if form was submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if ($_POST['interest']) {
//This is to add an interest to the database
CS50::query("INSERT INTO `interests` (`user_id`, `interest`) VALUES (?,?)", $_SESSION["id"], $_POST['interest']);
}
redirect("index.php");
} else {
// render form
redirect("index.php");
}
?>
示例10: render
<!-- original code -->
<?php
// configuration
require "../startbootstrap-business-casual-1.0.4/config.php";
// if user reached page via GET (as by clicking a link or via redirect)
if ($_SERVER["REQUEST_METHOD"] == "GET") {
// else render form
render("students.html", ["title" => "buy"]);
} else {
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Record Input
CS50::query("INSERT INTO student_update (FirstName, LastName, Email, University) VALUES (?, ?, ?,?)", $_POST["FirstName"], $_POST["LastName"], $_POST["Email"], $_POST["University"]);
}
}
redirect("/");
示例11: apologize
} else {
if (empty($_POST["newpassword"])) {
apologize("You must produce a password");
} else {
if (empty($_POST["confirmation"])) {
apologize("You must produce a password");
} else {
if ($_POST["newpassword"] != $_POST["confirmation"]) {
apologize("Passwords must match");
} else {
$userstats = CS50::query("SELECT * FROM users WHERE id = ?", $_SESSION["id"]);
$username = $userstats[0]["username"];
// check username for correctnes
if ($username != $_POST["username"]) {
apologize("Wrong username");
} else {
if (!password_verify($_POST["oldpassword"], $userstats[0]["hash"])) {
apologize("Wrong original password");
} else {
// update password
CS50::query("UPDATE users SET hash=? where id =?", password_hash($_POST["newpassword"], PASSWORD_DEFAULT), $_SESSION["id"]);
redirect("index.php");
}
}
}
}
}
}
}
}
}
示例12: PostmarkClient
// Store id of article in variable
$article = $_POST["article_id"];
// update mySQL
CS50::query("UPDATE portfolio SET status = 3 WHERE id = ?", $article);
// set submission destination and subject
$email_to = "phillyfan362@gmail.com";
$email_subject = "Crimson Article Submission";
// Actual article text variable
$articles = $_POST['articles'];
// Start of email message
$email_message = "Form details below.\n\n";
// Get title of article
$pieces = CS50::query("SELECT title FROM portfolio WHERE id = ?", $article);
$piece = $pieces[0]["title"];
// Get name of comper
$compers = CS50::query("SELECT name, email FROM users WHERE userid = ?", $_SESSION["id"]);
$name = $compers[0]["name"];
$email = $compers[0]["email"];
// Craft e-mail message body
$email_message .= "Comper: " . $name . "\r\n";
$email_message .= "Article Title: " . $piece . "\r\n";
$email_message .= "Comments: " . $articles . "\r\n";
$client = new PostmarkClient("211bda55-ecef-447c-ba35-7b2ca54e802f");
// Send email
$sendResult = $client->sendEmail("manavkhandelwal@college.harvard.edu", "phillyfan362@gmail.com", "Comper Article Submissions", "{$email_message}");
// Redirect
redirect("/");
} else {
if ($_SERVER["REQUEST_METHOD"] == "GET") {
redirect("/");
}
示例13: apologize
<?php
//configuration
require "../includes/config.php";
//create new array to store history information
$history = CS50::query("SELECT * FROM history WHERE user_id = ?", $_SESSION["id"]);
if (count($history) == 0) {
apologize("No transactions recorded.");
}
// dump($history);
//render buy form
render("history_form.php", ["title" => "History", "history" => $history]);
示例14: foreach
<?php
//configuration
require "../includes/config.php";
//query portfolio
$rows = CS50::query("SELECT * FROM portfolios WHERE user_id = ?", $_SESSION["id"]);
//new array to story portfolio contents
$positions = [];
//go through each row
foreach ($rows as $row) {
//look up symbol from row's stock on Yahoo
$stock = lookup($row["symbol"]);
//if look up was successful
if ($stock !== false) {
//fill information into array 'positions'
$positions[] = ["name" => $stock["name"], "symbol" => $row["symbol"], "price_per_stock" => $stock["price"], "shares" => $row["shares"], "total_price" => $stock["price"] * $row["shares"]];
}
}
//query user's cash
$cash = CS50::query("SELECT cash FROM users WHERE id = ?", $_SESSION["id"]);
//render portfolio
render("portfolio.php", ["positions" => $positions, "title" => "Portfolio", "cash" => $cash]);
示例15: json_encode
<?php
require __DIR__ . "/../dependencies/CS50_Provided/config.php";
$data = [];
// query the database for the weight inputs for this athlete
$data = CS50::query("SELECT WeightClass, Weight, Date FROM Weights WHERE Name = ?", $_GET["name"]);
// output places as JSON (pretty-printed for debugging convenience)
header("Content-type: application/json");
print json_encode($data, JSON_PRETTY_PRINT);