retrieving values from mysql

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
danwguy
Forum Contributor
Posts: 256
Joined: Wed Nov 17, 2010 1:09 pm
Location: San Diego, CA

retrieving values from mysql

Post by danwguy »

Ok, so I have a table called test, in the table there is rows for everyday of the week, titled monday, teusday... etc for monday there are time, 6am, 10am, 11am...etc I am trying to get those times to be echo-ed onto the screen but for some reason I can't figure it out. I have always run sql commands with

Code: Select all

 while($row = mysql_fetch_assoc($sql)) {
echo $row['monday'];
and so on, but there is no while loop for this one, I am using

Code: Select all

sql = mysql_query("SELECT monday FROM test WHERE first_name='Mary'");
nowhow do I get that array of 6am, 7am, 10a, 11am, and so on, out to the screen? Thank you for the advance.
fugix
Forum Contributor
Posts: 207
Joined: Fri Mar 18, 2011 8:01 pm

Re: retrieving values from mysql

Post by fugix »

do you have the times for monday stored in an array?
danwguy
Forum Contributor
Posts: 256
Joined: Wed Nov 17, 2010 1:09 pm
Location: San Diego, CA

Re: retrieving values from mysql

Post by danwguy »

this is what I have...

Code: Select all

<?php
session_start();
require("connect.php");
$day = "monday";
$sql = 'SELECT monday FROM test WHERE first_name=\'Mary\'';
	if(!$sql) {
		echo "Nope - " .mysql_error();
		exit();
		}
echo $row['monday'];


?>
picture of the table... http://www.theials.com/table.jpg
table called test, with rows first_name, last_name, monday, tuesday, wednesday, thrsday, friday, saturday, sunday, ID
in monday the values stored are 6am, 10am, 11am, 12am, 12pm, 1pm, 5pm, 6pm, 7pm, 8pm, 9pm, 10pm, 11pm.
So when I use

Code: Select all

$sql = mysql_query("SELECT monday FROM test WHERE first_name='Mary'");
how would I then get all those 6am, 10am... and so on out to the screen, since I can't say while($row= mysql_fetch_assoc($sql)) { echo $row['monday']; }
because I'm selecting monday, so there is no fetch_array or whatever.
fugix
Forum Contributor
Posts: 207
Joined: Fri Mar 18, 2011 8:01 pm

Re: retrieving values from mysql

Post by fugix »

you can use the while loop that you stated above..why do you think you wouldnt be able to?
danwguy
Forum Contributor
Posts: 256
Joined: Wed Nov 17, 2010 1:09 pm
Location: San Diego, CA

Re: retrieving values from mysql

Post by danwguy »

when I put the while loop in

Code: Select all

<?php
session_start();
require("connect.php");
$day = "monday";
$sql = 'SELECT monday FROM test WHERE first_name=\'Mary\'';
	if(!$sql) {
		echo "Nope - " .mysql_error();
		exit();
		}
while($row = mysql_fetch_assoc($sql)) {
echo $row['monday'];
}


?>
i get this error...
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /hermes/bosweb/test.php on line 10
fugix
Forum Contributor
Posts: 207
Joined: Fri Mar 18, 2011 8:01 pm

Re: retrieving values from mysql

Post by fugix »

Code: Select all

$sql = 'SELECT monday FROM test WHERE first_name=\'Mary\'';
needs to be...

Code: Select all

$sql =  mysql_query("SELECT monday FROM test WHERE first_name='Mary''');
then try it again
danwguy
Forum Contributor
Posts: 256
Joined: Wed Nov 17, 2010 1:09 pm
Location: San Diego, CA

Re: retrieving values from mysql

Post by danwguy »

same result
fugix
Forum Contributor
Posts: 207
Joined: Fri Mar 18, 2011 8:01 pm

Re: retrieving values from mysql

Post by fugix »

dunno if you copy pasted my code...but the last quotation should be a double quotation not a single quotation like i have
Post Reply