Jump to content

PHP/mySQL Returning values from query


HaikelChong
 Share

Recommended Posts

Code:<?php $resultName = mysql_query("SELECT name FROM tblUser ORDER BY totalCount DESC"); while($name = mysql_fetch_array($resultName)){ echo $name['name']; } ?>Hi. As you can see, I'm trying to get 'name' by order of totalCount DESC into variable $resultName. What my codes does is that it echoes out all the names. What i want to do is to echo the first value of name which has the highest totalCount. Then echo second value which has the second highest totalCount. and so on. Any help would be greatly appreciated. Thanks in advance.

Link to comment
Share on other sites

mysql_fetch_array gets an array where the index is numeric. i.e. 1, 2, 3, 4, etc.You want to use mysql_fetch_assoc where the index is the key of the variable. i.e. "name", "id", etc.To get the highest value use LIMIT 0, 1 after DESC

Link to comment
Share on other sites

Thank you, the scientist. Don't mind me asking another. I'm trying to get a hyperlink to call a php function. Is this the way to do it? I tested and it printed out 'no run'.<?phpfunction updateCount1(){mysql_query("UPDATE tblUser SET totalCount = totalCount + 1" . "WHERE userID = '" . $captureID1 . "'");echo $captureID1;} //end of updateCount1//random first person$randUser = rand(1,6);$resultUser = mysql_query("SELECT * FROM tblUser WHERE userID = '" . $randUser . "'");while ($row = mysql_fetch_assoc($resultUser)){$userID = $row['userID'];//$image = $row['image'];$name = $row['name'];}$captureID1 = $userID;if (isset($_GET['run'])) $linkchoice=$_GET['run'];else $linkchoice='';switch ($linkchoice){case 'first':updateCount1();break;case 'second':updateCount2();break;default :echo 'no run';}?><a href="?run=updateCount1">

Edited by HaikelChong
Link to comment
Share on other sites

Yes thank you for that. But it still doesn't work. I guess the way i call the function works. But not the function itself. In the following codes, I'm able to print out $name. But not $userID. Is there a reason why? FYI userID is the primary key in tblUser.<?php //random first person $randUser = rand(1,6); $resultUser = mysql_query("SELECT * FROM tblUser WHERE userID = '" . $randUser . "'"); while ($row = mysql_fetch_assoc($resultUser)) { $userID = $row['userID']; //$image = $row['image']; $name = $row['name']; } //$captureID1 = $userID;?>

Edited by HaikelChong
Link to comment
Share on other sites

Ingolme, I'm not really sure what you meant. I tried printing $userID the same way i did for $name. It works for $name but not for $userID. $userID shows nothing.thescientist, the code to connect to the database? I managed to print $name so there shouldn't be any problem with the connection to the database right? Correct me if i'm wrong.This is how i tried printing the variable. <div class="profilepic-right"><?php echo $name; ?></div>Yes it does display $name. But if i replaced it with $userID, it displays nothing. Thank you for any further help.

Link to comment
Share on other sites

Have you tried printing $row['userID'] to see if it's getting a value?You also might be changing the $userID value somewhere without meaning to. You haven't provided all your code so I can't know.

Link to comment
Share on other sites

I'm so sorry for disturbing. But this really means a lot to me. The value in $randUser is the same as $captureID1. I CAN echo $captureID1 and $randUser. But when i put either variables into the update mysql query, it didn't update it. It does update though when i put any number. Any idea why? Thank you!<?php //random first person $randUser = rand(1,6); $resultUser = mysql_query("SELECT * FROM tblUser WHERE userID = '" . $randUser . "'"); while ($row = mysql_fetch_assoc($resultUser)) { $name = $row['name']; } $captureID1 = $randUser; function updateCount1() { mysql_query("UPDATE tblUser SET totalCount = totalCount + 1 WHERE userID = '" . $captureID1 . "'"); } //end of updateCount1 ?> <div class="profilepic-left"><?php echo $captureID1; ?></div>

