Jump to content

MYSQL QUERY HELP!!! :(


mr msg

Recommended Posts

Hi I have done this IF statment using PHP and MYSQL so far and its not showing the results I want its 1:18am and I need to get it done now! :( can anybody help, please..

 

 

$result = mysql_query("SELECT * FROM People WHERE Access='1'");

if ($result)

  {

  echo"Access";

  }

else

  {

  echo"No Access";

  }

?>  :shrug: :shrug:

Link to comment
Share on other sites

Hi ix.selken, this is the code: (I am using Phpmyadmin for mysql database and dreamweaver to make the php in)

 

 

<?php

$con = mysql_connect("********","********","********");

if (!$con)

  {

  die('Could not connect: ' . mysql_error());

  }

 

mysql_select_db("names", $con);

 

$result = mysql_query("SELECT * FROM People WHERE Access='1'");

if ($result)

  {

  echo"Access";

  }

else

  {

  echo"No Access";

  }

?>

 

 

 

Hi abdbuet oh rite thats cool but I need an IF statement in there as I need 2 optional outcomes, thats the thing. I just need it to say if value in the "Access" colum equlas "1" then it will "Access" the user if not "No Access", Thanks.

 

 

Link to comment
Share on other sites

I also forgot to add that When I run this query it only displays one output either "Access" or "No Access".  When I change the value to "0" it still stays the same output as before. so the query does work it just doesn't show either option when the value is changed to either "1" or "0", if you get me, thanks.

Link to comment
Share on other sites

mysql_query() for a SELECT query either returns a result resource if the query executed without error or it returns a FALSE value if the query fails due to an error. A query that matches zero rows is a successful query and returns a result resource.

 

Your if/else code -

if ($result)
  {
  echo"Access";
  }
else
  {
  echo"No Access";
  }

 

will echo "Access" as long as the query executes without any errors, even if it matches zero rows.

 

You would want to use mysql_num_rows() inside the TRUE branch of the if/else statement to find out if the query returned any rows.

 

Also, your query makes little sense. You are only asking if there are any rows with Access='1'. Do you in fact want to test if a specific visitor has his access value set to 1?

Link to comment
Share on other sites

Hi PFMaBiSmAd, oh rite so what I am trying to do is, I have made a visual basic program were when the member log s in it sends a value "1" to the mysql n wen the member logs out it sends a value "0" to the mysql.

 

So im using this php and mysql to connect to databse so when user log s  in, the php will pik up on the databse and the value wil be"1" it will show a red image in the php, then when user logs out it that value in mysql change to "0" then the colours goes green in the php, this is what im trting to do but not working this is the code I got so far, please let me know. i dont think the "mysql_num_rows()" will work for what I am trying to do.

 

 

<?php

$con = mysql_connect("*****","*****","*****");

if (!$con)

  {

  die('Could not connect: ' . mysql_error());

  }

 

mysql_select_db("table");

 

$query = mysql_query("SELECT * FROM 'people' WHERE 'Access'='1'");

 

if ($query)

  {

  echo'<img src="red.jpg">';

  }

else

  {

  echo'<img src="green.jpg">';   

  }

?>

 

 

Link to comment
Share on other sites

oh im not sure on any of these -  mysql_fetch_array()  mysql_num_rows()  mysql_affected_rows() i tired to add it to the code but nothing happens or just one image would appear and they are not responding to the actual databse bieng updating. i am a bit of a NOOB unfortunantly.

Link to comment
Share on other sites

mysql_fetch_array() only gets one result row at a time. If you are expecting lots of results to come back, you need to use a loop to handle each one at a time.

 

For example:

 

while ($row = mysql_fetch_array($result)) {

    echo $row[0];

}

Link to comment
Share on other sites

mmmmm I did try it but nothing getting done right the code is not repeating  and sometimes causes errors. i have a seperate program that updates the value of the field in a table from "1" to "0" all the time when someone logs in and out so its allways getting updated i need php code to get this ALL the time,

 

can someone write the code giving indication what to do, been on this for 3 days got 2 days to finish this :( if you could would be much appriciated...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.