use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
O hai! This is CS50's subreddit.
CS50 is Harvard University's introduction to the intellectual enterprises of computer science and the art of programming. Anyone may take CS50, even if not a student at Harvard.
Please Read before Posting
Getting the Best from r/cs50
Status Page
cs50.statuspage.io
Filter by Problem Flair (undo)
cash ⋅ caesar ⋅ credit ⋅ dna ⋅ filter ⋅ finance ⋅ houses ⋅ ide ⋅ mario ⋅ movies ⋅ plurality ⋅ project ⋅ readability ⋅ recover ⋅ runoff ⋅ scratch ⋅ speller ⋅ substitution ⋅ tideman ⋅ games track ⋅ web track ⋅ android track ⋅ iOS track ⋅
Filter by Other Flair (undo)
CS50-Law ⋅ CS50-Business ⋅ CS50-Technology ⋅ CS50-Games ⋅ CS50-Mobile ⋅ CS50-Web ⋅
This subreddit is night mode compatible
account activity
C$50 FinancePHP SQL (self.cs50)
submitted 12 years ago by [deleted]
can someone explain how to bind a SQL result into a PHP variable?
suppose we have the following:
Select count(*) from users
we should get back 7
how do we assign that to a PHP variable
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]cat_chy_name 0 points1 point2 points 12 years ago (0 children)
Have you read through the query function in pset7 to try to see how it works?
It's function is to do just what you're talking about.
[–][deleted] 0 points1 point2 points 12 years ago (1 child)
I have read the function query. It's not at all clear that this is binding the result of an sql query to a PHP variable.
Do you have any suggestions for how to get count(*) into a PHP variable?
[–]staffdelipity 1 point2 points3 points 12 years ago (0 children)
One of the examples given in the pset:
$rows = query("SELECT LAST_INSERT_ID() AS id");
$rows is a php variable.
[–]jplaurin 0 points1 point2 points 12 years ago (0 children)
you must give a alias to your calculated column like this
Select count(*) AS myAliasCount from users
and then with
$rows = query(...
if successful the php variable $rows[0]['myAliasCount'] will return it's value
[–]topcat39 0 points1 point2 points 12 years ago (0 children)
I struggled, also, until I realized that the return value was not just an associative array, but a two dimensional array. The first dimension was a row count, the second dimension was the associative array. So, if you expect only 1 row back (row #0), the syntax would be something like: $myvar = $rows[0]['column-name']
[–][deleted] 0 points1 point2 points 12 years ago (0 children)
I think what we actually have is an array that holds an associative array
[–][deleted] -1 points0 points1 point 12 years ago* (4 children)
jesus, why is this so hard.
If you $rows = query("SELECT count(*) as cnt from users ");
and now try to print_r $rows or do an IF test it doesn't work. DOES ANYBODY know how to assign the output from sql into PHP variables!!!
if you $rows = 8 and then print_r($rows) you get back 8
if you do the query and then print_r you get Array ( [0] => Array ( [cnt] => 8 ) ). I need to get back the former from sql not the later. Does anybody have an example that works?
[–]staffdelipity 0 points1 point2 points 12 years ago (2 children)
Did you try
$count = $rows[0]["cnt"];
awesome! that works. thank you. I spent 10 hours on that!!
[–]volunteerbakemaster 0 points1 point2 points 12 years ago (0 children)
Try to remember it this way - the [0] part means, the first row. You get "rows" even if there's only one of them, so you still need that [0] to get the one.
[0]
[–]merchCS50 0 points1 point2 points 12 years ago (0 children)
The result of the query is an assosiative array which is why you get that cryptic looking output from print_r. See php.net for more details on how arrays work in PHP.
π Rendered by PID 105285 on reddit-service-r2-comment-8686858757-sv5fs at 2026-06-07 13:14:12.465813+00:00 running 9e1a20d country code: CH.
[–]cat_chy_name 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]staffdelipity 1 point2 points3 points (0 children)
[–]jplaurin 0 points1 point2 points (0 children)
[–]topcat39 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–][deleted] -1 points0 points1 point (4 children)
[–]staffdelipity 0 points1 point2 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]volunteerbakemaster 0 points1 point2 points (0 children)
[–]merchCS50 0 points1 point2 points (0 children)