Link to comment
Share on other sites

That's because you're not passing the variable to the function. Also, I don't see you calling updateCount() anywhere.Functions don't access globalr variables and it's usually better if they don't, that way you can reuse the function with different values.

function updateCount1($id) {  return mysql_query("UPDATE tblUser SET totalCount = totalCount + 1 WHERE userID = '" . $id  . "'");}updateCount($captureID1);

Link to comment
Share on other sites

Hello again. Let me give you a rundown on how the codes are supposed to work. When you first enter the page, it will random out a question and 2 names. Based on the question, for example 'Who is fatter?', you have to click either of the 2 hyperlinks based on your answer. One hyperlink will pass in one userID of the name and sames goes for the 2nd hyperlink. That hyperlink will then call a php function to add 1 to the totalCount of that userID. Also, after clicking the hyperlink, it refreshes the page and another set of random question and 2 names will be displayed. And the process continues.With the help i've gotten here, i managed to get all the parts working. Except for one. Problem: After randoming the first set of a question and 2 names and clicking the hyperlink based on my answer, it updated the totalCount of the second set instead of the first set like it was supposed to. My assumption: What i think is that, when i click the hyperlink, it randoms the second set first before calling the php function thus updating the totalCount of the second set, instead of the other way round.Is there any solutions besides using ajax? Guess it's the way how i call the php function using the hyperlink. Thanks in advance.

<body><div id="page-wrapper">	<div id="header">		<div class="header-content">		<?php			function updateCount1($id)			{			mysql_query("UPDATE tblUser SET totalCount = totalCount + 1 WHERE userID = '" . $id . "'");			} //end of updateCount1						function updateCount2($id2)			{			mysql_query("UPDATE tblUser SET totalCount = totalCount + 1 WHERE userID = '" . $id2 . "'");			} //end of updateCount2						//random question			$randQuestion = rand(1,6);			mysql_connect("localhost","root","");			mysql_select_db("game");			$resultQuestion = mysql_query("SELECT * FROM tblQuestion WHERE questionIDQS = '" . $randQuestion . "'");			while ($row = mysql_fetch_assoc($resultQuestion))			{			$question = $row['question'];			}?>				<h1><?php echo $question; ?></h1>			</div>	</div>	<div id="content">				<div class="content-left"><a href="index.php?run=first"><img src="images/pick_left.png" width="167" height="74" alt="Pick me!" /></a></div>		<div class="content-mid">				<?php			//random first person			$randUser = rand(1,6);			$captureID1 = $randUser;			$resultUser = mysql_query("SELECT * FROM tblUser WHERE userID = '" . $randUser . "'");			while ($row = mysql_fetch_assoc($resultUser))			{			$name = $row['name'];			}		?>			<div class="profilepic-left"><?php echo $name; ?></div>					<?php 			//random second person			$randUser = rand(1,6);			$captureID2 = $randUser;						//check for duplicates			while ($captureID1 == $captureID2)			{			$randUser = rand(1,6);			$captureID2 = $randUser;			}						$resultUser = mysql_query("SELECT * FROM tblUser WHERE userID = '" . $randUser . "'");			while ($row = mysql_fetch_assoc($resultUser))			{			$name = $row['name'];			}		?>			<div class="profilepic-right"><?php echo $name; ?></div>					</div>				<?php			//runs update function			if (isset($_GET['run'])){			$linkchoice=$_GET['run'];			}			else $linkchoice='';						switch ($linkchoice)			{			case 'first':				updateCount1($captureID1);				break;						case 'second':				updateCount2($captureID2);				break;						default :				echo 'no run';			}					?>				<div class="content-right"><a href="index.php?run=second"><img src="images/pick_right.png" width="166" height="74" alt="Pick me!" /></a></div>			</div>   	<br class="clear" /></div></body>

Edited by HaikelChong
Link to comment
Share on other sites

  • 1 month later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...